Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "stack.h"
- void getInput()
- {
- char ch;
- while(1)
- {
- makeEmpty();
- printf("Enter an RPN expression: ");
- scanf("%c", &ch);
- if(ch == 'q')
- break;
- while(1)
- {
- if(ch == '\n')
- break;
- if(ch >= 48 && ch <= 57)
- {
- if(!isFull())
- {
- push(ch - 48);
- }
- else
- {
- printf("Expression is too complex\n");
- exit(EXIT_FAILURE);
- }
- }
- switch(ch)
- {
- case '+':
- push(add(pop(), pop()));
- break;
- case '-':
- push(sub(pop(), pop()));
- break;
- case '*':
- push(mul(pop(), pop()));
- break;
- case '/':
- push(divide(pop(), pop()));
- break;
- case '=':
- printf("%d\n", pop());
- break;
- }
- scanf("%c", &ch);
- }
- }
- }
- int add(int a, int b)
- {
- return a + b;
- }
- int sub(int a, int b)
- {
- return a - b;
- }
- int mul(int a, int b)
- {
- return a * b;
- }
- int divide(int a, int b)
- {
- return a / b;
- }
- int main()
- {
- getInput();
- printf("%d\n", pop());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement