landier* 2022-10-28 21:50 采纳率: 85.7%
浏览 64
已结题

c语言怎么用函数使四则运算不按优先级

模拟简单运算器的工作,输入一个算式(没有空格),遇等号"="说明输入 结束,输出结果。假设计算器只能进行加、减、乘、除运算,运算数和结果都是整数,4 种运算符的优先级相同,按从左到右的顺序计算。例如,输入 1+2*10-10/2=后,输出10。试编写相应程序。其中的运算部分要求定义函数实现。

我想设四个函数让他分头进行,但是要输入啊——不会了

  • 写回答

1条回答 默认 最新

  • 谢玄. 2022-10-29 00:27
    关注

    我小学的时候写过一个类似的,但是因为那个时候还小。写的好丑,凑合着看吧。不懂问我;
    望采纳!

    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    
    
    #define TEN 10
    
    int ONE_H;
    int area;
    int OPERATION_1[TEN];
    int OPERATION_2[TEN];
    int OPERATION_A_YUN;
    int OPERATION_B_YUN;
    
    void Clearn();
    
    void AREA();
    void OPERATION_FOR_1();
    void OPERATION_FOR_2();
    void VALUATION();
    void ADD();
    void MINUS();
    void MULTIPLY();
    void DIVISION();
    bool GUI();
    
    int main() {
        printf("0");
        for (;;) {
            // 结果初始化为0
            area = 0;
            // 标志现在是 第奇次 还是第偶次运算 运算
            ONE_H = 0;
            //Clearn();
            while (1) {
                // 输出答案
    
                // 奇次
                if (ONE_H % 2 == 0)
                    OPERATION_FOR_1();
                // 偶次
                if (ONE_H % 2 == 1)
                    OPERATION_FOR_2();
    
                if (ONE_H == 0)
                    VALUATION();
                // 加法
                ADD();
                // 减法
                MINUS();
                // 乘法
                MULTIPLY();
                // 除法
                DIVISION();
                if ( GUI() ) {
                    break;
                }
                AREA();
                ONE_H++;
            }
        }
        return 0;
    }
    
    void AREA() {   //?????
        system("cls");
        printf("%d", area);
    }
    
    void OPERATION_FOR_1() {
        // 单个字符的读入 在10个字符 范围内
        for (OPERATION_A_YUN = 0; OPERATION_A_YUN < TEN; OPERATION_A_YUN++) {
            OPERATION_1[OPERATION_A_YUN] = getch();
            if (OPERATION_A_YUN == 0)
                system("cls");
            putchar(OPERATION_1[OPERATION_A_YUN]);
            // 如果输入的字符不是 数字的话就停止读入
            if (OPERATION_1[OPERATION_A_YUN] > '9' || OPERATION_1[OPERATION_A_YUN] < '0')
                break;
        }
    }
    
    void OPERATION_FOR_2() {
        for (OPERATION_B_YUN = 0; OPERATION_B_YUN < TEN; OPERATION_B_YUN++) {
            OPERATION_2[OPERATION_B_YUN] = getch();
            if (OPERATION_B_YUN == 0) system("cls");
            putchar(OPERATION_2[OPERATION_B_YUN]);
            if (OPERATION_2[OPERATION_B_YUN] > '9' || OPERATION_2[OPERATION_B_YUN] < '0') break;
        }
    }
    
    void VALUATION() {  //??OPERATION_1???????area
        int i, E_1 = 1;
        for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
            area = area + (OPERATION_1[i] - '0') * E_1;
            E_1 *= 10;
        }
    }
    
    void ADD() {
        if (OPERATION_1[OPERATION_A_YUN] == '+' && ONE_H != 0 && ONE_H % 2 == 1) {
            int i, E_1 = 1;
            for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
                area = area + (OPERATION_2[i] - '0') * E_1;
                E_1 *= 10;
            }
        }
        if (OPERATION_2[OPERATION_B_YUN] == '+' && ONE_H != 0 && ONE_H % 2 == 0) {
            int i, E_1 = 1;
            for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
                area = area + (OPERATION_1[i] - '0') * E_1;
                E_1 *= 10;
            }
        }
    }
    
    void MINUS() {
        if (OPERATION_1[OPERATION_A_YUN] == '-' && ONE_H != 0 && ONE_H % 2 == 1) {
            int i, E_1 = 1;
            for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
                area = area - (OPERATION_2[i] - '0') * E_1;
                E_1 *= 10;
            }
        }
        if (OPERATION_2[OPERATION_B_YUN] == '-' && ONE_H != 0 && ONE_H % 2 == 0) {
            int i, E_1 = 1;
            for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
                area = area - (OPERATION_1[i] - '0') * E_1;
                E_1 *= 10;
            }
        }
    }
    
    void MULTIPLY() {
        if (OPERATION_1[OPERATION_A_YUN] == '*' && ONE_H != 0 && ONE_H % 2 == 1) {
            int i, E_1 = 1, mod_A = 0;
            for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
                mod_A = mod_A + (OPERATION_2[i] - '0') * E_1;
                E_1 *= 10;
            }
            area *= mod_A;
        }
        if (OPERATION_2[OPERATION_B_YUN] == '*' && ONE_H != 0 && ONE_H % 2 == 0) {
            int i, E_1 = 1, mod_A = 0;
            for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
                mod_A = mod_A + (OPERATION_1[i] - '0') * E_1;
                E_1 *= 10;
            }
            area *= mod_A;
        }
    }
    
    void DIVISION() {
        if (OPERATION_1[OPERATION_A_YUN] == '/' && ONE_H != 0 && ONE_H % 2 == 1) {
            int i, E_1 = 1, DIVISION_A = 0;
            for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
                DIVISION_A = DIVISION_A + (OPERATION_2[i] - '0') * E_1;
                E_1 *= 10;
            }
            area /= DIVISION_A;
        }
        if (OPERATION_2[OPERATION_B_YUN] == '/' && ONE_H != 0 && ONE_H % 2 == 0) {
            int i, E_1 = 1, DIVISION_A = 0;
            for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
                DIVISION_A = DIVISION_A + (OPERATION_1[i] - '0') * E_1;
                E_1 *= 10;
            }
            area /= DIVISION_A;
        }
    }
    
    bool GUI() {
        // 如果输入的是 A 的话就归零
        if (OPERATION_1[OPERATION_A_YUN] == 'a' || OPERATION_1[OPERATION_A_YUN] == 'A') {
            area = 0;
            system("cls");
            printf("%d", area);
            return true;
        }
        if (OPERATION_2[OPERATION_B_YUN] == 'a' || OPERATION_1[OPERATION_B_YUN] == 'A') {
            area = 0;
            system("cls");
            printf("%d", area);
            return true;
        }
        if (OPERATION_1[OPERATION_A_YUN] == '=' && ONE_H != 0 && ONE_H % 2 == 0) {
            system("cls");
            printf("%d", area);
            return true;
        }
        if (OPERATION_2[OPERATION_B_YUN] == '=' && ONE_H != 0 && ONE_H % 2 == 1) {
            system("cls");
            printf("%d", area);
            return true;
        }
        return false;
    }
    
    void Clearn(){
        ONE_H = 0;
        area = 0;
        for(int i = 0 ; i < TEN ; i++ ){
            OPERATION_1[i] = 0;
        }
        for(int i = 0 ; i < TEN ; i++ ){
                OPERATION_2[i] = 0;
        }
        OPERATION_A_YUN = 0;
        OPERATION_B_YUN = 0;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 修改了问题 10月28日
  • 创建了问题 10月28日

悬赏问题

  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云