Podemos hacer una calculadora simple usando el lenguaje C mediante dos enfoques usando el cambio de mayúsculas y minúsculas y la instrucción if-else .
Aporte:
7+8 8-9 8*7 8/3
Producción:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x
Uso de la caja del interruptor:
Acercarse:
- Haremos todos los siguientes pasos dentro de un bucle interminable para que el programa de calculadora siga funcionando.
- Tome la entrada del operador y luego los operandos.
- Verifique si el usuario desea salir del programa, si es así, luego ciérrelo, para esto, podemos usar un carácter especial y decirle al usuario al respecto como aquí usamos «x».
- Usar operadores de verificación de casos de cambio y hacer operaciones en consecuencia.
C
// C Program to Make a Simple Calculator // Using switch case #include <stdio.h> #include <stdlib.h> int main() { char ch; double a, b; while (1) { printf("Enter an operator (+, -, *, /), if want to exit press x: "); scanf(" %c", &ch); // to exit if (ch == 'x') exit(0); printf("Enter two first and second operand: "); scanf("%lf %lf",&a,&b); // Using switch case we will differentiate // operations based on different operator switch (ch) { // For Addition case '+': printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b); break; // For Subtraction case '-': printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b); break; // For Multiplication case '*': printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b); break; // For Division case '/': printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b); break; // If operator doesn't match any case constant default: printf("Error! please write a valid operator\n"); } } }
Producción:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x
Usando If-Else
Acercarse:
- Haremos todos los siguientes pasos dentro de un bucle interminable para que el programa de calculadora siga funcionando.
- Tome la entrada del operador y luego los operandos.
- Verifique si el usuario desea salir del programa, si es así, luego ciérrelo, para esto, podemos usar un carácter especial y decirle al usuario, como aquí usamos «x».
- Usando sentencias if-else comprobaremos los operadores y haremos las operaciones correspondientes.
Ejemplo:
C
// C Program to Make a Simple Calculator // Using if else statement #include <stdio.h> #include <stdlib.h> int main() { char ch; double a, b; while (1) { printf("Enter an operator (+, -, *, /), if want to exit press x: "); scanf(" %c", &ch); // to exit if (ch == 'x') exit(0); printf("Enter two first and second operand: "); scanf("%lf %lf", &a, &b); // Using if else we will differentiate operations // based on different operator if (ch == '+') { // For Addition printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b); } else if (ch == '-') { // For Subtraction printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b); } else if (ch == '*') { // For Multiplication printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b); } else if (ch == '/') { // For Division printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b); } else { // If operator doesn't match any case constant printf("Error! please write a valid operator\n"); } } }
Producción:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x
Publicación traducida automáticamente
Artículo escrito por pushpamkjha14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA