Hello everyone! I have QUITE the load of homework to do tonight, one portion of which being a computer program. I had the program nearly finished, and I emailed it to myself from school. Unfortunately, it didn't get through - I have no program, and it's due tomorrow. Mine was like 500 lines of code, though a friend of mine finished his in 100 .. If anyone has any creative ideas of how to do this program, I'd be much obliged:
- Has to be constructed in WinOOT
- Input a number up to seven digits long, e.g - 1523682
- Outputs the number converted to english, e.g - 1 million 5 hundred twenty three thousand six hundred eighty two
-> As you can see from the above examples, the english number is fairly basic; no punctuation, no 'and', etc!
Thanks in advance!
Help with computer program!
- Eldaran Celuril
- Posts: 9
- Joined: Mon Feb 24, 2003 5:25 pm
- Location: Where the wind takes me ..
- Contact:
- Eldaran Celuril
- Posts: 9
- Joined: Mon Feb 24, 2003 5:25 pm
- Location: Where the wind takes me ..
- Contact:
- Eldaran Celuril
- Posts: 9
- Joined: Mon Feb 24, 2003 5:25 pm
- Location: Where the wind takes me ..
- Contact:
Have no idea what WinOOT is - but haveing a hard time to back down from programming problems I've solved it in Java
The code isn't very optimized and can be improved massive, also presently it requiers a 7 digit number as input, but that is easy to fix:
public class NumberInterp
{
private int input;
private int[] temp = new int[7];
private int temp2 = 1000000;
private int index = 6;
private String output;
public NumberInterp(int input)
{
this.input = input;
output = "";
}//end constructor
public String start()
{
while (input > 0)
{
temp[index] = input/temp2;
input = input%temp2;
index--;
temp2 = temp2/10;
}//end while
if (temp[6] > 0)
{ output += buildNumber1(temp[6]) + " million "; }
if (temp[5] > 0)
{ output += buildNumber1(temp[5]) + " hundred "; }
if (temp[4] > 0)
{ output += buildNumber2(temp[4]); }
if (temp[3] > 0)
{ output += buildNumber1(temp[3]); }
output += " thounsand ";
if (temp[2] > 0)
{ output += buildNumber1(temp[2]) + " hundred "; }
if (temp[1] > 0)
{ output += buildNumber2(temp[1]); }
if (temp[0] > 0)
{ output += buildNumber2(temp[0]); }
return output;
}
private String buildNumber1(int index)
{
switch (index)
{
case 1: return " one ";
case 2: return " two ";
case 3: return " three ";
case 4: return " four ";
case 5: return " five ";
case 6: return " six ";
case 7: return " seven ";
case 8: return " eight ";
case 9: return " nine ";
}//end switch
return null;
}//end method
private String buildNumber2(int index)
{
switch (index)
{
case 1: return " ten ";
case 2: return " twenty ";
case 3: return " thirty ";
case 4: return " forty ";
case 5: return " fifty ";
case 6: return " sixty ";
case 7: return " seventy ";
case 8: return " eighty ";
case 9: return " ninty ";
}//end switch
return null;
}//end method
}//end class
called with the parameter:
System.out.println(new NumberInterp(1234567).start());
it will return:
one million two hundred thirty four thounsand five hundred sixty seven
Basicall there are two "tricks" to this problem:
one you need to convert your input number so you know how many of "each" you have, eg 1 million and 2 hundred thousands.
This can easiestly (imo) be done with the modulus operation:
Interger divide in programming will return the number of times one integer is dividable by another eg:
10 / 3 = 3.
Modulus will return the remainder after a integer division:
10 % 3 = 1. (java and most other uses % as modulus, Pascal uses the keyword "mod")
This can be used to do the main calculation:
1234567 / 1000000 = 1
1234567 % 1000000 = 234567
Thus you know you have 1 millions and the remainder is 234567.
Running next modulus and integer dividing:
234567 / 100000 = 2
234567 % 100000 = 34567
and so on.
This is what happens in the first while loop.
Secondly you need to translate the 1, 2 etc into one, two, twenty etc.
This I use a swich structure for - it is basically just if constructions. And to avoid writing it all several times I've placed the code in methods: buildNumber1 and buildNumber2.
buildNumber1 returns a String with "one", "two" etc depending if the number is a 1,2 etc where as buildnumber2 returns "ten, twenty" etc.s if the number is a 1, 2....
Hope this helps a bit, if not I can try to explain it better
The code isn't very optimized and can be improved massive, also presently it requiers a 7 digit number as input, but that is easy to fix:
public class NumberInterp
{
private int input;
private int[] temp = new int[7];
private int temp2 = 1000000;
private int index = 6;
private String output;
public NumberInterp(int input)
{
this.input = input;
output = "";
}//end constructor
public String start()
{
while (input > 0)
{
temp[index] = input/temp2;
input = input%temp2;
index--;
temp2 = temp2/10;
}//end while
if (temp[6] > 0)
{ output += buildNumber1(temp[6]) + " million "; }
if (temp[5] > 0)
{ output += buildNumber1(temp[5]) + " hundred "; }
if (temp[4] > 0)
{ output += buildNumber2(temp[4]); }
if (temp[3] > 0)
{ output += buildNumber1(temp[3]); }
output += " thounsand ";
if (temp[2] > 0)
{ output += buildNumber1(temp[2]) + " hundred "; }
if (temp[1] > 0)
{ output += buildNumber2(temp[1]); }
if (temp[0] > 0)
{ output += buildNumber2(temp[0]); }
return output;
}
private String buildNumber1(int index)
{
switch (index)
{
case 1: return " one ";
case 2: return " two ";
case 3: return " three ";
case 4: return " four ";
case 5: return " five ";
case 6: return " six ";
case 7: return " seven ";
case 8: return " eight ";
case 9: return " nine ";
}//end switch
return null;
}//end method
private String buildNumber2(int index)
{
switch (index)
{
case 1: return " ten ";
case 2: return " twenty ";
case 3: return " thirty ";
case 4: return " forty ";
case 5: return " fifty ";
case 6: return " sixty ";
case 7: return " seventy ";
case 8: return " eighty ";
case 9: return " ninty ";
}//end switch
return null;
}//end method
}//end class
called with the parameter:
System.out.println(new NumberInterp(1234567).start());
it will return:
one million two hundred thirty four thounsand five hundred sixty seven
Basicall there are two "tricks" to this problem:
one you need to convert your input number so you know how many of "each" you have, eg 1 million and 2 hundred thousands.
This can easiestly (imo) be done with the modulus operation:
Interger divide in programming will return the number of times one integer is dividable by another eg:
10 / 3 = 3.
Modulus will return the remainder after a integer division:
10 % 3 = 1. (java and most other uses % as modulus, Pascal uses the keyword "mod")
This can be used to do the main calculation:
1234567 / 1000000 = 1
1234567 % 1000000 = 234567
Thus you know you have 1 millions and the remainder is 234567.
Running next modulus and integer dividing:
234567 / 100000 = 2
234567 % 100000 = 34567
and so on.
This is what happens in the first while loop.
Secondly you need to translate the 1, 2 etc into one, two, twenty etc.
This I use a swich structure for - it is basically just if constructions. And to avoid writing it all several times I've placed the code in methods: buildNumber1 and buildNumber2.
buildNumber1 returns a String with "one", "two" etc depending if the number is a 1,2 etc where as buildnumber2 returns "ten, twenty" etc.s if the number is a 1, 2....
Hope this helps a bit, if not I can try to explain it better
Insert signature here.
Originally posted by Tamerlane
@ Xandax
Just had that sick feeling going through me. Thanks for reminding me to do my homework.![]()
Never thought I'd live to see the day when Java is shown in all its horrible glory on SYM.![]()
no problem - anytime
BTW - just looked at the code and found out that both switch methods could be replaced by 2 array of strings wich would be much easier.
Making a string array holding "one"; two"; etc... and another "ten"; "twenty"..... and then using index-1 to get ahold of prober conversion - thus index of 1 subtracted 1 = 0, and then the arrayindex of 0 could be one or ten, depending on the array
Bah - hate finding easier ways just after I posted it
Actually I think I could shorten it down to about half the code with a few modifications
Insert signature here.