What is advanced C language? The interviewer asked you to write a calculator? From passing to full mark

catalogue

1. Pass writing

2. Excellent writing

3. Full mark writing method

First, we print a menu, accept the value entered by the user, and then judge what algorithm the user wants to use to calculate the two numbers. We use a function to achieve this goal.

void menu()
{
	printf("**********************************\n");
	printf("******* 1.add     2. sub    ******\n");
	printf("******* 3.mul     4. div    ******\n");
	printf("******* 0.exit              ******\n");
	printf("**********************************\n");
}

  ok, after printing, let's introduce three different calculator implementation methods.

1. Pass writing method (barely complete the task, the interviewer is not very satisfied)

int Add(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;
}
//The first pass grade writing method
int main()
{
	int input = 0;
	int ret = 0;
	int x = 0;
	int y = 0;
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Please enter 2 operands:>");
				scanf("%d %d", &x, &y);
				ret = Add(x, y);
				printf("ret = %d\n", ret);
			break;
		case 2:
			printf("Please enter 2 operands:>");
			scanf("%d %d", &x, &y);
			ret = Sub(x, y);
			printf("ret = %d\n", ret);
			break;
		case 3:
			printf("Please enter 2 operands:>");
			scanf("%d %d", &x, &y);
			ret = Mul(x, y);
			printf("ret = %d\n", ret);
			break;
		case 4:
			printf("Please enter 2 operands:>");
			scanf("%d %d", &x, &y);
			ret = Div(x, y);
			printf("ret = %d\n", ret);
			break;
		case 0:
			printf("Exit calculator\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
	return 0;
}

Although the first algorithm barely completes the task, the code in switch casez system is too lengthy, not concise and difficult to read. It can only get a pass score. Whether you can get an offer depends solely on the mood of the interviewer.

2.80 point algorithm (offer is appropriate, even sp,ssp)

int Add(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 x = 0;
	int y = 0;
	int ret = 0;

	printf("Please enter 2 operands:>");
	scanf("%d %d", &x, &y);
	ret = pf(x, y);
	printf("ret = %d\n", ret);
}


//The second method
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			Calc(Add);
			break;
		case 2:
			Calc(Sub);
			break;
		case 3:
			Calc(Mul);
			break;
		case 4:
			Calc(Div);
			break;
		case 0:
			printf("Exit calculator\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
	return 0;
}

Here, we use the callback function method to pass the function address to another function as parameters, which greatly improves the readability of the code and makes the code more concise. If we get the offer properly, SP and SSP also have a good chance. But still use switch case, which can't surprise the interviewer like the following method.

3. Full mark writing method (the interviewer calls directly, young man, you can take my seat)

int Add(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 = 0;
	do
	{
		int x = 0;
		int y = 0;
		int ret = 0;
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		int (*pfArr[5])(int, int) = {0, Add, Sub, Mul, Div};                        
		if (input == 0)
		{
			printf("Exit calculator\n");
		}
		else if(input>=1 && input<=4)
		{
			printf("Please enter 2 operands:>");
			scanf("%d %d", &x, &y);
			ret = pfArr[input](x, y);
			printf("%d\n", ret);
		}
		else
		{
			printf("Selection error\n");
		}
	} while (input);
	return 0;
}

The function pointer array is used here. The value entered by the user is used as the array subscript, and the corresponding function is called through the corresponding array element. It is really wonderful and fascinating. The interviewer shouted, "young man, you can sit in my position."

Full code link to this article c language / 2021.12.08 · Wu Changsheng / code - Code cloud - Open Source China (gitee.com)

(the interviewer examples given in this article are purely fictional. In order to more conveniently describe the differences between the three methods, please don't take them seriously)

This is the end of this article. Thank you for your reading. Welcome to praise and comment on each other. I wish you all the best.

Keywords: C Back-end

Added by edontee on Thu, 09 Dec 2021 00:55:53 +0200