用C语言实现简单的计算器

功能介绍

用C语言实现整形简单的加减乘除操作(通常方法与使用函数指针数组的方法)

代码实现

  • 方法一
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>

int main()
{
	while (1)
	{
		double x = 0, y = 0, ret = 0;//定义运算量以及结果
		char operator;//定义运算符
		printf("******************************\n");
		printf("请输入算式:>");
		scanf("%lf%c%lf", &x, &operator, &y);//输入算式
		switch (operator)//通过switch case语句控制加减乘除运算
		{
		case '+':
			ret = x + y;//加法运算
			break;
		case '-':
			ret = x - y;//减法运算
			break;
		case '*':
			ret = x * y;//乘法运算
			break;
		case '/':
			ret = x / y;//除法运算
			break;
		default:
			printf("输入错误,请重新输入!\n");
		}
		printf("%lf %c %lf = %lf\n", x, operator, y, ret);//输出结果
	}
	system("pause");
	return 0;
}

在VS2017环境下运行,结

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值