ZBLOG

#define _CRT_SECURE_NO_WARNINGS 1 #include #include #include #include #include #define MAX_SIZE 100 typedef struct { float data[MAX_SIZE]; int top; } Stack; void initialize(Stack* stack...

你可以将代码中的float类型修改为double类型,这样结果就能输出小数了。具体修改如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#define MAX_SIZE 100

typedef struct {
    double data[MAX_SIZE];
    int top;
} Stack;

void initialize(Stack* stack) 
{
    stack->top = -1;
}

bool isEmpty(Stack* stack) {
    return (stack->top == -1);
}

bool isFull(Stack* stack) {
    return (stack->top == MAX_SIZE - 1);
}

void push(Stack* stack, double value) {
    if (!isFull(stack)) {
        stack->data[++(stack->top)] = value;
    }
}

double pop(Stack* stack) {
    if (!isEmpty(stack)) {
        return stack->data[(stack->top)--];
    }

    return 0.0;
}

double peek(Stack* stack) {
    if (!isEmpty(stack)) {
        return stack->data[stack->top];
    }

    return 0.0;
}

int precedence(char operator)
{
    switch (operator) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        default:
            return 0;
    }
}

bool isOperator(char character) {
    switch (character) {
        case '+':
        case '-':
        case '*':
        case '/':
            return true;
        default:
            return false;
    }
}

bool isOperand(char character) {
    return isdigit(character);
}

double evaluateExpression(char expression[]) {
    Stack operandStack;
    initialize(&operandStack);

    for (int i = 0; expression[i] != '\0'; i++) {

        if (expression[i] == ' ' || expression[i] == '\t') {
            continue;
        }
        else if (isOperand(expression[i])) {
            char num[MAX_SIZE];
            int j = 0;

            while (isdigit(expression[i]) || expression[i] == '.') {
                num[j++] = expression[i++];
            }

            num[j] = '\0';

            double operand = atof(num);
            push(&operandStack, operand);

            i--;
        }
        else if (isOperator(expression[i])) {
            double operand2 = pop(&operandStack);
            double operand1 = pop(&operandStack);

            switch (expression[i]) {
                case '+':
                    push(&operandStack, operand1 + operand2);
                    break;
                case '-':
                    push(&operandStack, operand1 - operand2);
                    break;
                case '*':
                    push(&operandStack, operand1 * operand2);
                    break;
                case '/':
                    if (operand2 != 0) {
                        push(&operandStack, operand1 / operand2);
                    }
                    else {
                        printf("       除数不能为零!\n");
                        exit(0); // 程序异常退出
                    }
                    break;
            }
        }
    }

    return pop(&operandStack);
}

void infixToPostfix(char infix[], char postfix[]) {
    Stack operatorStack;
    initialize(&operatorStack);

    int i, j;
    i = j = 0;

    while (infix[i] != '\0') {

        if (infix[i] == ' ' || infix[i] == '\t') {
            i++;
            continue;
        }

        if (isOperand(infix[i])) {
            postfix[j++] = infix[i++];

            while (isdigit(infix[i]) || infix[i] == '.') {
                postfix[j++] = infix[i++];
            }

            postfix[j++] = ' ';
            i--;
        }
        else if (infix[i] == '(') {
            push(&operatorStack, infix[i]);
        }
        else if (infix[i] == ')') {
            while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
                postfix[j++] = pop(&operatorStack);
                postfix[j++] = ' ';
            }

            if (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
                printf("Error: Invalid expression\n");
                exit(0); // 程序异常退出
            }
            else {
                pop(&operatorStack); // 弹出左括号
            }
        }
        else if (isOperator(infix[i])) {
            while (!isEmpty(&operatorStack) && precedence(peek(&operatorStack)) >= precedence(infix[i])) {
                postfix[j++] = pop(&operatorStack);
                postfix[j++] = ' ';
            }

            push(&operatorStack, infix[i]);
        }

        i++;
    }

    while (!isEmpty(&operatorStack)) {
        postfix[j++] = pop(&operatorStack);
        postfix[j++] = ' ';
    }

    postfix[j] = '\0';
}

int main() {
    Stack* stack;
    char infix[MAX_SIZE];
    printf("请输入中缀(后缀)表达式:");
    fgets(infix, MAX_SIZE, stdin);

    char postfix[MAX_SIZE];
    infixToPostfix(infix, postfix);

    printf("后缀表达式为:%s\n", postfix);

    double result = evaluateExpression(postfix);
    printf("计算结果为:%f\n", result);

    return 0;
}

这样修改后,代码将会使用double类型进行计算,并输出小数结果。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9200

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?