firstly so thankful for the prior replys for my last post.
I have done all the code with this programme.
Code:
the program is about
/* dealing a string in a way like " A + B = ? ",till the result equals to zero.
it requires:
A and B are all below 100 number;
A or B should be used in " zero " to " nine " english word.
using blank space to make sure which one is A or B;
readin string ,but not output the result until result equals to zero;
output all the result at one time,except the result-zero;
here is a example
one + two =
seven + ten =
zero + five =
two two + three four =
zero + zero =
three
one seven
five
five six
*/
yet my code still seem too heavy and mess much.
how to make it brief and clean?
appreciate anyone can edit my code.thx!!!!!!!!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int WordToInt(char* s) //convert the string-input into number
{
if(strcmp(s, "zero")==0) return 0;
if(strcmp(s, "one")==0) return 1;
if(strcmp(s, "two")==0) return 2;
if(strcmp(s, "three")==0) return 3;
if(strcmp(s, "four")==0) return 4;
if(strcmp(s, "five")==0) return 5;
if(strcmp(s, "six")==0) return 6;
if(strcmp(s, "seven")==0) return 7;
if(strcmp(s, "eight")==0) return 8;
if(strcmp(s, "nine")==0) return 9;
else
return -1;
}
void function(int result)
{
}
int main( void )
{
int left, right; //store two operands
int temp,temp2;
int core[100];
int i = 0 ,j;
int b;
/*input part*/
do
{
char s[100]; // the input-string
char *Ptemp; //dealing the current string
int Add1[2] = {0}; //store the first operand
int Add2[2] = {0}; //store the seconde operand
int Count1 = 0; //gather the how many num in first operand
int Count2 = 0; //gather the how many num in second operand
fgets(s, 100, stdin);
Ptemp = strtok(s, " ");
while(Ptemp) // scan all the blank space in the input-string
{
int num = WordToInt(Ptemp);
if(num >= 0) //the read-in is number
{
if(!b) //not meet the ' + ' yet
{ Add1[Count1++] = num; }
else //have met the ' + ' already
{ Add2[Count2++] = num; }
}
else //read-in is not a number
{
if(strcmp(Ptemp, "+")==0)
{ b = 1 ; } //have read in ' + '
}
Ptemp = strtok(NULL, " "); //read in the next string which is seperated by strtok function
}
left = Count1 > 1 ? (Add1[0]*10 + Add1[1]) : Add1[0]; // calculat the first operand
right = Count2 > 1 ? (Add2[0]*10 + Add2[1]) : Add2[0]; // calculate the second operand
core[i++] = left + right ; // the storage of the result .
} while ( left != 0 || right != 0 ) ; // if core[i] == 0,finish input
/*output part*/
for( j =0 ; j != i ; j ++)
{
if (core[j] == 10)
{printf("one zero\n"); goto A;} //A is on the lastline .
else if (core[j] == 20)
{printf("two zero\n"); goto A;}
else if (core[j] == 30)
{printf("three zero\n"); goto A;}
else if (core[j] == 40)
{printf("four zero\n"); goto A;}
else if (core[j] == 50)
{printf("five zero\n"); goto A;}
else if (core[j] == 60)
{printf("six zero\n"); goto A;}
else if (core[j] == 70)
{printf("seven zero\n"); goto A;}
else if (core[j] == 80)
{printf("eight zero\n"); goto A;}
else if (core[j] == 90)
{printf("nine zero\n"); goto A;}
if( core[j] < 10 && core[j] > 0 )
{
switch (core[j])
{
case 1:printf("one ");break;
case 2:printf("two ");break;
case 3:printf("three ");break;
case 4:printf("four ");break;
case 5:printf("five ");break;
case 6:printf("six ");break;
case 7:printf("seven ");break;
case 8:printf("eight ");break;
case 9:printf("nine ");break;
}
printf("\n");
}
else if( core[j] < 99 )
{
temp = core[j] / 10 ;
temp2 = core[j] % 10;
switch (temp)
{
case 1:printf("one ");break;
case 2:printf("two ");break;
case 3:printf("three ");break;
case 4:printf("four ");break;
case 5:printf("five ");break;
case 6:printf("six ");break;
case 7:printf("seven ");break;
case 8:printf("eight ");break;
case 9:printf("nine ");break;
}
switch (temp2)
{
case 1:printf("one ");break;
case 2:printf("two ");break;
case 3:printf("three ");break;
case 4:printf("four ");break;
case 5:printf("five ");break;
case 6:printf("six ");break;
case 7:printf("seven ");break;
case 8:printf("eight ");break;
case 9:printf("nine ");break;
}
printf("\n");
}
A: ;
}
return 0;
}