Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- struct node
- {
- int data;
- struct node *next;
- };
- void push(int, struct node **);
- int pop(struct node **);
- int eval(int, int, char);
- void main()
- {
- char c;
- struct node *start=NULL;
- while((c=getchar())!='\n')
- {
- if(isdigit(c))
- {
- push((c-'0'),&start);
- }
- if(c=='*' || c=='/' || c=='+' || c=='-')
- {
- push(eval(pop(&start),pop(&start), c),&start);
- }
- }
- printf("\nAnswer is: %d", pop(&start));
- }
- void push(int element, struct node **start)
- {
- struct node *new;
- new=(struct node *) (malloc(sizeof(struct node)));
- new->data=element;
- new->next=NULL;
- if((*start)==NULL)
- {
- *start=new;
- return;
- }
- new->next=*start;
- *start=new;
- }
- int pop(struct node **start)
- {
- struct node *p;
- int x;
- p=(*start);
- (*start)=(p->next);
- x=p->data;
- free(p);
- return(x);
- }
- int eval(int top, int bot, char c)
- {
- printf("\n %d %d %c",top,bot,c);
- switch(c)
- {
- // printf("\n %d %d %c",top,bot,c);
- case '+':
- return (bot+top);
- case '/':
- return (bot/top);
- case '*':
- return (bot*top);
- case '-':
- return (bot-top);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement