Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- int top;
- int items[100];
- } Stack;
- void initialize(Stack *s)
- {
- s->top=0;
- }
- void push(Stack *s,int value)
- {
- s->items[s->top++]=value;
- }
- int pop(Stack *s)
- {
- return s->items[--s->top];
- }
- int isfull(Stack *s)
- {
- return s->top<100?0:1 ;
- }
- int isempty(Stack *s)
- {
- return s->top==0?1:0;
- }
- int postfix(char str[])
- {
- Stack s;
- initialize(&s);
- int i;
- for (i=0; i<strlen(str); i++)
- {
- if (str[i]==' ')
- continue;
- if (str[i]<='9' && str[i]>='0')
- push(&s,str[i]-'0');
- else
- {
- int a,b;
- if (isempty(&s))
- printf("error.\n\n");
- else
- a=pop(&s);
- if (isempty(&s))
- printf("error.\n");
- else b=pop(&s);
- switch(str[i])
- {
- case '+':
- push(&s,b+a);
- break;
- case '-':
- push(&s,b-a);
- break;
- case '*':
- push(&s,b*a);
- break;
- case '/':
- push(&s,b/a);
- break;
- default:
- printf("error.\n");
- }
- }
- }
- return pop(&s);
- }
- int main()
- {
- char str[20];
- gets(str);
- printf("\n\n%s=%d",str,postfix(str));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement