[C Language] analog Calculator
# Include <stdio. h> # include <stdlib. h> int _ add (int a, int B) {return a + B;} int _ sub (int a, int B) {return a-B ;} int _ mul (int a, int B) {return a * B;} int _ div (int a, int B) {if (B = 0) {printf ("except digit 0 \ n");} return a/B;} void menu () {printf ("************************* \ n "); printf ("***** 1.add 2.sub ***** \ n"); printf ("***** 3.mul 4.div ***** \ n "); printf ("********* 0. exit ********* \ n "); printf ("************************** \ n");} int main () {int input = 1; int num1 = 0; int num2 = 0; int ret = 0; while (input) {menu (); printf ("SELECT> :"); scanf ("% d", & input); printf ("Please input two integer operands>:"); scanf ("% d", & num1, & num2 ); switch (input) {case 1: ret = _ add (num1, num2); break; case 2: ret = _ sub (num1, num2); break; case 3: ret = _ mul (num1, num2); break; case 4: ret = _ div (num1, num2); break; case 0: exit (EXIT_FAILURE); break; default: printf ("incorrect selection. Please reselect it. \ n "); break;} printf (" ret = % d \ n ", ret);} return 0 ;}in addition, you can also use the enumeration structure (enum) to implement: # include <stdio. h> # include <stdlib. h> enum OP {EXIT, ADD, SUB, MUL, DIV}; int _ add (int a, int B) {return a + B;} int _ sub (int, int B) {return a-B;} int _ mul (int a, int B) {return a * B;} int _ div (int a, int B) {if (B = 0) {printf ("except digit 0 \ n") ;}return a/B;} void menu () {printf ("************************* \ n "); printf ("***** 1.add 2.sub ***** \ n"); printf ("***** 3.mul 4.div ***** \ n "); printf ("********* 0. exit ********* \ n "); printf ("************************** \ n ");} int (* pfun [5]) (int, int) = {0, _ add, _ sub, _ mul, _ div}; int main () {int input = 1; int num1 = 0; int num2 = 0; int ret = 0; while (input) {menu (); printf ("SELECT> :"); scanf ("% d", & input); printf ("Please input two integer operands>:"); scanf ("% d", & num1, & num2 ); if (input> = 1 & input <= 4) {ret = pfun [input] (num1, num2);} else if (input = 0) {exit (EXIT_FAILURE);} else {printf ("select error \ n");} printf ("ret = % d \ n", ret);} return 0 ;}