Advertisement
Guest User

Untitled

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