简易计算器的实现(+ - * /)
一、分析思路:
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;
}