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