C language: Use the parameters of the main function to implement an Integer Calculator
/* Use the parameters of the main function to implement an Integer Calculator. The program can accept three parameters. The first parameter "-a" is used to perform addition, and the "-s" option is used to perform subtraction, the "-m" option performs multiplication, and the "-d" option executes division. The next two parameters are operands. */# Include <stdio. h> # include <stdlib. h> int my_calculator (char * p, int num1, int num2) // calculator indicates the calculator {if (p = "-a") return num1 + num2; else if (p = "-s") return num1-num2; else if (p = "-m") return num1 * num2; else if (p = "-d") return num1/num2; else return 0 ;}int main () {char a, B; char * p = & B; int num1 = 0; int num2 = 0; int result = 0; printf ("Enter the computing expression:"); scanf ("% d % c % d", & num1, & a, & num2); // input formula. num1 and num2 are operands. a stores operators. // values p values for parameter pointers, '+ ','-', '*', '/', respectively, corresponding to "-a,-s,-m,-d". If it is another symbol, p is assigned '\ 0' while (1) // while (0) indicates false. As long as it is not 0 in the brackets, the loop is always executed {if (a = '+') {p = "-"; break;} else if (a = '-') {p = "-s"; break;} else if (a = '*') {p = "-m"; break;} else if (a = '/') {p = "-d"; break ;} else * p = '\ 0';} result = my_calculator (p, num1, num2 ); // function call result printf ("% d % c % d = % d \ n", num1, a, num2, result ); // Output system ("pause"); return 0;} respectively ;}
Result: Enter the computing expression: 6/26/2 = 3. Press any key to continue...