1

The question being asked is to evaluate RPN expressions and have the = be the terminating character of the sequence so that the program runs the RPN and calculates the expression it is given. So what I'm having some trouble with is understanding how I should convert my characters to integers as the instructions specifically say use scanf("%c", &ch) which pulls the input in as characters and not ints. How would I convert my characters to ints so I can push them to the array and do the operations on them accordingly?

//
// input: 1 2 3 * + =
// output: 7
//

#include <stdio.h>
#include <stdlib.h>
int collection[100];
int top;

void push(double v){
    if (top>99)
    {
        printf ("Stack Overflow\n");
        exit(0);
    }
    collection[++top]=v;
}
double pop(){
    double v;
    if(top < 0)
    {
        printf("stack underflow\n");
        exit(0);
    }
    v=collection[--top];
    return v;

}

int main(void){
    char ch;
    double a,b,c,sum;
    int i;
    top=-1;

    printf("Enter an RPN expression: ");
    while(ch!='='){
        scanf("%c", &ch);
        i=0;
        c=1;
        push(ch);
        if(collection[i]=='+'){
            a=pop();
            b=pop();
            c=b+a;
            push(c);
        }
        else if(collection[i]=='-'){
            a=pop();
            b=pop();
            c=b-a;
            push(c);
        }
        else if(collection[i]=='*'){
            a=pop();
            b=pop();
            c=b*a;
            push(c);
        }
        else if(collection[i]=='/'){
            a=pop();
            b=pop();
            c=b/a;
            push(c);
        }
        else{
            while(collection[i]!=0){
                i++;
            }
            i=i-1;
            sum=0;
            while(i>=0){
                sum=sum+((collection[i]-48)*c);
                c=c*10;
                i--;
            }
            push(sum);
        }
    }
    printf("%lf\n",c);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Senglish
  • 115
  • 1
  • 3
  • 9
  • 1
    The title and the code both bear little relevance to the question. If you need help converting characters to integers, say that in the title. By the way it's Polish, not "Polar". – n. m. could be an AI Oct 14 '15 at 05:53
  • When you push to a stack (or append to an array), you access the array first and increment then: `arr[n++]`. When you pop the stack, you decrement fisrt, then access the array. `arr[--n]`. That's because `n` is the index one beyond the array. You've got the former wrong. – M Oehm Oct 14 '15 at 06:09
  • Note that `printf("%lf\n",c);` where `c` is of type `char` is going to lead to unhappiness; the `%lf` conversion specification requires a `double` argument, not an `int` (which is the type the `char` value will be promoted to). – Jonathan Leffler Oct 14 '15 at 06:31

2 Answers2

0

Use double atof(const char *nptr);

The atof() function converts the initial portion of the string pointed to by nptr to double. Although your programming structure in giving commands to the calculator is bad. Use separate functions for each task in algorithm to avoid complications

Here is (some of) my RPN calculator:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cal.h"
#define MAXOP 100

int main(){

   int type;
   double op2,op3;
   char s[MAXOP];

   while((type = getop(s)) != EOF){
       switch(type){
       case NUMBER:
            push(atof(s));
            break;
       case 'x':
            op2 = pop();
            op3 = pop();
            push(op2);
            push(op3);
            break;
       case 'o':
            clear();
            break;
       case 's':
            push(sin(pop()));
            break;
       case '+':
            push(pop() + pop());
            break;
       case '*':
            push(pop() * pop());
            break;
       case '-':
            op2 = pop();
            push(pop() - op2);
            break;
       case '/':
            op2 = pop();
            push(pop() / op2);
            break;
       case '%':
            op2 = pop();
            push((int)pop() % (int)op2);
            break;
       case '\n':
            printf("\t%.4g\n",showtop());
            break;
       default: 
            printf("What was that?\n");
            break;
       }
   }

   return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

If satisfied with unsigned int digits:

char ch;
scanf( "%c", &ch );
ch -= '0';

Then you can compose the number from digits by multiplying by 10 and adding the next digit.

renonsz
  • 571
  • 1
  • 4
  • 17