how can i design this program

A

ASSASSIN

i need a program that convert a number form to written one
help me please and thank you
 
I

Ian Collins

ASSASSIN said:
i need a program that convert a number form to written one
help me please and thank you

Only if you rephrase your question so it makes sense!

If you want help, you should post an attempt.
 
J

joe

i need a program that convert a number form to written one
help me please and thank you

If you mean "How do I convert a numeric type into a string type", you
may try something in the boost library:

int foo = 5;
std::string answer = boost::lexical_cast<std::string>(foo);
 
J

Jeff Schwab

ASSASSIN said:
i need a program that convert a number form to written one

<jk> (1) Find a pencil... </jk>

You may find standard string-streams useful. Below is an example of how
to use a std::eek:stringstream to convert a primitive int to a standard string.

If this (or any other solution on this thread) appeals to you, please
post back and ask how to make the same code work for many different
numeric types (i.e. not just int). Hopefully, you will get a nice
introduction to both templates and function overloads.

#include <cassert>
#include <sstream>
#include <string>

std::string
to_string(int a_number) {
std::eek:stringstream out;
out << a_number;
return out.str();
}

int main() {
std::string const s = to_string(42);
assert(s == "42");
}
 
T

Thomas J. Gritzan

Jeff said:
<jk> (1) Find a pencil... </jk>

You may find standard string-streams useful. Below is an example of how
to use a std::eek:stringstream to convert a primitive int to a standard
string.

If this (or any other solution on this thread) appeals to you, please
post back and ask how to make the same code work for many different
numeric types (i.e. not just int). Hopefully, you will get a nice
introduction to both templates and function overloads.

Why not just read the FAQ instead, where exactly this template stuff is
written down, and ask when something is unclear?
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html
 
J

James Kanze

You may find standard string-streams useful. Below is an
example of how to use a std::eek:stringstream to convert a
primitive int to a standard string.

I just wonder if he wasn't looking for something to convert an
integer value of 1234 to a string like "one thousand two hundred
thirty four". (I wrote one once which worked for English,
French and German. Handling i18n issues for this was, shall we
say, quite amusing.)
 
J

Jeff Schwab

James said:
I just wonder if he wasn't looking for something to convert an
integer value of 1234 to a string like "one thousand two hundred
thirty four". (I wrote one once which worked for English,
French and German. Handling i18n issues for this was, shall we
say, quite amusing.)

I did a simple-minded version of this once (no i18n "amusement" :)) for
a high-level dashboard application, that reported how many of our
nightly tests had failed for each set of sample input. It had
descriptions like "there were four tests, of which two failed," or
"there were six tests, and all of them passed." The numbers were
obtained from a simple dictionary lookup, though, so for numbers bigger
than the size of the dictionary, I reverted to printing decimal digits.
Once our regression test suite had grown large enough to trigger these
larger numbers, I got a complaint that the effect was jarring: Either
print the numbers in English, or as Arabic numerals, but for the love of
Knuth do *not* switch back and forth!
 
J

James Kanze

yes James ...my frinde . thats exactly what I mean , can you
lend me a hand :) !!!!!

Not really very much. It was a long, long time ago (and the
actual code was in assembler 8086), and I've forgotten many of
the details (except that most languages have some special tricky
parts: German uses "one and twenty", rather than "twenty one",
and French has things like "sixty-fifteen", rather than
"seventy-five"). The basic idea, however, is to use a number of
tables: for the units, for the tens, and for the names of the
higher entities. Something like:

char const unitStrings[] = {
NULL,
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
} ;

char const tenStrings[] = {
NULL,
NULL,
"twenty",
"thirty",
"fourty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninty"
} ;

char const powerStrings[] = {
NULL,
"thousand",
"million",
// ...
}

I do remember than I basically handled the number by powers of a
thousand, recursively: the basic function started out something
like:

std::string
convert( unsigned n, int i = 0 )
{
std::string result ;
if ( n / 1000 != 0 ) {
result = convert( n / 1000, i + 1 ) ;
}
n %= 1000 ;
int units = n % 10 ;
n /= 10 ;
int tens = n % 10 ;
n /= 10 ;
int hundreds = n ;
if ( hundreds != 0 ) {
if ( ! result.empty() ) {
result += ' ' ;
}
if ( unitStrings[ hundreds ] != NULL ) {
result += unitStrings[ hundreds ] ;
result += ' ' ;
}
result += "hundred" ;
}
while ( tens != 0 && tenStrings[ tens ] == NULL ) {
-- tens ;
units += 10 ;
}
if ( tens != 0 ) {
if ( ! result.empty() ) {
result += ' ' ;
}
result += tenStrings[ tens ] ;
}
if ( units != 0 ) {
if ( ! result.empty() ) {
result += ' ' ;
}
result += unitStrings[ units ] ;
}
if ( i != 0 ) {
result += ' ' ;
result += powerStrings[ i ] ;
}
return result ;
}

That's just off the top of my head, of course---no guarantees.
It was a lot hairier in assembler:). With support for German.
You'll notice however that the same trick I used to get the
teens right works for the French sixy-fifteen; just leave the
entry for seventy NULL. After that, there are a lot of
subtilities: the French say "twenty and one" (but "twenty two",
the "and" is only used if the tens string is followed by "one"),
and back then, the word for hundred was written in the plurial
if there was more than one of them, AND the number was a round
hundreds, i.e. "deux cents", but "deux cent un". And German
didn't use any spaces, French used hyphens between the tens and
the units (but spaces elsewhere). And French spells "thousand"
"mil" if it's in a year, but "mille" in all other contexts. And
probably a few other details that have slipped my mind for the
moment. And in general: do you write "one hundred", or
"hundred" (both French and German strongly favor the latter)?
Still, the above should be a reasonable starting point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,499
Latest member
DewittK739

Latest Threads

Top