Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #define PI 3.141592
  5. int main()
  6. {
  7. float x, rad = (PI / 180), *num = (float*)malloc(sizeof(float));
  8. if (num == NULL)
  9. {
  10. puts("Error: failed to allocate memory.");
  11. return;
  12. }
  13. int count = 0;
  14. char ch;
  15. while (1)
  16. {
  17. finish:
  18. if (count > 0)
  19. {
  20. scanf(" %f", &num[count]);
  21. if (ch == '-')
  22. {
  23. num[count] = (num[count - 1] - num[count]);
  24. printf("%f\n", num[count]);
  25. }
  26. else if (ch == '+')
  27. {
  28. num[count] = (num[count - 1] + num[count]);
  29. printf("%f\n", num[count]);
  30. }
  31. else if (ch == ':')
  32. {
  33. num[count] = (num[count - 1] / num[count]);
  34. printf("%f\n", num[count]);
  35. }
  36. else if (ch == '*')
  37. {
  38. num[count] = (num[count - 1] * num[count]);
  39. printf("%f\n", num[count]);
  40. }
  41. }
  42. else
  43. {
  44. num[count] = 0;
  45. scanf(" %f", &num[count]);
  46. }
  47. here:
  48. scanf(" %c", &ch);
  49. switch (ch)
  50. {
  51. case'-':
  52. count++;
  53. num = (float*)realloc(num, sizeof(float)*(count + 1));
  54. if (num == NULL)
  55. {
  56. puts("Error: failed to allocate memory.");
  57. exit(1);
  58. }
  59. goto finish;
  60. case'+':
  61. count++;
  62. num = (float*)realloc(num, sizeof(float)*(count + 1));
  63. if (num == NULL)
  64. {
  65. puts("Error: failed to allocate memory.");
  66. exit(1);
  67. }
  68. goto finish;
  69. case':':
  70. count++;
  71. num = (float*)realloc(num, sizeof(float)*(count + 1));
  72. if (num == NULL)
  73. {
  74. puts("Error: failed to allocate memory.");
  75. exit(1);
  76. }
  77. goto finish;
  78. case'*':
  79. count++;
  80. num = (float*)realloc(num, sizeof(float)*(count + 1));
  81. if (num == NULL)
  82. {
  83. puts("Error: failed to allocate memory.");
  84. exit(1);
  85. }
  86. goto finish;
  87. case's':
  88. scanf(" %f", &x);
  89. num[count] = sin(x*rad);
  90. printf("%f\n", num[count]);
  91. goto here;
  92. case'c':
  93. scanf(" %f", &x);
  94. num[count] = cos(x*rad);
  95. printf("%f\n", num[count]);
  96. goto here;
  97. case't':
  98. scanf(" %f", &x);
  99. if (x == 90)
  100. puts("Error: no such value.");
  101. else
  102. {
  103. num[count] = (sin(x*rad) / cos(x*rad));
  104. printf("%f\n", num[count]);
  105. }
  106. goto here;
  107. case'l':
  108. scanf(" %f", &x);
  109. num[count] = log10(x);
  110. printf("%f", num[count]);
  111. goto here;
  112. case'e':
  113. scanf(" %f", &x);
  114. num[count] = exp(x);
  115. printf("%f\n", num[count]);
  116. goto here;
  117. case'/':
  118. num[count] = sqrt(num[count]);
  119. printf("%f\n", num[count]);
  120. goto here;
  121. case'^':
  122. scanf(" %f", &x);
  123. num[count] = pow(num[count], x);
  124. printf("%f\n", num[count]);
  125. goto here;
  126. case'D':
  127. for (int i = count; i >= 0; i--)
  128. num[i] = num[i + 1];
  129. num = (float*)realloc(num, sizeof(float));
  130. if (num == NULL)
  131. {
  132. puts("Error: failed to allocate memory.");
  133. exit(1);
  134. }
  135. count = 0;
  136. goto finish;
  137. case'Q':
  138. exit(0);
  139. default:
  140. puts("Error: no such char.");
  141. exit(1);
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement