Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct stack{
  5. int *tab;
  6. int top;
  7. int maxsize;
  8. }stack;
  9. void push(stack* stos, int el){
  10. stos->top++;
  11. stos->tab[stos->top]=el;
  12. }
  13. int pop(stack* stos){
  14. int res = stos->tab[stos->top];
  15. stos->top--;
  16. return res;
  17. }
  18. void add(stack *stos){
  19. int a = pop(stos);
  20. int b = pop(stos);
  21. push(stos, b+a);
  22. }
  23. void subtract(stack *stos){
  24. int a = pop(stos);
  25. int b = pop(stos);
  26. push(stos, b-a);
  27. }
  28. void multiply(stack *stos){
  29. int a = pop(stos);
  30. int b = pop(stos);
  31. push(stos, b*a);
  32. }
  33. void divide(stack *stos){
  34. int a = pop(stos);
  35. int b = pop(stos);
  36. push(stos, b/a);
  37. }
  38. int calconp(stack *stos, int n, int k){
  39. char *what;
  40. what = malloc((k+1)*sizeof(char));
  41. int i=n;
  42. while(i>0 || stos->top>0){
  43. scanf("%s", what);
  44. if(what[0]=='+') add(stos);
  45. if(what[0]=='-') subtract(stos);
  46. if(what[0]=='*') multiply(stos);
  47. if(what[0]=='/') divide(stos);
  48. if(what[0]>=48 && what[0]<58){
  49. int number = 0;
  50. int j=0;
  51. while(j<k && what[j]!='\0'){
  52. number = 10*number+what[j]-48;
  53. j++;
  54. }
  55. push(stos,number);
  56. i--;
  57. }
  58. }
  59. free(what);
  60. return stos->tab[0];
  61. }
  62.  
  63.  
  64.  
  65. int main(){
  66. int n, k;
  67. scanf("%d %d", &n, &k);
  68. stack *stos=malloc(2*sizeof(int)+sizeof(int*));
  69. stos->tab=malloc(n*sizeof(int));
  70. stos->top=-1;
  71. stos->maxsize=n;
  72. int res = calconp(stos,n,k);
  73. printf("%d", res);
  74. free(stos->tab);
  75. free(stos);
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement