Posted By


OdnetninI on 09/08/12

Tagged


Statistics


Viewed 470 times
Favorited by 0 user(s)

Calculator V 2.5


/ Published in: C
Save to your folder(s)

New Fuctions


Copy this code and paste it in your HTML
  1. // Includes of the System
  2. #include <stdio.h>
  3. #include <inttypes.h>
  4. #include <math.h>
  5.  
  6. // Float variables of numbers
  7. float a = 0;
  8. float b = 0;
  9.  
  10. // Float variable of result
  11. float result = 0;
  12.  
  13. // Check Variable
  14. uint8_t lastresult = 0;
  15.  
  16. // Define PI
  17. #define PI 3.14159265
  18.  
  19. /*
  20.   Fuction AskNumbers
  21.   Ask for the numbers
  22. */
  23. void AskNumbers ()
  24. {
  25. if (lastresult == 0 )
  26. {
  27. printf("Elige el primer Número: ");
  28. scanf("%f", &a);
  29. printf("Elige el segundo Número: ");
  30. scanf("%f", &b);
  31. }
  32. else if (lastresult == 1)
  33. {
  34. a = result;
  35. printf("Elige el segundo Número (El Primero es el Resultado anterior): ");
  36. scanf("%f", &b);
  37. }
  38. else if (lastresult == 2)
  39. {
  40. b = result;
  41. printf("Elige el Primer Número (El Segundo es el Resultado anterior): ");
  42. scanf("%f", &a);
  43. }
  44. }
  45.  
  46. /*
  47.   Fuction PrintfOptions
  48.   Printf the Options
  49. */
  50. void PrintfOptions ()
  51. {
  52. printf("Basic Calculador by OdnetninI\n");
  53. printf("-----------------------------\n");
  54. printf("1. Suma\n");
  55. printf("2. Resta\n");
  56. printf("3. Multiplicación\n");
  57. printf("4. División\n");
  58. printf("5. Potencia\n");
  59. printf("6. Coseno\n");
  60. printf("7. Seno\n");
  61. printf("8. Tangente\n");
  62. printf("-----------------------------\n");
  63. }
  64.  
  65. /*
  66.   Main
  67.   Init point of System
  68. */
  69. int main()
  70. {
  71. // Temporal vairables
  72. uint8_t salir = 0;
  73. uint8_t opcion = 0;
  74.  
  75. // Main While
  76. while(salir == 0)
  77. {
  78. // Printf the Options
  79. PrintfOptions();
  80.  
  81. // Ask for the option
  82. printf("Elija su Opción: ");
  83. scanf("%d", &opcion);
  84.  
  85. // Check if answer is correct
  86. if (opcion > 0 && opcion < 6)
  87. AskNumbers();
  88.  
  89. else if (opcion >= 6 && opcion < 9)
  90. {
  91. if (lastresult == 0 )
  92. {
  93. printf("Elige el Número: ");
  94. scanf("%f", &a);
  95. }
  96. else
  97. {
  98. a = result;
  99. }
  100. }
  101.  
  102. // Make the operation
  103. switch (opcion)
  104. {
  105. case 1:
  106. printf("%5.2f + %5.2f = %5.2f\n", a, b, a + b);
  107. result = a + b;
  108. break;
  109.  
  110. case 2:
  111. printf("%5.2f - %5.2f = %5.2f\n", a, b, a - b);
  112. result = a - b;
  113. break;
  114.  
  115. case 3:
  116. printf("%5.2f * %5.2f = %5.2f\n", a, b, a * b);
  117. result = a * b;
  118. break;
  119.  
  120. case 4:
  121. printf("%5.2f / %5.2f = %5.2f\n", a, b, a / b);
  122. result = a / b;
  123. break;
  124.  
  125. case 5:
  126. result = a;
  127. for (float i = 0; i < b-1; i++)
  128. result *= a;
  129. printf("%5.2f ^ %5.2f = %5.2f\n", a, b, result);
  130. break;
  131.  
  132. case 6:
  133. result = cos (a*PI/180);
  134. printf("cos(%5.2f) = %5.2f", a, result);
  135. break;
  136.  
  137. case 7:
  138. result = sin (a*PI/180);
  139. printf("sin(%5.2f) = %5.2f", a, result);
  140. break;
  141.  
  142. case 8:
  143. result = tan (a*PI/180);
  144. printf("tan(%5.2f) = %5.2f", a, result);
  145. break;
  146.  
  147. default:
  148. printf("Operación Desconocida\n");
  149. break;
  150. }
  151.  
  152. // Ask for go out
  153. printf("¿Desea salir? [0:No, 1:Si]");
  154. scanf("%d", &opcion);
  155.  
  156. // Check the answer
  157. if (opcion == 1)
  158. salir = 1;
  159.  
  160. else
  161. {
  162. salir = 0;
  163. printf("¿Quieres conservar el resultado? [0:No, 1:Resultado es A, 2: Resultado es B]");
  164. scanf("%d", &opcion);
  165. if (opcion == 1) lastresult = 1;
  166. else if (opcion == 2) lastresult = 2;
  167. else lastresult = 0;
  168. }
  169.  
  170. // New Line
  171. printf("\n");
  172. }
  173.  
  174. // Exit of the program
  175. return 0;
  176. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.