Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- int main()
- {
- int count = 0;
- char ch;
- float x, k = 2.718281, pi = 3.141592, rad = (pi / 180), *num = (float*)malloc(sizeof(float));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- return;
- }
- printf("Calculate numbers until writing worth and then pressing enter: ");
- while (1)
- {
- if (count > 0)
- {
- scanf(" %f", &num[count]);
- if (ch == '-')
- num[count] = (num[count - 1] - num[count]);
- else if (ch == '+')
- num[count] = (num[count - 1] + num[count]);
- else if (ch == ':')
- num[count] = (num[count - 1] / num[count]);
- else if (ch == '*')
- num[count] = (num[count - 1] * num[count]);
- }
- else
- scanf(" %f", &num[count]);
- finish:
- if (ch == '=')
- {
- scanf(" %c", &ch);
- if (!ch)
- {
- 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;
- printf("Calculate numbers until writing worth and then pressing enter: ");
- float conv;
- conv = atof(ch);
- num[count] = conv;
- goto finish;
- }
- else
- scanf(" %c", &ch);
- }
- scanf(" %c", &ch);
- switch (ch)
- {
- case'-': // subtraction
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- break;
- case'+': // connecting
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- break;
- case':': // division
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- break;
- case'*': // multiplication
- count++;
- num = (float*)realloc(num, sizeof(float)*(count + 1));
- if (num == NULL)
- {
- puts("Error: failed to allocate memory.");
- exit(1);
- }
- break;
- case'=':
- printf("The result is: %f\n", num[count]);
- goto finish;
- case's': // sin
- scanf(" %f", &x);
- num[count] = sin(x*rad);
- goto finish;
- case'c': // cos
- scanf(" %f", &x);
- num[count] = cos(x*rad);
- goto finish;
- case't': // tan
- scanf(" %f", &x);
- if (x == 90 || x == 270)
- puts("Error: no such value.");
- else
- num[count] = (sin(x*rad) / cos(x*rad));
- goto finish;
- case'l': // log
- scanf(" %f", &x);
- num[count] = log10(x);
- goto finish;
- case'e': // exp
- scanf(" %f", &x);
- num[count] = exp(x);
- case'k': // a mathematical constant
- scanf(" %f", &x);
- num[count] = pow(k, x);
- goto finish;
- case'P': // pi
- scanf(" %f", &x);
- num[count] = pow(pi, x);
- goto finish;
- case'n': // tuple
- scanf(" %f", &x);
- float j = 1;
- for (float i = 1; i <= x; i++)
- j *= i;
- num[count] = j;
- j = 1;
- goto finish;
- case'm': // mod
- printf("Enter x: ");
- scanf(" %f", &x);
- int y;
- printf("Enter y: ");
- scanf(" %f", &y);
- num[count] = fmod(x, y);
- goto finish;
- case'/': // root
- num[count] = sqrt(num[count]);
- goto finish;
- case'^': // exponent
- scanf(" %f", &x);
- num[count] = pow(num[count], x);
- goto finish;
- case'R': // reset
- 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;
- printf("Calculate numbers until writing worth and then pressing enter: ");
- break;
- case'Q': // quit
- exit(0);
- default:
- puts("Error: no such char.");
- exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement