简易计算器的实现

小项目1 ---C 语言做一个简单的计算器


bk_3d33e46410a73b90105787f79ffae2a7_l6p323.jpg

1,项目说明:

实现一个简易的仅支持两个操作数运算的计算器,不涉及词法分析与语法树,内容很简单,适合 C 语言入门(刚掌握语法程度)的进行练手。

2,项目介绍

能执行加、减、乘、除操作。本程序涉及的所有数学知识都很简单,但输入过程会增加复杂性。因为我们需要检查输入,确保用户没有要求计算机完成不可能的任务。还必须允许用户一次输入一个计算式,例如: 32.4 + 32 或者 9 * 3.2

2.1,项目流程:
1.获取用户输入的计算表达式。
2.检查输入的表达式格式,确保表达式对应的实际操作可以执行。
3.执行计算。
4.返回计算结果并在终端打印。
2.2,项目效果图:

![S6Q]OY{HLV``7YHC41(CZMK.jpg](http://upload-images.jianshu.io/upload_images/2619158-238b2ea80960b82f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3,项目实现

3.1获取输入
     double number1=0.0; // 定义第一个操作数 
     double number2=0.0; // 定义第二个操作数 
     char operation=0; // operation 必须是 '+' '-' '*' '/' 或 '%' 
     printf("\nEnter the calculation\n"); 
     scanf("%lf%c%lf",&number1,&operation,&number2);```
#####     3.2输入检查
     当输入的操作为 / 或者 % 时,第二个操作数不能为 0 。如果为 0 则操作无效。
#####     3.3循环输入,用户选择y/n是够继续
for(;;){
    switch(){
  ……

}
char answer = getchar();//从键盘中输入一个字符
if(answer == 'y' || answer == 'Y'){
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
}
if(answer == 'n' || answer == 'N'){
break; /* Go back to the beginning */
}
}

#### 4,项目源码

/*Exercise 3.4 A calculator that allows multiple calculations */

include <stdio.h>

int main()
{
double number1 = 0.0; /* First operand value a decimal number /
double number2 = 0.0; /
Second operand value a decimal number /
char operation = 0;
char answer ;/
Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
for(;;){

switch(operation)
{
case '+': // No checks necessary for add
printf("= %lf\n", number1 + number2);
break;

case '-':                     // No checks necessary for subtract
  printf("= %lf\n", number1 - number2);
  break;

case '*':                     // No checks necessary for multiply
  printf("= %lf\n", number1 * number2);
  break;                              

case '/':
  if(number2 == 0)           // Check second operand for zero 
    printf("\n\n\aDivision by zero error!\n");
  else
    printf("= %lf\n", number1 / number2);
  break;

case '%':                    // Check second operand for zero
  if((long)number2 == 0) 
     printf("\n\n\aDivision by zero error!\n");
  else
    printf("= %ld\n", (long)number1 % (long)number2);
  break;

default:                     // Operation is invalid if we get to here
  printf("\n\n\aIllegal operation!\n");
  break;

}

/* The following statements added to prompt for continuing */

printf("\n Do you want to do another calculation? (y or n): ");
scanf(" %c", &answer);

if(answer == 'y' || answer == 'Y'){
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2); /* Go back to the beginning /
}
if(answer == 'n' || answer == 'N'){
break; /
Go back to the beginning */
}
}
return 0;
}

#### 5,项目提升
    能支持任意多个操作数的运算,引入运算符优先关系机制,届时更新

######[友情链接](https://github.com/apress/beg-c-5th-edition)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,500评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,253评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,839评论 18 399
  • 这些都表明,SONY 将在 E3 大会上前面展开 PlayStation VR 的营销攻势,这意味着 PlaySt...
    04b9cd76cdec阅读 380评论 0 0
  • 一杯清茗散掉大雾,指尖残留的温度。 十八岁那年以为未来还很远,走进大学才发现都是转眼。 时间像是突然加快了脚步,我...
    晓茗阅读 236评论 0 0