Hi, guys, I've been trying to get this code to work. First, get the postfix notation string from user (e.g. 3 4 +). The digits are single digits, there is one space between them. Nothing fancy. After I get the string, I clean it up from spaces, and then I try to perform calculations. Only + - * /, nothing else. However, the program does not work. It compiles, but I have 4 warnings:
integral size mismatch in argument : conversion supplied in the following line (all 4 of the same lines in the switch statement:
Code:
case '+': result = left + right;
i++; push(&myStack, result); break;
Here is the rest of the code. I do not paste here the functions as they have been working properly in other programs.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 50
typedef struct {
char elements[MAXSIZE];
int count;
} StackType;
StackType myStack;
void initialize(StackType *stackPtr);
int isEmpty(StackType *stackPtr);
int isFull(StackType *stackPtr);
void push(StackType *stackPtr, char c);
char pop(StackType *stackPtr);
int main()
{
int left,right;
int result = 0;
int i = 0;
char c;
char expression[MAXSIZE];
initialize(&myStack);
printf("Enter the expression in the RPN form: ");
gets(expression);
while(expression[i] != '\0')
{
if(expression[i] != ' ')
{
i++;
continue;
}
else
{
expression[i] = expression[i+1];
i++;
}
}
i = 0;
while(expression[i] != '\0')
{
if(isdigit(expression[i]))
{
push(&myStack, expression[i]);
i++;
continue;
}
else
{
c = pop(&myStack);
right = atoi(&c);
if(isEmpty(&myStack))
{
printf("The result is %d\n", right);
return 0;
}
c = pop(&myStack);
left = atoi(&c);
}
switch(expression[++i])
{
case '+': result = left + right;
i++; push(&myStack, result); break;
case '-': result = left - right;
i++; push(&myStack, result); break;
case '*': result = left * right;
i++; push(&myStack, result); break;
case '/': result = left / right;
i++; push(&myStack, result); break;
default: printf("Incorrect function. Aborting... \n"); return 0;
}
}
return 0;
}
I will appreciate if someone could take a look and tell me what I am doing wrong. I've been having problems with stacks (and queues as well)... and also this is the first time for me to use atoi and isdigit, so maybe that's where the problem is. Or the increment of i???
Thanks.