Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #define PI 3.141592
- int main()
- {
- float x, rad = (PI / 180), *num = (float*)malloc(sizeof(float));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- return;
- }
- int count = 0;
- char ch;
- while (1)
- {
- finish:
- if (count > 0)
- {
- scanf(" %f", &num[count]);
- if (ch == '-')
- {
- num[count] = (num[count - 1] - num[count]);
- printf("%f\n", num[count]);
- }
- else if (ch == '+')
- {
- num[count] = (num[count - 1] + num[count]);
- printf("%f\n", num[count]);
- }
- else if (ch == ':')
- {
- num[count] = (num[count - 1] / num[count]);
- printf("%f\n", num[count]);
- }
- else if (ch == '*')
- {
- num[count] = (num[count - 1] * num[count]);
- printf("%f\n", num[count]);
- }
- }
- else
- {
- num[count] = 0;
- scanf(" %f", &num[count]);
- }
- here:
- scanf(" %c", &ch);
- switch (ch)
- {
- case'-':
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- goto finish;
- case'+':
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- goto finish;
- case':':
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- goto finish;
- case'*':
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- goto finish;
- case's':
- scanf(" %f", &x);
- num[count] = sin(x*rad);
- printf("%f\n", num[count]);
- goto here;
- case'c':
- scanf(" %f", &x);
- num[count] = cos(x*rad);
- printf("%f\n", num[count]);
- goto here;
- case't':
- scanf(" %f", &x);
- if (x == 90)
- puts("Error: no such value.");
- else
- {
- num[count] = (sin(x*rad) / cos(x*rad));
- printf("%f\n", num[count]);
- }
- goto here;
- case'l':
- scanf(" %f", &x);
- num[count] = log10(x);
- printf("%f", num[count]);
- goto here;
- case'e':
- scanf(" %f", &x);
- num[count] = exp(x);
- printf("%f\n", num[count]);
- goto here;
- case'/':
- num[count] = sqrt(num[count]);
- printf("%f\n", num[count]);
- goto here;
- case'^':
- scanf(" %f", &x);
- num[count] = pow(num[count], x);
- printf("%f\n", num[count]);
- goto here;
- case'D':
- for (int i = count; i >= 0; i--)
- num[i] = num[i + 1];
- num = (float*)realloc(num, sizeof(float));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- count = 0;
- goto finish;
- case'Q':
- exit(0);
- default:
- puts("Error: no such char.");
- exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement