Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define wielkosc 102
  6.  
  7. int main()
  8. {
  9.     char str[wielkosc] = { '\0' };
  10.  
  11.     printf("Podaj wyrazenie : ");
  12.     fgets(str, wielkosc, stdin);
  13.  
  14.     str[strlen(str) - 1] = '\0';
  15.  
  16.     for (int i = 0; i < wielkosc - 2; ++i)
  17.     {
  18.         if (str[i] == '\0')
  19.             break;
  20.  
  21.         if (!((str[i] >= '0' && str[i] <= '9') || str[i] == '+' || str[i] == '-'))
  22.         {
  23.             printf("Incorrect input");
  24.             return 1;
  25.         }
  26.     }
  27.  
  28.     int currentValue = 0;
  29.     char *c = &str[0];
  30.     sscanf(c, "%d", &currentValue);
  31.  
  32.     {
  33.         char kalkulator[1024];
  34.         sprintf(kalkulator, "%d", currentValue);
  35.         c += strlen(kalkulator);
  36.     }
  37.  
  38.     while (*c != '\0')
  39.     {
  40.         char znak;
  41.         int wartosc;
  42.  
  43.         if (sscanf(c, "%c%d", &znak, &wartosc) == 2)
  44.         {
  45.             if (znak == '+')
  46.             {
  47.                 currentValue += wartosc;
  48.             }
  49.             else if (znak == '-')
  50.             {
  51.                 currentValue -= wartosc;
  52.             }
  53.         }
  54.  
  55.  
  56.         // :)
  57.         char kalkulator[1024];
  58.         sprintf(kalkulator, "%c%d", znak, wartosc);
  59.         c += strlen(kalkulator);
  60.     }
  61.  
  62.     printf("%d", currentValue);
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement