C primer plus第六版第八章编程第8题

本文介绍了一个使用C语言编写的简易计算器程序,该程序能够处理加、减、乘、除四种基本运算,并通过有效的输入验证确保了操作数和运算符的合法性。用户可以选择继续计算或退出程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
char Computing_method(void); //检查第一个字符是否输入正确
char menu(void); //处理第一个字符
float get_int1(void); //处理第一个数字
float get_int2(void); //处理第二个数字
int main(void)
{
char ch;
float result,act1,act2;

printf("Enter the operation of your choice:\n");
printf("a. add          s. subtract\nm. multiply          d. divide\n");
printf("q. quit\n");

while((ch=menu())!='q')
{
   printf("Enter first number:\n");
switch(ch){
    case 'a': act1=get_int1();
              act2=get_int2();
              result=act1+act2;
        
        printf("%.1f+%.1f=%.1f\n",act1,act2,result);
     
        printf("Enter the operation of your choice:\n");
        printf("a. add          s. subtract\nm. multiply          d. divide\n");
        printf("q. quit\n");
                break;
    case 's': act1=get_int1();
              act2=get_int2();
              result=act1-act2;
                printf("%.1f-%.1f=%.1f\n",act1,act2,result);
        
        printf("Enter the operation of your choice:\n");
        printf("a. add          s. subtract\nm. multiply          d. divide\n");
        printf("q. quit\n");
                break;
    case 'm': act1=get_int1();
              act2=get_int2();
              result=act1*act2;
                printf("%.1f*%.1f=%.1f\n",act1,act2,result);
        
        printf("Enter the operation of your choice:\n");
        printf("a. add          s. subtract\nm. multiply          d. divide\n");
        printf("q. quit\n");
                break;
    case 'd':
     
             act1=get_int1();
             act2=get_int2();
        if(act2==0){      //除数不可以是0
            printf("Ender a numer other than 0:");
           act2=get_int2();
        }
             result=act1/act2;
        printf("%.1f/%.1f=%.1f\n",act1,act2,result);
        
        printf("Enter the operation of your choice:\n");
        printf("a. add          s. subtract\nm. multiply          d. divide\n");
        printf("q. quit\n");
                break;
            default : break;
        }
    while(getchar()!='\n')
        continue;
}
printf("Bye!\n");

return 0;

}

char menu(void){
char ch;

ch=Computing_method();
    
    while(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'&&ch!='q'){
    printf("Please respond with a, s, m, d, or q.\n");
    printf("Enter the operation of your choice:\n");
    printf("a. add          s. subtract\nm. multiply          d. divide\n");
    printf("q. quit\n");
        ch=Computing_method();
    }

return ch;
}

char Computing_method(void){ //检查是否有效输入
int ch;

ch=getchar();
while(getchar()!='\n')
    continue;

return ch;
}

float get_int1(void) //处理第一个数字
{
float act1;
char ch;

while(scanf("%f",&act1)!=1){
    
while((ch=getchar())!='\n')
    putchar(ch);
    printf(" is not an number.\n");
    printf("Please enter a numbr, such as 2.5, -1, or 3:");
    
}
printf("Ender second number:\n");

return act1;

}

float get_int2(void) //处理第二个数字
{
float act2;
char ch;

while(scanf("%f",&act2)!=1){
  
while((ch=getchar())!='\n')
        putchar(ch);
    printf(" is not an number.\n");
    printf("Please enter a numbr, such as 2.5, -1, or 3:");
}

return act2;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值