C语言练习 | 实现四则运算和单位换算的简易计算器

作为C语言初学者,尝试完成了一个可以实现四则运算和单位换算的简易计算器,请多指教。

一、需求分析

1、四则运算:
(1) 能识别用户输入的运算(加+、减、乘*、除/)表达式并计算,如:用户输入189+219,按回车确定后可计算该加法运算的结果并打印输出,其它运算以此类推;
(2)能检测表达式中的错误或不符合运算法则的情况并提示。
2、单位转换:
(1)能实现长度单位换算:英寸、英尺、毫米、厘米、米;
(2)能实现质量单位换算:磅、盎司、克、千克、斤、吨;
(3)能实现面积单位换算:公顷、亩、平方米、平方英尺;

二、分析

四则运算功能采用 switch 语句判断运算符;
单位转换功能使用全局变量存储各单位数值,采用switch语句判断单位表示;
四则运算和单位换算用两个函数实现,其中不同类型的单位换算也用函数实现;
用while语句实现循环运行。

三、程序实现

笔者采用VS2008编写C语言程序,代码如下:

/*
	功能:实现四则运算和单位换算的简易计算器
		  单位换算包括长度单位、质量单位和面积单位
	时间:2020/07/12
	作者:抬手摘星辰
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  
#include <conio.h>
#define FT_TO_M ((float)0.3048)
#define FT_TO_IN (12)
#define IN_TO_CM (2.54)
#define LB_TO_OZ (16)
#define LB_TO_G (453.5924)
#define OZ_TO_G (28.3595)

// 四则运算
void FourArithmetic(float data1, float data2, char op)
{
	printf("* 结果为:\n");
	switch(op)
	{
		case'+':
			printf("%.2f + %.2f = %.2f\n",data1,data2,data1+data2);
			break;
		case'-':
			printf("%.2f - %.2f = %.2f\n",data1,data2,data1-data2);
			break;
		case'*':
			printf("%.2f * %.2f = %.2f\n",data1,data2,data1*data2);
			break;
		case'/':
			if(0==data2)
				printf("Division by zero!\n");
			else
				printf("%.2f / %.2f = %.2f\n",data1,data2,data1/data2);
			break;
		case'%':
			printf("%.2f% % %.2f = %.2f\n",data1,data2,(int)data1%(int)data2);
			break;
		default:
			printf("* 无效的运算符号,请重新输入!\n");
	}
}

//长度单位换算
void LengthUnit(float data1, char op1[5], char op2[5])
{
	int i = 0;
	float data3 = 0.0;
	
	//长度单位换算
	if (strcmp(op1, "in") == 0) i = 1;
	else if (strcmp(op1, "ft") == 0) i = 2;
	else if (strcmp(op1, "mm") == 0) i = 3;
	else if (strcmp(op1, "cm") == 0) i = 4;
	else if (strcmp(op1, "m") == 0) i = 5;
	else printf("* 请检查单位字母输入是否正确!\n");

	switch(i)
	{
		case 1:  //in
			if(strcmp(op2, "ft") == 0) data3 = data1/FT_TO_IN;
			else if(strcmp(op2, "mm") == 0) data3 = data1*(IN_TO_CM*10);
			else if(strcmp(op2, "cm") == 0) data3 = data1*IN_TO_CM;
			else if(strcmp(op2, "m") == 0) data3 = data1*(IN_TO_CM*0.01);
			else data3 = data1;
			break;
		case 2:  //ft
			if(strcmp(op2, "ft") == 0) data3 = data1;
			else if(strcmp(op2, "mm") == 0) data3 = data1*(FT_TO_M*1000);
			else if(strcmp(op2, "cm") == 0) data3 = data1*(FT_TO_M*100);
			else if(strcmp(op2, "m") == 0) data3 = data1*FT_TO_M;
			else data3 = data1*FT_TO_IN;
			break;
		case 3:  //mm
			if (strcmp(op2, "in") == 0) data3 = data1/IN_TO_CM*10;
			else if(strcmp(op2, "ft") == 0) data3 = data1/FT_TO_M*1000;
			else if(strcmp(op2, "mm") == 0) data3 = data1;
			else if(strcmp(op2, "cm") == 0) data3 = data1/10;
			else data3 = data1/1000;
			break;
		case 4:  //cm
			if (strcmp(op2, "in") == 0) data3 = data1/IN_TO_CM;
			else if(strcmp(op2, "ft") == 0) data3 = data1/FT_TO_M *100;
			else if(strcmp(op2, "mm") == 0) data3 = data1*10;
			else if(strcmp(op2, "cm") == 0) data3 = data1;
			else data3 = data1/100;
			break;
		case 5:  //m
			if (strcmp(op2, "in") == 0) data3 = data1/IN_TO_CM*100;
			else if(strcmp(op2, "ft") == 0) data3 = data1/FT_TO_M;
			else if(strcmp(op2, "mm") == 0) data3 = data1*1000;
			else if(strcmp(op2, "cm") == 0) data3 = data1*100;
			else data3 = data1;
			break;
		default:
			printf("* 无法换算,请重新输入!\n");
	}
	
	printf("%.3f %s = %.3f %s\n",data1,op1,data3, op2);

}


// 质量单位换算
void WeightUnit(float data1, char op1[5], char op2[5])
{
	int i = 0;
	float data3 = 0.0;

	if (strcmp(op1, "lb") == 0) i = 1;
	else if (strcmp(op1, "oz") == 0) i = 2;
	else if (strcmp(op1, "g") == 0) i = 3;
	else if (strcmp(op1, "kg") == 0) i = 4;
	else if (strcmp(op1, "Jin") == 0) i = 5;
	else if (strcmp(op1, "ton") == 0) i = 6;
	else printf("* 请检查单位字母输入是否正确!\n");

	switch(i)
	{
	case 1: //lb
		if(strcmp(op2, "oz") == 0) data3 = data1 * LB_TO_OZ;
		else if(strcmp(op2, "g") == 0) data3 = data1*LB_TO_G;
		else if(strcmp(op2, "kg") == 0) data3 = data1*LB_TO_G / 1000;
		else if(strcmp(op2, "Jin") == 0) data3 = data1*0.9071;
		else if(strcmp(op2, "ton") == 0) data3 = data1*LB_TO_G*1e-6;
		else data3 = data1;
		break;
	case 2: //oz
		if(strcmp(op2, "lb") == 0) data3 = data1 / LB_TO_OZ;
		else if(strcmp(op2, "g") == 0) data3 = data1 * OZ_TO_G;
		else if(strcmp(op2, "kg") == 0) data3 = data1*OZ_TO_G / 1e3;
		else if(strcmp(op2, "Jin") == 0) data3 = data1 * 0.056699;
		else if(strcmp(op2, "ton") == 0) data3 = data1*OZ_TO_G*1e-6;
		else data3 = data1;
		break;
	case 3://g
		if(strcmp(op2, "lb") == 0) data3 = data1/LB_TO_G;
		else if(strcmp(op2, "oz") == 0) data3 = data1 / OZ_TO_G;
		else if(strcmp(op2, "kg") == 0) data3 = data1/1000;
		else if(strcmp(op2, "Jin") == 0) data3 = data1 / 500;
		else if(strcmp(op2, "ton") == 0) data3 = data1*1e-6;
		else data3 = data1;
		break;
	case 4: //kg
		if(strcmp(op2, "lb") == 0) data3 = data1/LB_TO_G * 1000;
		else if(strcmp(op2, "oz") == 0) data3 = data1 / OZ_TO_G * 1000;
		else if(strcmp(op2, "g") == 0) data3 = data1*1000;
		else if(strcmp(op2, "Jin") == 0) data3 = data1 * 2;
		else if(strcmp(op2, "ton") == 0) data3 = data1*1e-3;
		else data3 = data1;
		break;
	case 5: //Jin
		if(strcmp(op2, "lb") == 0) data3 = data1 * 1.10231;
		else if(strcmp(op2, "oz") == 0) data3 = data1 * 17.63698;
		else if(strcmp(op2, "g") == 0) data3 = data1* 500;
		else if(strcmp(op2, "kg") == 0) data3 = data1 / 2;
		else if(strcmp(op2, "ton") == 0) data3 = data1*5*1e-4;
		else data3 = data1;
		break;
	case 6: //ton
		if(strcmp(op2, "lb") == 0) data3 = data1 * 2204.6226;
		else if(strcmp(op2, "oz") == 0) data3 = data1 * 35273.9619;
		else if(strcmp(op2, "g") == 0) data3 = data1 * 1e6;
		else if(strcmp(op2, "kg") == 0) data3 = data1 * 1e3;
		else if(strcmp(op2, "Jin") == 0) data3 = data1 * 2000;
		else data3 = data1;
		break;
	default:
		printf("* 无法换算,请重新输入!\n");
	}

	printf("%.3f %s = %.3f %s\n",data1,op1,data3, op2);

}

// 面积单位换算
void AreaUnit(float data1, char op1[5], char op2[5])
{
	int i = 0;
	float data3 = 0.0;

	if (strcmp(op1, "hm2") == 0) i = 1;
	else if (strcmp(op1, "Mu") == 0) i = 2;
	else if (strcmp(op1, "m2") == 0) i = 3;
	else if (strcmp(op1, "ft2") == 0) i = 4;
	else printf("* 请检查单位字母输入是否正确!\n");

	switch(i)
	{
	case 1: //hm2
		if(strcmp(op2, "Mu") == 0) data3 = data1 * 15;
		else if(strcmp(op2, "m2") == 0) data3 = data1 * 10000;
		else if(strcmp(op2, "ft2") == 0) data3 = data1 * 107639.1042;
		else data3 = data1;
		break;
	case 2: //Mu
		if(strcmp(op2, "hm2") == 0) data3 = data1 / 15;
		else if(strcmp(op2, "m2") == 0) data3 = data1 / 15 * 10000;
		else if(strcmp(op2, "ft2") == 0) data3 = data1 * 7175.9403;
		else data3 = data1;
		break;
	case 3: //m2
		if(strcmp(op2, "hm2") == 0) data3 = data1 / 1000;
		else if(strcmp(op2, "Mu") == 0) data3 = data1 * 0.0015 ;
		else if(strcmp(op2, "ft2") == 0) data3 = data1 * 10.76391;
		else data3 = data1;
		break;
	case 4: //ft2
		if(strcmp(op2, "hm2") == 0) data3 = data1 * 9.2903e-6;
		else if(strcmp(op2, "Mu") == 0) data3 = data1 * 0.0001394;
		else if(strcmp(op2, "m2") == 0) data3 = data1 * 0.092903;
		else data3 = data1;
		break;
	default:
		printf("* 无法换算,请重新输入!\n");
	}

	printf("%.3f %s = %.3f %s\n",data1,op1,data3, op2);

}

// 单位换算
void UnitConversion(float data1, char op1[5], char op2[5], int type)
{
	if (type == 1)
		LengthUnit(data1, op1, op2);
	else if (type == 2)
		WeightUnit(data1, op1, op2);
	else
		AreaUnit(data1, op1, op2);
}

void main()
{
	printf("**********  简易计算器  **********\n\n");
	printf("注意:按ESC退出,按任意键继续!\n");
	printf("单位用如下字母表示:\n");
	printf("*********************************\n");
	printf("* 长度单位换算\n* (英寸(in)、英尺(ft)、毫米(mm)、厘米(cm)、米(m));\n");
	printf("* 质量单位换算\n* (磅(lb)、盎司(oz)、克(g)、千克(kg)、斤(Jin)、吨(ton));\n");
	printf("* 面积单位换算\n* (公顷(hm2)、亩(Mu)、平方米(m2)、平方英尺(ft2));\n");
	printf("*********************************\n");
	float data1,data2;
	int a,type;
	char op,key = '\0';
	char op1[5], op2[5];
	
	printf("* 请选择操作:\n");

	while((key = _getch()) != 0x1b) // 0x1b: ESC 
	{
		printf("1.四则运算;  2.单位换算\n");
		scanf_s("%d",&a);

		if(a == 1)
		{
			printf("* 请输入表达式(如:1+2):\n");
			scanf("%f%c%f",&data1,&op,&data2);

			FourArithmetic(data1, data2, op); // 四则运算
		}

		else if(a == 2)
		{
			printf("* 请输入需要换算的单位(如:100m):\n");
			scanf("%f%s",&data1,&op1);
			printf("* 请输入换算后的单位(如:m):\n");
			scanf("%s",&op2);

			if (strcmp(op1, "ft") == 0 || strcmp(op1, "in") == 0 || strcmp(op1, "mm") == 0 \
				|| strcmp(op1, "cm") == 0|| strcmp(op1, "m") == 0)
				type = 1;
			else if (strcmp(op1, "lb") == 0 || strcmp(op1, "oz") == 0 || strcmp(op1, "g") == 0 \
				|| strcmp(op1, "kg") == 0 || strcmp(op1, "Jin") == 0 || strcmp(op1, "ton") == 0 )
				type = 2;
			else if (strcmp(op1, "hm2") == 0 || strcmp(op1, "Mu") == 0 || strcmp(op1, "m2") == 0 \
				|| strcmp(op1, "ft2") == 0 )
				type = 3;
			else printf("* 请检查单位字母输入是否正确!\n");

			UnitConversion(data1, op1, op2, type); //单位换算

		}

		else
			printf("* 暂不支持该选项,请重新选择!\n");
		
		printf("\n******************************\n");
		printf("* 请再次选择操作(ESC退出):\n");
	}
	
	system("pause");
}

运行结果:
在这里插入图片描述
在这里插入图片描述

四、小结

上述简易计算器比较简单,主要用了函数、while语句、if语句、switch语句、对输入字符串的判断等知识点,但是代码较多,或许有更简单的实现方式,笔者还在学习之中,不妥之处,请多指教。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值