How to Compose Digits into Integer Descriptions?

AI Thread Summary
The discussion focuses on creating a program that reads digit characters and converts them into a verbal representation of their integer values. The initial approach involves reading the input as a string, converting characters to integers, and storing them in an integer vector. However, concerns arise regarding scalability, particularly with longer inputs. Suggestions include using an array of strings to represent place values (ones, tens, hundreds, etc.) and looping through the input string in reverse to match the correct place values. Additionally, it is advised to clean the input to remove non-digit characters and to handle pluralization correctly when printing the results. The importance of structuring the program to accommodate varying lengths of input is emphasized.
darkvalentine
Messages
11
Reaction score
0
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.
 
Technology news on Phys.org
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Don't ask homework questions :P

Proceeding along your original route of storing the char input and transforming it into an int vector:

Instead of switch/case, I'd create an array/vector of strings, with each string having the name "ones","tens","hundreds",...

Then just create a loop that runs backwards from the length of the input string down.

A couple details: you can determine how big an array is using sizeof( yourArray ). You might also want to do a preliminary parse to remove things like commas from the input.
 
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Here we go:

Code:
#!/usr/bin/perl
$_=<>;
s/\D+//gs;
print "$_ (in integers) is";
@_=split //;
for(0..$#_) { print " ".(($_>0)?"and ":"")."$_[$_] ".(qw/one ten hundred thousand ten-thousand hundred-thousand million ten-million hundred-million billion ten-billion hundred-billion trillion ten-trillion hundred-trillion quadrillion/)[$#_-$_] . (($_[$_]==1)?"":"s"); }
print "\n";

Seriously, though:

1) Homework = not here, see the sticky.
2) Clean your input (make sure there are no other characters like decimal points, commas, other characters, etc.)
3) Make one array of names (as suggested above), like "one", "ten", "hundred", etc. However, note: there's no "s" on the end. There's a difference when you print them between saying "5 hundreds", "1 hundred", or "0 hundreds". Only print an "s" after the place's name if the value is 1!
4) Make a second array of the characters that were input
5) Loop through the character array normally, but loop through the "place name" array differently-- start in the middle (where appropriate), and work backwards, rather than forwards.

DaveE
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top