Advertisement
kdelekta

Untitled

Apr 4th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. // Kalkulator ONP
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int convert (char * num, int length)
  6. {
  7.     int res = 0;
  8.     int idx = 0;
  9.     int multiplier = 1;
  10.  
  11.     for(int i = 1; i < length && num[i] != '\0'; i++) multiplier *= 10;
  12.  
  13.     while (multiplier > 0)
  14.     {
  15.         res += (num[idx] - 48) * multiplier;
  16.         multiplier /= 10;
  17.         idx++;
  18.     }
  19.  
  20.     return res;
  21. }
  22.  
  23. int main() {
  24.     int n, k;
  25.     scanf("%d %d", &n, &k);
  26.     int *arr = malloc(n * sizeof(int));
  27.     int idx = -1;
  28.  
  29.     while(n > 0 || idx > 0){
  30.         char *opr = malloc((k + 1) * sizeof(char));
  31.         scanf("%s", opr);
  32.         if(idx > 0 && (opr[0] == '+' || opr[0] == '-' || opr[0] == '*' || opr[0] == '/')) {
  33.             int a = arr[idx];
  34.             idx--;
  35.             int b = arr[idx];
  36.             if(opr[0] == '+'){
  37.                 arr[idx] = b + a;
  38.             }else if(opr[0] == '-'){
  39.                 arr[idx] = b - a;
  40.             }else if(opr[0] == '*'){
  41.                 arr[idx] = b * a;
  42.             }else if(opr[0] == '/'){
  43.                 arr[idx] = b / a;
  44.             }
  45.         }
  46.         else {
  47.             n--;
  48.             idx++;
  49.             arr[idx] = convert(opr, k);
  50.         }
  51.     }
  52.     printf("%d", arr[0]);
  53.     free(arr);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement