bailling_ 2022-05-17 15:37 采纳率: 100%
浏览 41
已结题

简单计算器的运行 在操作完一次后可以继续再次运行/退出

问题遇到的现象和发生背景

运行程序出现错误 但是不知道该如何解决

问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int x,y;
double a,b;
void plus(double a,double b)
{
    printf("%.5f + %.5f=%.5f\n",a,b,a+b);
}
void minus(double a,double b)
{
    printf("%.5f - %.5f=%.5f\n",a,b,a-b);
}
void multiply(double a,double b)
{
    printf("%.5f * %.5f=%.5f\n",a,b,a*b);
}
void divide(double a,double b)
{
    printf("%.5f / %.5f=%.5f\n",a,b,a/b);
}
void Interface()
{
    printf("--------------------------------\n");
    printf("|          实用计算器          |\n");
    printf("|            1-加法            |\n");
    printf("|            2-减法            |\n");
    printf("|            3-乘法            |\n");
    printf("|            4-除法            |\n");
    printf("|            0-退出            |\n");
    printf("--------------------------------\n");
    printf("    你想要进行哪一种计算(0-4) \n");
}
void input()
{
    printf("请输入第一个数字:");
    scanf("%lf\n",&a);
    printf("请输入第二个数字:");
    scanf("%lf\n",&b);
}
int main(void)
{
    int c=0;
    Interface();
    do
    {

        do
        {
            scanf("%d",&x);
        }
        while(x<0||x>4);
        if(x==0)
            break;
        else if(x==1)
            {
                input();
                plus(a,b);
            }
        else if(x==2)
            {
                input(a,b);
                minus(a,b);
            }
        else if(x==3)
            {
                input(a,b);
                multiply(a,b);
            }
        else
            {
                input(a,b);
                divide(a,b);
            }
        printf("是否继续计算?y 继续 n 退出\n");
        scanf("%d",&y);
        if(y=='y')
            c=1;
        else if(y=='n')
            break;
    }
    while(c);
    return 0;

}


运行结果及报错内容

死循环 请输入第一个数字处会连续输入两次

我想要达到的结果

解决代码问题 正确运行

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-05-17 16:20
    关注

    修改处见注释,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    char x, y;       //int x, y;
    double a, b;
    void plus(double a, double b)
    {
        printf("%.5f + %.5f=%.5f\n", a, b, a + b);
    }
    void minus(double a, double b)
    {
        printf("%.5f - %.5f=%.5f\n", a, b, a - b);
    }
    void multiply(double a, double b)
    {
        printf("%.5f * %.5f=%.5f\n", a, b, a * b);
    }
    void divide(double a, double b)
    {
        if (b != 0)                  //修改
            printf("%.5f / %.5f=%.5f\n", a, b, a / b);
        else
            printf("intput error\n");
    }
    void Interface()
    {
        printf("--------------------------------\n");
        printf("|          实用计算器          |\n");
        printf("|            1-加法            |\n");
        printf("|            2-减法            |\n");
        printf("|            3-乘法            |\n");
        printf("|            4-除法            |\n");
        printf("|            0-退出            |\n");
        printf("--------------------------------\n");
        printf("    你想要进行哪一种计算(0-4) \n");
    }
    void input()
    {
        printf("请输入第一个数字:");
        scanf("%lf", &a);         //scanf("%lf\n", &a);
        printf("请输入第二个数字:");
        scanf("%lf", &b);         //scanf("%lf\n", &b);
    }
    int main(void)
    {
        int c = 0;
        //Interface(); 
        do
        {
            Interface(); //修改
            do
            {
              scanf("%d", &x);
            } while (x < 0 || x>4);
            if (x == 0)
                break;
            else if (x == 1)
            {
                input();
                plus(a, b);
            }
            else if (x == 2)
            {
                input();    //input(a, b);
                minus(a, b);
            }
            else if (x == 3)
            {
                input(); //input(a, b);
                multiply(a, b);
            }
            else
            {
                input(); //input(a, b);
                divide(a, b);
            }
            printf("是否继续计算?y 继续 n 退出\n");
            scanf(" %c", &y);    //scanf("%d", &y);
            if (y == 'y')
                c = 1;
            else if (y == 'n')
                break;
        } while (c);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月17日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。