Achieve a simple function calculator with a transfer table

Because the time to achieve using traditional methods, the code would be too cumbersome. Here, the code calculator is realized by the transfer table. (Multi-file format)

head File

#ifndef _CAL_H_
#define _CAL_H_

#pragma warning(disable:4996)
#include<stdio.h>
#include<Windows.h>

int my_add(int, int);
int my_sub(int, int);
int my_mul(int, int);
int my_div(int, int);

#endif

Corresponding method implemented

#include"cal.h"

int my_add(int x, int y)
{
	return x + y;

}
int my_sub(int x, int y)
{ 
	return x - y;
}
int my_mul(int x, int y)
{
	return x * y;
}
int my_div(int x, int y)
{
	return x / y;
}

The main logic implementation

#include"cal.h"

void menu()
{
	printf("###########################\n");
	printf("##  1.ADD        2.SLB   ##\n");
	printf("##  3.MUL        4.DIV   ##\n");
	printf("##               0.QUIT  ##\n");
	printf("###########################\n");
	printf("Plesae Enter:>");
}

int main()
{
	int(*p[4])(int,int) = { my_add, my_sub, my_mul, my_div };
	int quit = 0;
	while (!quit)
	{
		menu();
		int select = 0;
		scanf("%d", &select);

		if (select == 0)
		{
			quit = 1;
			continue;
		}
		if (select<1 && select>4)
		{
			quit = 1;
			continue;
		}
		int x = 0; int y = 0;
		printf("请输入两个数:");
		scanf("%d %d", &x, &y);
		int z=p[select - 1](x,y);
		printf("结果是:%d\n", z);

	}
	printf("byebye!\n");
	system("pause");
	return 0;
}

A rookie, limited, Xie Bo grateful to the Friends of criticism and be grateful!

Published 14 original articles · won praise 0 · Views 148

Guess you like

Origin blog.csdn.net/qq_41041036/article/details/103680882