Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct stack{
- int *tab;
- int top;
- int maxsize;
- }stack;
- void push(stack* stos, int el){
- stos->top++;
- stos->tab[stos->top]=el;
- }
- int pop(stack* stos){
- int res = stos->tab[stos->top];
- stos->top--;
- return res;
- }
- void add(stack *stos){
- int a = pop(stos);
- int b = pop(stos);
- push(stos, b+a);
- }
- void subtract(stack *stos){
- int a = pop(stos);
- int b = pop(stos);
- push(stos, b-a);
- }
- void multiply(stack *stos){
- int a = pop(stos);
- int b = pop(stos);
- push(stos, b*a);
- }
- void divide(stack *stos){
- int a = pop(stos);
- int b = pop(stos);
- push(stos, b/a);
- }
- int calconp(stack *stos, int n, int k){
- char *what;
- what = malloc((k+1)*sizeof(char));
- int i=n;
- while(i>0 || stos->top>0){
- scanf("%s", what);
- if(what[0]=='+') add(stos);
- if(what[0]=='-') subtract(stos);
- if(what[0]=='*') multiply(stos);
- if(what[0]=='/') divide(stos);
- if(what[0]>=48 && what[0]<58){
- int number = 0;
- int j=0;
- while(j<k && what[j]!='\0'){
- number = 10*number+what[j]-48;
- j++;
- }
- push(stos,number);
- i--;
- }
- }
- free(what);
- return stos->tab[0];
- }
- int main(){
- int n, k;
- scanf("%d %d", &n, &k);
- stack *stos=malloc(2*sizeof(int)+sizeof(int*));
- stos->tab=malloc(n*sizeof(int));
- stos->top=-1;
- stos->maxsize=n;
- int res = calconp(stos,n,k);
- printf("%d", res);
- free(stos->tab);
- free(stos);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement