Posted By


OdnetninI on 09/08/12

Tagged


Statistics


Viewed 517 times
Favorited by 0 user(s)

Calculator V 2.0


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

New version of Calculator


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.