Thread: dealing math in string with C.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Unhappy dealing math in string with C.

    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;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well you have a word to int conversion function, but it could probably do a lot more than just convert one digit. If it converted a whole operand then your calculations would be very natural. You also have a stub function that needs to be written or excised. An int to word function would also be helpful in the output phase as well. To that end, my suggestion for improvement something like this, only to provoke thought:
    Code:
    void int_to_word (int foo , char *bar , size_t bar_len)
    {
    	const char *const ONES[10] =
    	{
    		"zero" , "one" , "two" , "three" , "four" , "five" , "six" , 
    		"seven" , "eight" , "nine"
    	};
    	int wrote = 0;
    
    	while (foo > 0 && wrote < (int)bar_len)
    	{
    		wrote += snprintf (bar + wrote , bar_len - wrote , "%s " , ONES[foo / 10]);
    		foo %= 10;
    	}
    
    	return ;
    }
    Naturally, this applies just as much to the conversion function that goes the other way.
    Last edited by whiteflags; 05-09-2010 at 01:16 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Code:
    	int wrote = 0;
    
    	while (foo > 0 && wrote < (int)bar_len)
    	{
    		wrote += snprintf (bar + wrote , bar_len - wrote , "%s " , ONES[foo / 10]);
    		foo %= 10;
    	}
    
    	return ;
    }
    cant figure out what snprinf here use for.

    yet I tried to make the Int to Word function in pointer way,
    but that may neglect some bugs when output is a result can be divided with no remainder,such as 10,20,30.......etc

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The idea with snprintf is to print the string ONES[foo / 10] to bar. A complete breakdown of the call follows:

    wrote += snprintf (bar + wrote , bar_len - wrote , "%s " , ONES[foo / 10]);

    The first argument bar + wrote, computes our place in the string bar. We start out with wrote as zero, which translates to the front of the string on the first call.

    The second argument is bar_len - wrote, which computes how many bytes at most snprintf should write to bar. If ONES[foo / 10] is too long of a string, then bar will be truncated in lieu of accessing bar out of bounds.

    The third argument "%s " tells sprintf we expect a string and a space as output.

    The last argument is that which is to be copied.

    What you say is an error is not an error at all. For foo that is 10, you would copy ONES[10 / 10] or ONES[1]. Next, foo %= 10; updates foo so that foo / 10 is the next digit. Except after the update, foo is zero, which fails the while condition. That means we're done.

    The only real constraint of int_to_word is that foo must be positive.

    Like I said earlier, I hope this gives you ideas for word to int conversion. You could clean up a lot if you converted whole operands. As I showed you how to break down a integer by division, you will have to figure out how to build an integer by first looking up digits by word, and using multiplication and addition to put that digit in its place.
    Last edited by whiteflags; 05-09-2010 at 09:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM