1: #include<stdio.h>
2: #include<conio.h>
3: #include<string.h>
4: #define MAX 50
5: int stack[MAX];
6: char post[MAX];
7: int top=-1;
8: void pushstack(int tmp);
9: void calculator(char c);
10: void main()
11: {
12: int i;
13: clrscr();
14: printf("Insert a postfix notation :: ");
15: gets(post);
16: for(i=0;i<strlen(post);i++)
17: {
18: if(post[i]>='0' && post[i]<='9')
19: {
20: pushstack(i);
21: }
22: if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')
23: {
24: calculator(post[i]);
25: }
26: }
27: printf("\n\nResult :: %d",stack[top]);
28: getch();
29: }
30: void pushstack(int tmp)
31: {
32: top++;
33: stack[top]=(int)(post[tmp]-48);
34: }
35: void calculator(char c)
36: {
37: int a,b,ans;
38: a=stack[top];
39: stack[top]='\0';
40: top--;
41: b=stack[top];
42: stack[top]='\0';
43: top--;
44: switch(c)
45: {
46: case '+':
47: ans=b+a;
48: break;
49: case '-':
50: ans=b-a;
51: break;
52: case '*':
53: ans=b*a;
54: break;
55: case '/':
56: ans=b/a;
57: break;
58: case '^':
59: ans=b^a;
60: break;
61: default:
62: ans=0;
63: }
64: top++;
65: stack[top]=ans;
66: }
getsprogramming Tutorials , C Programming, CPP Programming and JAVA Programming , Python Programming , Javascript Programming Welcome to getsprogramming Blog Here You Find Different Programs in Easier way to Explain and gain Knowledge and Try Your Self..Thanks
Thursday, 24 September 2015
Write a C Program to Evaluate Postfix Expression using Stack
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment