Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. int main()
  5. {
  6. int count = 0;
  7. char ch;
  8. float x, k = 2.718281, pi = 3.141592, rad = (pi / 180), *num = (float*)malloc(sizeof(float));
  9. if (num == NULL)
  10. {
  11. puts("Error: failed to allocate memory.");
  12. return;
  13. }
  14. printf("Calculate numbers until writing worth and then pressing enter: ");
  15. while (1)
  16. {
  17. if (count > 0)
  18. {
  19. scanf(" %f", &num[count]);
  20. if (ch == '-')
  21. num[count] = (num[count - 1] - num[count]);
  22. else if (ch == '+')
  23. num[count] = (num[count - 1] + num[count]);
  24. else if (ch == ':')
  25. num[count] = (num[count - 1] / num[count]);
  26. else if (ch == '*')
  27. num[count] = (num[count - 1] * num[count]);
  28. }
  29. else
  30. scanf(" %f", &num[count]);
  31. finish:
  32. if (ch == '=')
  33. {
  34. scanf(" %c", &ch);
  35. if (!ch)
  36. {
  37. for (int i = count; i >= 0; i--)
  38. num[i] = num[i + 1];
  39. num = (float*)realloc(num, sizeof(float));
  40. if (num == NULL)
  41. {
  42. puts("Error: failed to allocate memory.");
  43. exit(1);
  44. }
  45. count = 0;
  46. printf("Calculate numbers until writing worth and then pressing enter: ");
  47. float conv;
  48. conv = atof(ch);
  49. num[count] = conv;
  50. goto finish;
  51. }
  52. else
  53. scanf(" %c", &ch);
  54. }
  55. scanf(" %c", &ch);
  56. switch (ch)
  57. {
  58. case'-': // subtraction
  59. count++;
  60. num = (float*)realloc(num, sizeof(float)*(count + 1));
  61. if (num == NULL)
  62. {
  63. puts("Error: failed to allocate memory.");
  64. exit(1);
  65. }
  66. break;
  67. case'+': // connecting
  68. count++;
  69. num = (float*)realloc(num, sizeof(float)*(count + 1));
  70. if (num == NULL)
  71. {
  72. puts("Error: failed to allocate memory.");
  73. exit(1);
  74. }
  75. break;
  76. case':': // division
  77. count++;
  78. num = (float*)realloc(num, sizeof(float)*(count + 1));
  79. if (num == NULL)
  80. {
  81. puts("Error: failed to allocate memory.");
  82. exit(1);
  83. }
  84. break;
  85. case'*': // multiplication
  86. count++;
  87. num = (float*)realloc(num, sizeof(float)*(count + 1));
  88. if (num == NULL)
  89. {
  90. puts("Error: failed to allocate memory.");
  91. exit(1);
  92. }
  93. break;
  94. case'=':
  95. printf("The result is: %f\n", num[count]);
  96. goto finish;
  97. case's': // sin
  98. scanf(" %f", &x);
  99. num[count] = sin(x*rad);
  100. goto finish;
  101. case'c': // cos
  102. scanf(" %f", &x);
  103. num[count] = cos(x*rad);
  104. goto finish;
  105. case't': // tan
  106. scanf(" %f", &x);
  107. if (x == 90 || x == 270)
  108. puts("Error: no such value.");
  109. else
  110. num[count] = (sin(x*rad) / cos(x*rad));
  111. goto finish;
  112. case'l': // log
  113. scanf(" %f", &x);
  114. num[count] = log10(x);
  115. goto finish;
  116. case'e': // exp
  117. scanf(" %f", &x);
  118. num[count] = exp(x);
  119. case'k': // a mathematical constant
  120. scanf(" %f", &x);
  121. num[count] = pow(k, x);
  122. goto finish;
  123. case'P': // pi
  124. scanf(" %f", &x);
  125. num[count] = pow(pi, x);
  126. goto finish;
  127. case'n': // tuple
  128. scanf(" %f", &x);
  129. float j = 1;
  130. for (float i = 1; i <= x; i++)
  131. j *= i;
  132. num[count] = j;
  133. j = 1;
  134. goto finish;
  135. case'm': // mod
  136. printf("Enter x: ");
  137. scanf(" %f", &x);
  138. int y;
  139. printf("Enter y: ");
  140. scanf(" %f", &y);
  141. num[count] = fmod(x, y);
  142. goto finish;
  143. case'/': // root
  144. num[count] = sqrt(num[count]);
  145. goto finish;
  146. case'^': // exponent
  147. scanf(" %f", &x);
  148. num[count] = pow(num[count], x);
  149. goto finish;
  150. case'R': // reset
  151. for (int i = count; i >= 0; i--)
  152. num[i] = num[i + 1];
  153. num = (float*)realloc(num, sizeof(float));
  154. if (num == NULL)
  155. {
  156. puts("Error: failed to allocate memory.");
  157. exit(1);
  158. }
  159. count = 0;
  160. printf("Calculate numbers until writing worth and then pressing enter: ");
  161. break;
  162. case'Q': // quit
  163. exit(0);
  164. default:
  165. puts("Error: no such char.");
  166. exit(1);
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement