intmain(){int input =1;int a =0, b =0;int ret =0;do{menu();//调用菜单显示printf("请输入需要进行的计算模式:");scanf("%d",&input);switch(input)//选择对应操作 不同操作通过不同函数实现{case1:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sum(a, b);break;case2:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sub(a, b);break;case3:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Mul(a, b);break;case4:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Div(a, b);break;case0:printf("已退出\r\n");break;default:printf("输入有误 请重新输入\n");break;}}while(input);return0;}
3.函数方法实现
intSum(int x,int y){printf("%d\n",x+y);}intSub(int x,int y){printf("%d\n", x - y);}intMul(int x,int y){printf("%d\n", x * y);}intDiv(int x,int y){printf("%d\n", x / y);}
总代码如下:
#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>voidmenu(){printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");}intSum(int x,int y){printf("%d\n",x+y);}intSub(int x,int y){printf("%d\n", x - y);}intMul(int x,int y){printf("%d\n", x * y);}intDiv(int x,int y){printf("%d\n", x / y);}//普通法intmain(){int input =1;int a =0, b =0;int ret =0;do{menu();printf("请输入需要进行的计算模式:");scanf("%d",&input);switch(input){case1:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sum(a, b);break;case2:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sub(a, b);break;case3:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Mul(a, b);break;case4:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Div(a, b);break;case0:printf("已退出\n");break;default:printf("输入有误 请重新输入\n");break;}}while(input);return0;}
函数指针数组法
思路:通过把函数方法放在指针数组中,并巧妙利用下标和操作数数字相同来调用函数方法。
#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>voidmenu(){printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");}intSum(int x,int y){return x + y;}intSub(int x,int y){return x - y;}intMul(int x,int y){return x * y;}intDiv(int x,int y){return x / y;}intmain(){int input =1;int a =0, b =0;int ret =0;//函数指针数组法int(*ptr[])(int a,int b)={0,Sum,Sub,Mul,Div};//放入函数指针do{menu();printf("请输入计算模式:");scanf("%d",&input);if(input <=4&& input >=1)//通过if语句来判断输入值是否符合条件{printf("请输入操作数:");scanf("%d %d",&a,&b);int ret=(*ptr[input])(a, b);//调用函数指针中的input方法printf("%d\n",ret);}elseif(input==0){printf("退出计算器\n");}else{printf("输入有误 请重新输入\n");}}while(input);return0;}
回调函数法
思路:在一个函数方法中,定义一个函数指针来调用函数方法。
总代码如下
#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>voidmenu(){printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");}intSum(int x,int y){return x + y;}intSub(int x,int y){return x - y;}intMul(int x,int y){return x * y;}intDiv(int x,int y){return x / y;}voidcalc(int(*pf)(int,int)){int ret =0;int x, y;printf("请输入操作数:");scanf("%d %d",&x,&y);
ret =(*pf)(x, y);//函数指针 *pf(可以接收任何方法)printf("%d\r\n",ret);}intmain(){int input =1;int a =0, b =0;int ret =0;do{menu();printf("请输入计算模式:");scanf("%d",&input);switch(input){case1:calc(Sum);break;case2:calc(Sub);break;case3:calc(Mul);break;case4:calc(Div);break;case0:printf("已退出\r\n");break;default:printf("输入有误 请重新输入!\r\n");break;}}while(input);return0;}