Posted By


OdnetninI on 09/08/12

Tagged


Statistics


Viewed 666 times
Favorited by 0 user(s)

Calculator V 2.1


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

New Operation


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("5. Potencia\n");
  55. printf("-----------------------------\n");
  56. }
  57.  
  58. /*
  59.   Main
  60.   Init point of System
  61. */
  62. int main()
  63. {
  64. // Temporal vairables
  65. uint8_t salir = 0;
  66. uint8_t opcion = 0;
  67.  
  68. // Main While
  69. while(salir == 0)
  70. {
  71. // Printf the Options
  72. PrintfOptions();
  73.  
  74. // Ask for the option
  75. printf("Elija su Opci���³n: ");
  76. scanf("%d", &opcion);
  77.  
  78. // Check if answer is correct
  79. if (opcion > 0 && opcion < 6)
  80. AskNumbers();
  81.  
  82. // Make the operation
  83. switch (opcion)
  84. {
  85. case 1:
  86. printf("%5.2f + %5.2f = %5.2f\n", a, b, a + b);
  87. result = a + b;
  88. break;
  89.  
  90. case 2:
  91. printf("%5.2f - %5.2f = %5.2f\n", a, b, a - b);
  92. result = a - b;
  93. break;
  94.  
  95. case 3:
  96. printf("%5.2f * %5.2f = %5.2f\n", a, b, a * b);
  97. result = a * b;
  98. break;
  99.  
  100. case 4:
  101. printf("%5.2f / %5.2f = %5.2f\n", a, b, a / b);
  102. result = a / b;
  103. break;
  104.  
  105. case 5:
  106. result = a;
  107. for (float i = 0; i < b-1; i++)
  108. result *= a;
  109. printf("%5.2f ^ %5.2f = %5.2f\n", a, b, result);
  110. break;
  111.  
  112. default:
  113. printf("Operaci���³n Desconocida\n");
  114. break;
  115. }
  116.  
  117. // Ask for go out
  118. printf("���¿Desea salir? [0:No, 1:Si]");
  119. scanf("%d", &opcion);
  120.  
  121. // Check the answer
  122. if (opcion == 1)
  123. salir = 1;
  124.  
  125. else
  126. {
  127. salir = 0;
  128. printf("���¿Quieres conservar el resultado? [0:No, 1:Resultado es A, 2: Resultado es B]");
  129. scanf("%d", &opcion);
  130. if (opcion == 1) lastresult = 1;
  131. else if (opcion == 2) lastresult = 2;
  132. else lastresult = 0;
  133. }
  134.  
  135. // New Line
  136. printf("\n");
  137. }
  138.  
  139. // Exit of the program
  140. return 0;
  141. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.