Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define wielkosc 102
- int stringlength(const char *str)
- {
- for (int i = 0; ; i++)
- {
- if (str[i] == '\0')
- {
- return i;
- }
- }
- }
- int main()
- {
- char str[wielkosc] = { '\0' };
- printf("Podaj wyrazenie : ");
- fgets(str, wielkosc, stdin);
- //str[stringlength(str) - 1] = '\0';
- for (int i = 0; i < wielkosc - 2; ++i)
- {
- if (str[i] == '\0' || str[i] == '\n')
- break;
- if (!((str[i] >= '0' && str[i] <= '9') || str[i] == '+' || str[i] == '-'))
- {
- printf("Incorrect input");
- return 1;
- }
- }
- int currentValue = 0;
- char *c = &str[0];
- sscanf(c, "%d", ¤tValue);
- {
- char kalkulator[1024];
- sprintf(kalkulator, "%d", currentValue);
- c += stringlength(kalkulator);
- }
- while (*c != '\0')
- {
- char znak;
- int wartosc;
- if (sscanf(c, "%c%d", &znak, &wartosc) == 2)
- {
- if (znak == '+')
- {
- currentValue += wartosc;
- }
- else if (znak == '-')
- {
- currentValue -= wartosc;
- }
- }
- char kalkulator[1024];
- sprintf(kalkulator, "%c%d", znak, wartosc);
- c += stringlength(kalkulator);
- }
- printf("%d", currentValue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement