C language-pointers (3) three implementations of function pointers and calculators

 --------------------------------------------------

-----------------------------------

-------------------------

-----------------

----------

--------

-----

---

Table of contents

function pointer variable

function pointer type

 Array of function pointers

Simulate calculator using C language


function pointer variable

1. Function pointers are used to store function addresses . The purpose is to directly pass addresses Call function.

2.The function name is the function's address. (We can use the following code to demonstrate)

#include<stdio.h>
void test()
{
	printf("hello world!");
}
int main()
{
	printf("%p\n", test);
	printf("%p\n", &test);
	return 0;
}

We foundThe function name is the address of the function

function pointer type

int (*test)(int x,int y)
|      |         |
|      |         |
|      |         |
|      |         |test指向函数的参数类型和个数的交代
|      |函数指针变量名
|test指向函数的返回类型

int(*)(int x,int y)-test指向的指针变量的类型
x和y写和省略都是可以的

 Call the function pointed to by the function pointer(Find the sum of two numbers ):

#include<stdio.h>
int QiuHe(int x, int y)
{
	return x + y;
}
int main()
{
	int a = 0, b = 0;
	int(*test)(int, int) = QiuHe;
	printf("请输入两个你喜欢的整数:\n");
	scanf_s("%d%d", &a, &b);
	printf("求和结果是:%d\n", (*test)(a, b));
	//因为函数名就是函数地址,所以可以这么写
	//printf("求和结果是:%d\n", test(a, b));
	return 0;
}

 Array of function pointers

1. Definition: We mentioned before that a pointer array is a storage space for a pointer type variable, and our function pointer array stores the address of the function in an array< /span>.

int (*test[])()

 Because[ ] has a higher priority than *,

Sotest is first combined with [ ] indicating that test is an array,

When test[ ] is combined with int(*)() type, it becomes function pointer array< /span>.

 Usage scenarios of function pointer array:

Simulate calculator using C language

1. The following is our traditional function call simulation calculator algorithm. There aremany repetitive codes written and time The complexity is high, and the codeis not efficient.

#include<stdio.h>
#include <windows.h>
int JiaFa(int x, int y)
{
	return x + y;
}
int JianFa(int x, int y)
{
	return x - y;
}
int ChenFa(int x, int y)
{
	return x * y;
}
int ChuFa(int x, int y)
{
	return x / y;
}
void menu()
{
	system("color F4");
	printf("***************C语言计算器***************\n");
	printf("***********1.加法########2.减法**********\n");
	printf("***********3.乘法########4.除法**********\n");
	printf("***********####0.退出计算器####**********\n");
	printf("*****************************************\n");
}
int main()
{
	int a = 0, b = 0, input = 0, sum = 0;
	do 
	{
		menu();
		printf("请输入你要进行的计算:\n");
		scanf_s("%d",&input);
		switch (input)
		{
		case 0:
			printf("您已退出计算器!\n");
			break;
		case 1:
			printf("请选择你要进行的操作数:\n");
			scanf_s("%d%d", &a, &b);
			sum = JiaFa(a, b);
			printf("最后的结果为%d:\n", sum);
			break;
		case 2:
			printf("请选择你要进行的操作数:\n");
			scanf_s("%d%d", &a, &b);
			sum = JianFa(a, b);
			printf("最后的结果为%d:\n", sum);
			break;
		case 3:
			printf("请选择你要进行的操作数:\n");
			scanf_s("%d%d", &a, &b);
			sum = ChenFa(a, b);
			printf("最后的结果为%d:\n", sum);
			break;
		case 4:
			printf("请选择你要进行的操作数:\n");
			scanf_s("%d%d", &a, &b);
			sum = ChuFa(a, b);
			printf("最后的结果为%d:\n", sum);
			break;
		default:
			printf("输入错误,重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

Implementation of function pointer array:

This greatly improves the efficiency of program operation

#include<stdio.h>
#include <windows.h>
int JiaFa(int x, int y)
{
	return x + y;
}
int JianFa(int x, int y)
{
	return x - y;
}
int ChenFa(int x, int y)
{
	return x * y;
}
int ChuFa(int x, int y)
{
	return x / y;
}
void menu()
{
	system("color F4");
	printf("***************C语言计算器***************\n");
	printf("***********1.加法########2.减法**********\n");
	printf("***********3.乘法########4.除法**********\n");
	printf("***********####0.退出计算器####**********\n");
	printf("*****************************************\n");
}
int main()
{
	int a = 0, b = 0, input = 0, sum = 0;
	int(*p[5])(int, int) = { NULL,JiaFa,JianFa,ChenFa,ChuFa };
	//设置成五个元素的数组    0     1      2      3     4
	do
	{
		menu();
		printf("请输入你要进行的计算:\n");
		scanf_s("%d", &input);
		if (1 <= input && input <= 4)
		{
			printf("请输入两个操作数:\n");
			scanf_s("%d%d", &a, &b);
			sum = (*p[input])(a, b);
			printf("最后的结果是:%d\n", sum);
		}
		else if (input == 0)
		{
			printf("您已退出计算机!\n");
		}
		else
		{
			printf("输入错误,重新输入!\n");
		}
	} while (input);
	return 0;
}

 You can also use the function pointer method to function-encapsulate the contents of the switch, and then use the function pointer to call the calculation function.

#include<stdio.h>
#include <windows.h>
int JiaFa(int x, int y)
{
	return x + y;
}
int JianFa(int x, int y)
{
	return x - y;
}
int ChenFa(int x, int y)
{
	return x * y;
}
int ChuFa(int x, int y)
{
	return x / y;
}
void menu()
{
	system("color F4");
	printf("***************C语言计算器***************\n");
	printf("***********1.加法########2.减法**********\n");
	printf("***********3.乘法########4.除法**********\n");
	printf("***********####0.退出计算器####**********\n");
	printf("*****************************************\n");
}
void ZhuanYi(int (*p)(int,int))
{
	int a = 0, b = 0, sum = 0;
	printf("请输入两个操作数:\n");
	scanf_s("%d%d", &a, &b);
	sum = p(a, b);
	printf("最后的结果是:%d\n", sum);
}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请输入你要进行的计算:\n");
		scanf_s("%d", &input);
		switch (input)
		{
		case 0:
			printf("您已退出计算器!\n");
			break;
		case 1:
			ZhuanYi(JiaFa);
			break;
		case 2:
			ZhuanYi(JianFa);
			break;
		case 3:
			ZhuanYi(ChenFa);
			break;
		case 4:
			ZhuanYi(ChuFa);
			break;
		default:
			printf("输入错误,重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

----------------------------------------

-----------------------

-------------

---------

------

Guess you like

Origin blog.csdn.net/2301_79201049/article/details/134466846