hi my problem is that
whan i want to add 2+3 i enter it in postfix 23+ it give me 5 which is correct but whan i want to add 20+30 i enter 2030+ it add me 3&0 because i use char so, this progm is limited to 1 digit.so help me so tht i can can evaluate 12,7,3,-,/,2,1,5,+,*,+ so help me solving this problem thanks

#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<string.h>
#define MAX 50
char stack[MAX] ;
char postfix[MAX] ;
int top=-1;
char pop();
void push(char);
int Evaluate(void);
int power(int b,int a)
{if (a==0)
return 1;
return b*power(b,a-1);}
void main()
{
int m;
clrscr() ;
cout<<"enter postfix equation: ";
gets(postfix) ;
m=Evaluate();
cout<<m;
getch() ;
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(postfix);
for(i=0;i<l;i++)
{
if (postfix[i] != '^' &&postfix[i] != '*' &&postfix[i] != '+' &&postfix[i] != '/' &&postfix[i] != '-' )
{
push(postfix[i]);
}
else if(postfix[i] == '^' ||postfix[i] == '*' ||postfix[i] == '+' ||postfix[i] == '/' ||postfix[i] == '-' )
{
a = pop ( );
b = pop ( );
a-=48;
b-=48;

switch(postfix[i])
{
 case '^' : q=power(b,a); break;
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
push ( q );
}
}
z = pop ();
return z;
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}

Recommended Answers

All 2 Replies

For what type is it legal to use more than one char at a time in the type? That's the type that should be used to declare stack and the variables called a and b. Then you will need some other mechanism to convert that type to numerical values besides the -= 48 protocol. Once you have figured out the type to use, then the way to convert that type to numerical version will be easier to see, too, hopefully.

When you want to bump your own post it is best to post a request for clarification, etc, and not request assistance for your thread from a different, even if similar, thread.

I'd suggest changing stack to either be of some numerical type. If the input expression is something like this:

-0.6 37.6 + 47 * sin30 *

then you can use the >> to separate the different "tokens" in the expresion (a token can be either an operand or an operator) into strings. Evaluate the token string to see if it is an operand or an operator. If it is an operand then convert the token string to an appropriate numerical type and pop the numerical type onto a(n approriately declared) stack. If the token string is an operator then pop the appropriate number of operands from the stack, do the appropriate calculation and push the result back onto the stack. Since the tokens representing operators will also be strings you can either use an if/else sequence using strings as comparisons or use the first char of the string as the comparison in a switch statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.