c - Where to place printf to avoid multiple printouts in my rpn calculator? -


my rpn-calculator works, has problem. problem prints out every consecutive calculation makes along way.

i have tried different ways of fixing this, latest 1 being adding integer goes every calculation , printf prints if integer above 0 there being 1 number on stack.

however, causes problems when there more 1 calculation going on (for example writing 5 5 + 10 5 * *) cause 10 500 being printed out because there 1 item on stack after first calculation.

how can solve this?

#define max_stack_size 100 #define max_buffer_size 100  char buff[max_buffer_size];      double x, stack[max_stack_size];      double t;      int i, k=0, num_operand=0;   int main(void) {  while(x != 'q') {     if( scanf("%s", buff) < 1 )              { return 0; } if(isdigit(buff[0]) || isdigit(buff[1])) {     sscanf(buff, "%lf", &x);  if (num_operand < max_stack_size)                      {                              stack[num_operand]=x;                              num_operand ++;                      } else { printf("make stack bigger\n");}  } else { switch(buff[0]) {     case '+': stack[num_operand - 2] = stack[num_operand - 1] + stack[num_operand - 2];                                                 num_operand --;                                                 num_operand --;                                                 t = stack[num_operand];                                                 k++;                                                 num_operand ++;                                                 break;     case '-': stack[num_operand - 2] = stack[num_operand - 2] - stack[num_operand - 1];                                                 num_operand --;                                                 num_operand --;                                                 t = stack[num_operand];                                                 k++;                                                 num_operand ++;                                                 break;     case '/': stack[num_operand - 2] = stack[num_operand - 2] / stack[num_operand - 1];                                                 num_operand --;                                                 num_operand --;                                                 t = stack[num_operand];                                                 k++;                                                 num_operand ++;                                                 break;     case '*': stack[num_operand - 2] = stack[num_operand - 1] * stack[num_operand - 2];                                                 num_operand --;                                                 num_operand --;                                                 t = stack[num_operand];                                                 k++;                                                 num_operand ++;                                                 break;     } }     if (num_operand == 1 && k !=0) {         k = 0;         printf("%lf \n", t); } } } 

"%s" consumes leading white-spaces ' ' , '\n' alike - distinction between end of line , space separator lost.

to distinguish between lines of input use fgets() , process line. print result. @molbdnilo

it test q, code needs test textual contents of line, not double x. @bluepixy

int main(void) {   char line[max_buffer_size];    // read line   while (fgets(line, sizeof line, stdin) && line[0] != 'q') {     // set these variables per each line.     double x, stack[max_stack_size];     double t;     int i, k = 0, num_operand = 0;      const char *p = line;     char buff[max_buffer_size];     int n;  // # of characters scanned      // process tokens     while (sscanf(p, "%s %n", buff, &n) == 1) {       ...        // next token       p += n;     } // endwhile      // print     if (num_operand == 1 && k != 0) {       k = 0;       printf("%lf \n", t);       fflush(stdout);     }   } // endwhile 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -