Home > Software engineering >  How to make the calculator count the expression with spaces in the input?
How to make the calculator count the expression with spaces in the input?

Time:01-08

I tried to make calculator supporting brackets, but I have no idea how to deal if the user's input includes expression with spaces, for example: input: (2 3) * 2 i got: 2 If it's normally get (2 3)*2, it counts 10 . My code:

#include <stdio.h> 
#define MAX_SIZE 1024 
  
int insert_operand(int *operand, int * top_num, int num) /* data is pushed into the data stack*/
{
     (*top_num)   ;
     operand[*top_num] = num; /*save data*/
      
     return 0; /*Exit normally*/
}
  
int insert_oper (char * oper , int *top_oper , char ch) 
{
     (*top_oper)  ;
     oper[*top_oper] = ch; /*save operator*/
  
     return 0; /*Exit normally*/
}
  
int compare(char *oper , int *top_oper , char ch) /* compare the priority of the operating server*/
{
     
     if((oper[*top_oper] == '-' || oper[*top_oper] == ' ') /*Determine whether the current priority is higher than the priority of the operator at the top of the stack*/
             && (ch == '*' || ch == '/'))
     {
         return 0;
     }
  
     else if(*top_oper == -1 || ch == '('
             || (oper[*top_oper] == '(' && ch != ')')) /*Determine whether the operator stack is empty; whether the top operator is '('*/
     {
         return 0;
     }
  
     else if (oper[*top_oper] =='(' && ch == ')')
     {
         (*top_oper)--;
         return 1; 
     }
  
     else
     {
         return -1; /*Operate the operator*/
     }
  
}
  
int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*perform data operation*/
{
     int num_1 = operand[*top_num]; /*Take out two data from the data stack*/
     int num_2 = operand[*top_num - 1];
  
     int value = 0;
  
     if(oper[*top_oper] == ' ') 
     {
         value = num_1   num_2;
     }
  
     else if(oper[*top_oper] == '-') 
     {
         value = num_2 - num_1;
     }
  
     else if(oper[*top_oper] == '*') 
     {
         value = num_2 * num_1;
     }
  
     else if(oper[*top_oper] == '/') 
     {
         value = num_2 / num_1;
     }
  
     (*top_num) --; /*Move the top of the data stack down one bit*/
     operand[*top_num] = value; /*Push the obtained value into the data stack*/
     (*top_oper) --; /*Move the top of the operator stack down one bit*/
  
  
}
  
int main()
{
     int operand[MAX_SIZE] = {0}; /*data stack, initialize*/
     int top_num = -1;
  
     char oper[MAX_SIZE] = {0}; /*operator stack, initialize*/
     int top_oper = -1;
  
     char *str = (char *) malloc (sizeof(char) * 100); /*get expression (without =)*/
     scanf("%s", str);
  
     char* temp;
     char dest[MAX_SIZE];
     int num = 0;
  
     int i = 0;
     while(*str != '\0')
     {
         temp = dest;
  
         while(*str >= '0' && *str <= '9') /*judging whether it is data*/
         {
             *temp = *str;
             str  ;
             temp  ;
         } /*Encounter a symbol to exit*/
  
         if(*str != '(' && *(temp - 1) != '\0') /*Determine whether the symbol is '('*/
         {
             *temp = '\0';
  
             num = atoi(dest); /*convert string to number*/
             insert_operand(operand, &top_num,num); /*Push data into the data stack*/
         }
  
          while(1)
          {
              i = compare(oper,&top_oper,*str); /*judgment operator priority*/
  
             if(i == 0)
             {
                 insert_oper(oper,&top_oper,*str); /*press operator*/
                 break;
             }
  
             else if(i == 1) /*judging whether the expression in brackets ends*/
             {
                 str  ;
             }
  
             else if(i == -1) /* data processing */
             {
                 deal_date(operand,oper,&top_num,&top_oper);
             }
  
          }
  `
         str   ; /* point to the next character of the expression */
     }
  `
     printf("%d\n",operand[0]); /*output result*/
  
     return 0; 

I tried to count the equation even if there is a space in it. Can someone please help?

CodePudding user response:

Solving the problem by removing spaces:

So if you're working with equation as string you can simply remove spaces with function like this (there will be probably better way or function in library string.h but this was first guess):

char* DeleteSpaces( char* stringWithSpaces, size_t lengthOfString)
    {
    char* stringWithoutSpaces = (char*)calloc(lengthOfString   1, sizeof(char));
    if( !stringWithoutSpaces )
        return NULL;

    for( unsigned int x = 0, y = 0; x <= lengthOfString; x  , y   )
        {
        if( stringWithSpaces[x] == ' ' ) // if the character is space
            {
            while( stringWithSpaces[x] == ' ' && x < lengthOfString ) // skip all the spaces OR go away before you hit '\0'
                x  ;

            stringWithoutSpaces[y] = stringWithSpaces[x]; // then copy next character into new string
            }
        else  // if there's no space just copy the character
            stringWithoutSpaces[y] = stringWithSpaces[x];
        }

    return stringWithoutSpaces;
    }

This will basically remove all spaces from your received equation. If you really need the smallest possible memory requirement you can use realloc() at the end of the function for more optimal memory usage.

Here's simple example of how to use the function so you can test it:

int main()
{
    char firstString[] = "H        e l l o w o r l d\0";
    char* secondString;

    secondString = DeleteSpaces( firstString, strlen(firstString) );
    if( !secondString )
        return -1;

    printf( "%s", secondString );
    free( secondString );
    return 0;
}

Don't forget to use free(SecondString). I hope I helped you atleast a little :)

CodePudding user response:

As with the previous answer, I added in a function to remove any spaces from the entered formula in order to process the requested calculation. Also, coupled with that, I revised the "scanf" input to read in all of the entered characters which looked to be another symptom you were facing. With that, following is a refactored version of your program with the additional space compression function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 1024

int insert_operand(int *operand, int * top_num, int num) /* data is pushed into the data stack*/
{
     (*top_num)   ;
     operand[*top_num] = num; /*save data*/

     return 0; /*Exit normally*/
}

int insert_oper (char * oper , int *top_oper , char ch)
{
     (*top_oper)  ;
     oper[*top_oper] = ch; /*save operator*/

     return 0; /*Exit normally*/
}

int compare(char *oper , int *top_oper , char ch) /* compare the priority of the operating server*/
{

     if((oper[*top_oper] == '-' || oper[*top_oper] == ' ') /*Determine whether the current priority is higher than the priority of the operator at the top of the stack*/
             && (ch == '*' || ch == '/'))
     {
         return 0;
     }

     else if(*top_oper == -1 || ch == '('
             || (oper[*top_oper] == '(' && ch != ')')) /*Determine whether the operator stack is empty; whether the top operator is '('*/
     {
         return 0;
     }

     else if (oper[*top_oper] =='(' && ch == ')')
     {
         (*top_oper)--;
         return 1;
     }

     else
     {
         return -1; /*Operate the operator*/
     }

}

int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*perform data operation*/
{
     int num_1 = operand[*top_num]; /*Take out two data from the data stack*/
     int num_2 = operand[*top_num - 1];

     int value = 0;

     if(oper[*top_oper] == ' ')
     {
         value = num_1   num_2;
     }

     else if(oper[*top_oper] == '-')
     {
         value = num_2 - num_1;
     }

     else if(oper[*top_oper] == '*')
     {
         value = num_2 * num_1;
     }

     else if(oper[*top_oper] == '/')
     {
         value = num_2 / num_1;
     }

     (*top_num) --; /*Move the top of the data stack down one bit*/
     operand[*top_num] = value; /*Push the obtained value into the data stack*/
     (*top_oper) --; /*Move the top of the operator stack down one bit*/

    return value;
}

void compress(char *stx)    /* The additional function */
{
    char work[101];
    int i = strlen(stx);

    strcpy(work, stx);

    for (int j = 0; j < i; j  )
    {
        stx[j] = 0;
    }

    i = 0;

    for (int j = 0; j < (int)strlen(work); j  )
    {
        if (work[j] != ' ')
        {
            stx[i] = work[j];
            i  ;
        }
    }
}

int main()
{
     int operand[MAX_SIZE] = {0}; /*data stack, initialize*/
     int top_num = -1;

     char oper[MAX_SIZE] = {0}; /*operator stack, initialize*/
     int top_oper = -1;

     char *str = (char *) malloc (sizeof(char) * 100); /*get expression (without =)*/
     //scanf("%s", str);
     scanf("%[^\n]", str);  /* Refined the scanf call to receive all characters prior to the newline/return character */

     compress(str);         /* Added this function to remove spaces */

     char* temp;
     char dest[MAX_SIZE];
     int num = 0;

     int i = 0;
     while(*str != '\0')
     {
         temp = dest;

         while(*str >= '0' && *str <= '9') /*judging whether it is data*/
         {
             *temp = *str;
             str  ;
             temp  ;
         } /*Encounter a symbol to exit*/

         if(*str != '(' && *(temp - 1) != '\0') /*Determine whether the symbol is '('*/
         {
             *temp = '\0';

             num = atoi(dest); /*convert string to number*/
             insert_operand(operand, &top_num,num); /*Push data into the data stack*/
         }

          while(1)
          {
              i = compare(oper,&top_oper,*str); /*judgment operator priority*/

             if(i == 0)
             {
                 insert_oper(oper,&top_oper,*str); /*press operator*/
                 break;
             }

             else if(i == 1) /*judging whether the expression in brackets ends*/
             {
                 str  ;
             }

             else if(i == -1) /* data processing */
             {
                 deal_date(operand,oper,&top_num,&top_oper);
             }

          }

         str   ; /* point to the next character of the expression */
     }

     printf("%d\n",operand[0]); /*output result*/

     return 0;

}

Testing out your sample formula with some additional spacing to ensure the compression function was working properly, following was the terminal output.

@Vera:~/C_Programs/Console/Calculate/bin/Release$ ./Calculate 
(2   3) * 2
10

Give that a try and see if it meets the spirit of your project.

CodePudding user response:

As pointed out in the comments and other answers, the solution may be to simply "compact" the spaces out of the string before trying to analyse the string's content.

This doesn't require a lot of code:

#include <stdio.h>

char *stripSP( char *src ) {
    for( char *d = src, *s = src; ( *d = *s ) != '\0'; s   )
        d  = *d != ' ';
    return src;
}

int main( void ) {
    char *s[] = { "Hello", "H e   l l  ooo", "(2   5) * 3" };
    for( int i = 0; i < 3; i   ) {
        printf( "From '%s'  ", s[i] );
        printf( "'%s'\n", stripSP( s[i] ) );
    }
    return 0;
}
From 'Hello'  'Hello'
From 'H e   l l  ooo'  'Hellooo'
From '(2   5) * 3'  '(2 5)*3'

Even more compact would be to use array indexing:

char *stripSP( char s[] ) {
    for( int f=0, t=0; (s[t] = s[f  ]) != '\0'; t  = s[t] != ' ' ) {}
    return s;
}
  • Related