使用C语言通过多种方法实现转移表
方法一、普通法
思路:如图实现多种操作,首先创建菜单,需要运行一次再判断条件,所以通过do{}while(); 循环来实现多次。有多种选择,使用switch case选择语句,再在对应case内容中实现相应算法即可。
1.创建菜单
void menu()
{printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");
}
2.通过do{}while()x循环和switch{}case:选择语句进行操作实现
int main()
{int input = 1;int a = 0, b = 0;int ret = 0;do{menu();printf("请输入需要进行的计算模式:");scanf("%d",&input);switch (input){case 1:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sum(a, b);break;case 2:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Sub(a, b);break;case 3:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Mul(a, b);break;case 4:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Div(a, b);break;case 0:printf("已退出\r\n");break;default:printf("输入有误 请重新输入\n");break;}} while (input);return 0;
}
3.函数方法实现
int Sum(int x,int y)
{printf("%d\n",x+y);
}int Sub(int x, int y)
{printf("%d\n", x - y);
}int Mul(int x, int y)
{printf("%d\n", x * y);
}int Div(int x, int y)
{printf("%d\n", x / y);
}
总代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>void menu()
{printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");
}int Sum(int x,int y)
{printf("%d\n",x+y);
}int Sub(int x, int y)
{printf("%d\n", x - y);
}int Mul(int x, int y)
{printf("%d\n", x * y);
}int Div(int x, int y)
{printf("%d\n", x / y);
}
int main()
{int input = 1;int a = 0, b = 0;int ret = 0;do{menu();printf("请输入需要进行的计算模式:");scanf("%d",&input);switch (input){case 1:printf("请输入两个操作数:");scanf("%d %d",&a,&b);Sum(a, b);break;case 2:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Sub(a, b);break;case 3:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Mul(a, b);break;case 4:printf("请输入两个操作数:");scanf("%d %d", &a, &b);Div(a, b);break;case 0:printf("已退出\n");break;default:printf("输入有误 请重新输入\n");break;}} while (input);return 0;
}
函数指针数组法
思路:通过把函数方法放在指针数组中,并巧妙利用下标和操作数数字相同来调用函数方法。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>void menu()
{printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");
}int Sum(int x,int y)
{return x + y;
}int Sub(int x, int y)
{return x - y;
}int Mul(int x, int y)
{return x * y;
}int Div(int x, int y)
{return x / y;
}
int main()
{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){printf("请输入操作数:");scanf("%d %d",&a,&b);int ret=(*ptr[input])(a, b);printf("%d\n",ret);}else if(input==0){printf("退出计算器\n");}else{printf("输入有误 请重新输入\n");}} while (input);return 0;
}
回调函数法
思路:在一个函数方法中,定义一个函数指针来调用函数方法。
总代码如下
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>void menu()
{printf("************************\n");printf("************************\n");printf("****1.Sum 2.Sub******\n");printf("****3.Mul 4.Div******\n");printf("***** 0.退出 ******\n");printf("************************\n");printf("************************\n");
}int Sum(int x,int y)
{return x + y;
}int Sub(int x, int y)
{return x - y;
}int Mul(int x, int y)
{return x * y;
}int Div(int x, int y)
{return x / y;
}void calc(int(*pf)(int, int))
{int ret = 0;int x, y;printf("请输入操作数:");scanf("%d %d",&x,&y);ret = (*pf)(x, y);printf("%d\r\n",ret);
}
int main()
{int input = 1;int a = 0, b = 0;int ret = 0;do{menu();printf("请输入计算模式:");scanf("%d",&input);switch (input){case 1:calc(Sum);break;case 2:calc(Sub);break;case 3:calc(Mul);break;case 4:calc(Div);break;case 0:printf("已退出\r\n");break;default:printf("输入有误 请重新输入!\r\n");break;}} while (input);return 0;
}