6月19日 (text 1)简易计算器

简易计算器的实现(+ - * /)

一、分析思路:
1.根据要求,得到所需的功能为 + - * / 4个功能,无需其他计算
2.设计用户界面:
<1>可以重复输入,给客户提供一个判断是否继续的窗口
<2>在除法时考虑除零错误
<3>在输入符号错误时向客户报错
二、设计流程
1、编写主函数,定义类型 int main()
2、在函数外编写 “ + - * / ”的4种运算函数
4、编写一个操作函数 void calculator() 将用户界面加入
5、在操作函数中将“ + - * / ”的函数调用,并且在 “/ ”进行输入是否正确的判断
6、判断 char op的输入是否有误(只能计算 + - * / )如果输入错误则进行报错
7、在操作函数中将while循环加入
三、程序代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
int AddInt( int a, int b){
		return a + b;
}
int SubInt( int a, int b){
		return a - b;
}
int MulInt( int a, int b){
		return a * b;
}
int DivInt( int a, int b){
		return a / b;
}
void calculator(){
		int a , b;
		int c;
		char op;
		bool tag ;
		while(1){
			tag = true;
			printf("please input data operator data\n");
			scanf("%d %c %d", &a ,&op ,&b);
			if ( '+' == op ){
					c = AddInt(a ,b);
			}
			else if ( '-' == op ){
					c = SubInt(a ,b);
			}
			else if ( '*' == op ){
					c = MulInt(a ,b);
			}
			else if ( '/' == op ){
					if ( 0 == b){
							tag = false;
							printf ( " input data error\n");
					}
					else {
							c = DivInt(a ,b);
					}
			}
			else {
					tag = false;
					printf("input error\n");
			}
			if (tag){
					printf("=>%d\n",c);
			}
			printf(" continue input? Y/N\n");
			char word;
			word = getchar();
			scanf("%c", &word);
			if ( word!='y' && word != 'Y'){
					break;
			}
		}
}
int main(){
		calculator();
		system("pause");
		return 0;
}


		

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值