Aqua.... 2021-09-28 10:31 采纳率: 50%
浏览 17
已结题

按要求用c语言设计计算器小程序(vs2019)

img

  • 写回答

1条回答 默认 最新

  • Soda Wang 2021-09-28 11:22
    关注

    这是大致的代码框架,希望可以帮到你

    #include<conio.h>
    #include<stdio.h>
    
    int main()
    {
        char ch, op = '!';
        int num1 = 0, num2 = 0;
        while (1) {
            ch = _getch();
            printf("%c", ch);
            if (ch >= '0' && ch <= '9') {
                if (op == '!') {
                    num1 = num1 * 10 + (ch - '0');
                }
                else {
                    num2 = num2 * 10 + (ch - '0');
                }
            }
            else if (ch == '+'
                || ch == '-'
                || ch == '*'
                || ch == '/') {
                op = ch;
            }
    
            else if (ch == '\n' || ch == '=') {
                break;
            }
    
            else {
                continue;
            }
        }
    
        switch (op)
        {
        case '+':
            printf("%d\n", num1 + num2);
            break;
        default:
            break;
        }
    
        return 0;
    }
    

    这里只写了加法功能,其他运算和容错功能需要你自己实现

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月9日
  • 创建了问题 9月28日