Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct{
- int top;
- int items[251];
- }STACK;
- void push(int x, STACK *ps){
- if(ps->top == 250){
- printf("Error: stack overflow");
- exit(EXIT_FAILURE);
- }else
- ps->items[++(ps->top)] = x;
- return;
- }
- int pop(STACK *ps){
- /* if(empty(ps)){
- printf("Error: stack underflow");
- exit(EXIT_FAILURE);
- }else */
- return(ps->items[(ps->top)--]);
- }
- int main(void){
- int pole [30];
- char string [250];
- STACK stack;
- stack.top = -1;
- int cislo;
- int operacia;
- int vysledok;
- while(scanf("%s",string) != EOF){
- if (strcmp("+",string)){
- operacia = pop(&stack) + pop(&stack);
- push(operacia,&stack);}else
- if (strcmp("-",string)){
- operacia = pop(&stack) + pop(&stack);
- push(operacia,&stack);}else
- if (strcmp("*",string)){
- operacia = pop(&stack) + pop(&stack);
- push(operacia,&stack);}else
- if (strcmp("/",string)){
- operacia = pop(&stack) + pop(&stack);
- push(operacia,&stack);}else{
- cislo = atoi(string);
- push(cislo,&stack);
- }
- }
- vysledok = pop(&stack);
- printf("%d",vysledok);
- return 0;
- }
Add Comment
Please, Sign In to add comment