2301_77959776 2023-05-04 03:21 采纳率: 100%
浏览 202
已结题

用C语言编写一个简单的计算器

要求以实现十进制浮点数的连续四则运算,并能够对括号的嵌套进行正确解析,能够解析“减号”和“负号”

img

img

  • 写回答

8条回答 默认 最新

  • threenewbee 2023-05-04 03:45
    关注

    花了一个多小时纯手工帮你写好了,你只需要加上你的学号姓名即可交差,如果满意,还请赏个采纳吧,谢谢

    // Q7937325.cpp : 定义控制台应用程序的入口点。
    //
    //#include "stdafx.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    /*****************************************/
    /* 将数字字符转化成浮点型实数进行计算 */
    /*****************************************/
    double readnum(char f[], int*i)
    {
        double x = 0.0;
        int k = 0;
        while (f[*i] >= '0'&&f[*i] <= '9')
        {
            x = x * 10 + (f[*i] - '0');
            (*i)++;
        }
        if (f[*i] == '.')
        {
            (*i)++;
            while (f[*i] >= '0'&&f[*i] <= '9')
            {
                x = x * 10 + (f[*i] - '0');
                (*i)++;
                k++;
            }
        }
        while (k-->0)
        {
            x = x / 10.0;
        }
        return (x);
    }
    /*******************************/
    /* 计算后缀表达式的值 */
    /*******************************/
    double evalpost(char f[])
    {
        double obst[10];
        int top = 0;
        int i = 0;
        double x1, x2;
        while (f[i] != '=')
        {
            if (f[i] >= '0'&&f[i] <= '9')
            {
                obst[top] = readnum(f, &i); top++;
            }
            else if (f[i] == ' ')
                i++;
            else if (f[i] == '+')
            {
                x1 = obst[--top];
                x2 = obst[--top];
                obst[top] = x1 + x2;
                i++;
                top++;
            }
            else if (f[i] == '-')
            {
                x1 = obst[--top];
                x2 = obst[--top];
                obst[top] = x2 - x1;
                i++;
                top++;
            }
            else if (f[i] == '*')
            {
                x1 = obst[--top];
                x2 = obst[--top];
                obst[top] = x1*x2;
                i++;
                top++;
            }
            else if (f[i] == '*')
            {
                x1 = obst[--top];
                x2 = obst[--top];
                obst[top] = x1*x2;
                i++;
                top++;
            }
            else if (f[i] == '/')
            {
                x1 = obst[--top];
                x2 = obst[--top];
                obst[top] = x2 / x1;
                i++;
                top++;
            }
        }
        return obst[0];
    }
    /***********************************/
    /* 判断字符是否为操作字符 */
    /***********************************/
    int is_operation(char op)
    {
        switch (op)
        {
        case'^':
        case'K':
        case'+':
        case'-':
        case'*':
        case'/': return 1;
        default: return 0;
        }
    }
    /*****************************/
    /* 判断字符的优先级 */
    /*****************************/
    int priority(char op)
    {
        switch (op)
        {
        case'=': return -1;
        case'(': return 0;
        case'+':
        case'-': return 1;
        case'*':
        case'/': return 2;
        default: return -1;
        }
    }
    /******************************/
    /* 中缀表达式转化成后缀表达式*/
    /******************************/
    void postfix(char e[], char f[])
    {
        int i = 0, j = 0, k = 0;
        char opst[100];
        int top = 0;
        opst[0] = '='; top++;
        while (e[i] != '=')
        {
            if ((e[i] >= '0'&&e[i] <= '9') || e[i] == '.')
                f[j++] = e[i];
            else if (e[i] == '(')
            {
                opst[top] = e[i]; top++;
            }
            else if (e[i] == ')')
            {
                k = top - 1;
                while (opst[k] != '(') { f[j++] = opst[--top]; k = top - 1; }
                top--;
            }
            else if (is_operation(e[i]))
            {
                f[j++] = ' ';
                while (priority(opst[top - 1]) >= priority(e[i]))
                    f[j++] = opst[--top];
                opst[top] = e[i];
                top++;
            }
            i++;
        }
        while (top) f[j++] = opst[--top]; f[j] = '\0';
    }
    void print_1(char val[])
    {
        system("cls");
        printf("| ______________________________________ | \n");
        printf("| |                                    | | \n");
        printf("| |        欢迎使用多功能计算器        | | 本计算器能够进行10进制数 \n");
        printf("| |____________________________________| | +,/,*,/,()的连续运算\n");
        printf("| |____________________________________| | \n");
        printf("| |       设计人:这里放你的学号姓名   | | 输入所要经计算的表达式 \n");
        printf("| |____________________________________| | 如:a*b/(c-d)\n");
        printf("|                                        | \n");
        printf("|   %s", val);
        for (int i = 0; i < 35 - strlen(val); i++) 
            printf(" ");
        printf("  | \n");
        printf("| ___  ___  ___  ___  ___  ___  ___  ___ | \n");
        printf("|  ________ ________ ________ ________   | \n");
        printf("|  |      | |      | |      | |      |   | \n");
        printf("|  |  ⑨  | |  ⑧  | |  ⑦  | |  ×  |   | \n");
        printf("|  |______| |______| |______| |______|   | \n");
        printf("|  ________ ________ ________ ________   | \n");
        printf("|  |      | |      | |      | |      |   | \n");
        printf("|  |  ⑥  | |  ⑤  | |  ④  | |  -  |   | \n");
        printf("|  |______| |______| |______| |______|   | \n");
        printf("|  ________ ________ ________ ________   | \n");
        printf("|  |      | |      | |      | |      |   | \n");
        printf("|  |  ③  | |  ②  | |  ①  | |  +  |   | 继续计算/退出?1/0\n");
        printf("|  |______| |______| |______| |______|   | \n");
        printf("|  ________ ________ ________ ________   | \n");
        printf("|  |      | |      | |      | |      |   | \n");
        printf("|  |  〇  | |   = | |  AC  | |  ÷  |   | \n");
        printf("|  |______| |______| |______| |______|   | \n");
        printf("|________________________________________| \n");
        getch();
        system("cls");
    }
    void printf_2()
    {
        system("cls");
        printf("\n\n\n\n\n\n\n\n\t\t\t ##############################\n");
        printf("\t\t\t # #\n");
        printf("\t\t\t #----------谢谢使用----------#\n");
        printf("\t\t\t # #\n");
        printf("\t\t\t ##############################\n");
        printf("\t\t\t --制作\n ");
    }
    /****************/
    /* 转化 */
    /****************/
    void zhuanhuan(char g[], char e[])
    {
        int k, i, j = 0;
        for (i = 0; g[i] != '='; i++)
        {
            k = i + 1;
            if (g[i] == '('&&g[k] == '-')
            {
                e[j++] = g[i];
                e[j++] = '0';
            }
            else e[j++] = g[i];
        }
        e[j] = '=';
    }
    int main()
    {
        int wei;
        char e[100], f[100], g[100];
        int sign; int flag;
        print_1("0");
        do
        {
            printf("输入所要经计算的表达式(如:a*b/(c-d)=):\n");
            scanf("%s", g);
            zhuanhuan(g, e);
            postfix(e, f);
            printf("输出保留几位小数:\n");
            scanf("%d", &wei);
            char resstr[100];
            sprintf(resstr, "%s%.*lf", g, wei, evalpost(f));
            print_1(resstr);
            while (1)
            {
                flag = 3;
                printf("继续计算/退出?1/0?");
                sign = getch();
                printf("%c\n", sign);
                switch (sign)
                {
                case '1':flag = 1; getch(); break;
                case '0':flag = 0; getch(); break;
                default: printf("非法输入,请重新输入:\n");
                }
                if (flag == 1 || flag == 0)break;
            }
        } while (flag == 1);
        printf_2();
        return 0;
    }
    

    开发截图

    img

    以下是程序运行效果

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(7条)

报告相同问题?

问题事件

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