加减乘除法的计算器(也是C Primer Plus 的第八章编程练习的第八题)

//进行加减乘除法的程序
#include<stdio.h>
#include<ctype.h>

int menu(void);			//菜单函数
void clean(void);		//清理多余的字符
int get_choice(void);	//获取字符
float add(void);		//加法函数
float subtract(void);	//减法函数
float multiply(void);	//乘法函数
float divide(void);		//除法函数
float get_number(void); //用于加减乘函数里获取数据
float get_number_l(void);//用于除法函数里获取数据
int main(void)
{
	int ch, i;
	float result = 0;
	float data;
	while ((ch = menu()) != 'q')
	{
		int k = 1;
		int i = 1;
		switch (ch)
		{
		case 'a':
			result = add();
			break;
		case 's':
			result = subtract();
			break;
		case 'm':
			result = multiply();
			break;
		case 'd':
			result = divide();
			break;
		default: 
			break;
		}
		if((data = result - (int)result)!=0)  //处理整数与浮点数的情况
			printf("The calculation result is: %.4f\n\n",result);
		else
			printf("The calculation result is: %d\n\n", (int)result);
	}
	printf("\nBye!\n");
	return 0;
}

int menu(void)	//菜单函数
{
	int ch;
	printf("Enter the operation of your choice: \n");
	printf("a. add\t\ts. subtract\nm. multiply\t\td. divide\nq. quit\n"); //显示菜单选项
	ch = get_choice();
	if (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
	{
		printf("Please enter a ture choice: ");
		ch = get_choice();
	}
	return ch;
	
}

int get_choice(void)
{
	char ch;
	scanf_s(" %c", &ch);
	clean();
	return ch;
}

void clean(void)
{
	while (getchar() != '\n')
		continue;
}

float get_number(void)
{
	float ch;
	char i;
	while (scanf_s("%f", &ch) != 1)
	{
		while ((i = getchar()) != '\n')
			putchar(i);
		printf(" is not an number.\n");
		printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
	}
	return ch;
}

float get_number_l(void)
{
	float ch;
	char i;
	while (scanf_s("%f", &ch) != 1 ||ch==0)
	{
		while ((i=getchar()) != '\n')
			putchar(i);
		if (ch == 0)
		{
			printf("Enter a number other than 0: ");
			continue;
		}
		printf(" is not an number.\n");
		printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
	}
	return ch;
}

float add(void)			//加法函数
{
	float ch_1, ch_2;
	float result;
	printf("Enter first number: ");
	ch_1 = get_number();
	printf("Enter second number: ");
	ch_2 = get_number();
	result = ch_1 + ch_2;
	return result;
}

float subtract(void)	//减法函数
{
	float ch_1, ch_2;
	float result;
	printf("Enter first number: ");
	ch_1 = get_number();
	printf("Enter second number: ");
	ch_2 = get_number();
	result = ch_1 - ch_2;
	return result;
}

float multiply(void)	//乘法函数
{
	float ch_1, ch_2;
	float result;
	printf("Enter first number: ");
	ch_1 = get_number();
	printf("Enter second number: ");
	ch_2 = get_number();
	result = ch_1 * ch_2;
	return result;
}

float divide(void)		//除法函数
{
	float ch_1, ch_2;
	float result;
	printf("Enter first number: ");
	ch_1 = get_number_l();
	printf("Enter second number: ");
	ch_2 = get_number_l();
	result = ch_1 / ch_2;
	return result;
}

测试结果:
请添加图片描述
请添加图片描述
请添加图片描述
//初学者分享,如有问题请多多指出,可以进行交流,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值