C语言实现后缀表达式转中缀表达式(封装得很好哦~

本题实现了用二叉树实现后缀表达式转换为中缀表达式,代码封装效果很好。
输入要求:

  • 要求运算符和数字之间以空间间隔开来
  • 最后可以有=也可以没有
    输出的中缀表达式都每一部分都含有括号

这是可以进行优化的点

//
// Created by 86152 on 2023/6/9.
//

//Author hugo
//Time 2023/6/9 11:36
/*
 *本题是表达式计算的题目,需要将后缀表达式转化为中缀表达式
 * 需要输入的数字和运算符之间以空格分开
*/
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100
#define NUMBER 1
#define OPERATOR 2
typedef struct aNode {
    int state;
    int num;
    char op;
    struct aNode *leftchild;
    struct aNode *rightchild;
} Node;
typedef struct aStack {
    Node *stack[MAX_SIZE];
    int top;
} Stack;

int isOperator(char ch) {
    return (ch == '+' || ch == '-' || ch == '/' || ch == '*');
}

Node *initList(char *string) {
    if (isOperator(string[0])) {
        Node *node = (Node *) malloc(sizeof(Node));
        node->state = OPERATOR;
        node->op = string[0];
        node->leftchild = NULL;
        node->rightchild = NULL;
        return node;
    } else {
        int N = atoi(string);
        Node *node = (Node *) malloc(sizeof(Node));
        node->state = NUMBER;
        node->num = N;
        node->leftchild = NULL;
        node->rightchild = NULL;
        return node;
    }
}

Stack *initStack() {
    Stack *S = (Stack *) malloc(sizeof(Stack));
    S->top = -1;
    for (int i = 0; i < MAX_SIZE; ++i) {
        S->stack[i] = NULL;
    }
    return S;
}

int isEmpty(Stack *S) {
    return (S->top == -1);
}

int isFull(Stack *S) {
    return S->top == MAX_SIZE;
}

Node *popStack(Stack *S) {
    if (isEmpty(S)) {
        printf("The stack is empty\n");
        exit(1);
    }
    return S->stack[(S->top)--];
}

void pushStack(Stack *S, Node *data) {
    if (isFull(S)) {
        printf("The stack is full\n");
        exit(1);
    }
    S->stack[++(S->top)] = data;
}

void VISIT(Node *node) {
    if (node->state == NUMBER) {
        printf("%d", node->num);
    } else {
        printf("%c", node->op);
    }
}

Node *bulidTree() {
    Stack *S = initStack();
    char str[20];
    while (scanf("%s", str) != EOF) {
        Node *node = initList(str);
        if (str[0] == '=') break;
        if (node->state == OPERATOR) {
            Node *p = popStack(S);
            Node *q = popStack(S);
            node->leftchild = q;
            node->rightchild = p;
            pushStack(S, node);
        } else {
            pushStack(S, node);
        }
    }
    return S->stack[0];
}

void inorderTraversal(Node *root) {
    if (root == NULL) return;
    if (root->leftchild != NULL || root->rightchild != NULL) {
        printf("(");
    }
    inorderTraversal(root->leftchild);
    VISIT(root);
    inorderTraversal(root->rightchild);
    if (root->leftchild != NULL || root->rightchild != NULL) {
        printf(")");
    }
}

int main() {
    char postfix[MAX_SIZE];
    printf("input the postfix : \n");
    Node *root = bulidTree();
    inorderTraversal(root);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C语言实现中缀表达式后缀表达式的方法如下: 1. 创建一个空的栈,用于存储运算符。 2. 从左到右遍历中缀表达式的每个字符。 3. 如果遇到操作数(数字),直接输出到后缀表达式。 4. 如果遇到左括号"(",将其压入栈中。 5. 如果遇到右括号")",则将栈中的运算符依次弹出并输出到后缀表达式,直到遇到左括号为止。注意:左括号不输出到后缀表达式,也不入栈。 6. 如果遇到运算符,比较其与栈顶运算符的优先级: - 如果栈为空,或栈顶运算符为左括号"(",则将当前运算符入栈。 - 如果当前运算符的优先级大于栈顶运算符的优先级,则将当前运算符入栈。 - 如果当前运算符的优先级小于或等于栈顶运算符的优先级,则将栈顶运算符弹出并输出到后缀表达式,然后继续比较当前运算符与新的栈顶运算符的优先级,直到当前运算符的优先级大于栈顶运算符的优先级,或栈为空,然后将当前运算符入栈。 7. 当遍历完中缀表达式后,将栈中剩余的运算符依次弹出并输出到后缀表达式。 以下是一个示例代码,演示了如何实现中缀表达式后缀表达式的过程: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 typedef struct { char data[MAX_SIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } int isEmpty(Stack *s) { return s->top == -1; } int isFull(Stack *s) { return s->top == MAX_SIZE - 1; } void push(Stack *s, char c) { if (isFull(s)) { printf("Stack is full.\n"); exit(1); } s->data[++s->top] = c; } char pop(Stack *s) { if (isEmpty(s)) { printf("Stack is empty.\n"); exit(1); } return s->data[s->top--]; } int isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } int getPriority(char c) { if (c == '*' || c == '/') { return 2; } else if (c == '+' || c == '-') { return 1; } else { return 0; } } void infixToPostfix(char *infix, char *postfix) { Stack s; initStack(&s); int len = strlen(infix); int j = 0; for (int i = 0; i < len; i++) { char c = infix[i]; if (isOperator(c)) { while (!isEmpty(&s) && isOperator(s.data[s.top]) && getPriority(s.data[s.top]) >= getPriority(c)) { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '(') { push(&s, c); } else if (c == ')') { while (!isEmpty(&s) && s.data[s.top] != '(') { postfix[j++] = pop(&s); } if (!isEmpty(&s) && s.data[s.top] == '(') { pop(&s); } else { printf("Invalid expression: Unmatched parentheses.\n"); exit(1); } } else { postfix[j++] = c; } } while (!isEmpty(&s)) { if (s.data[s.top] == '(') { printf("Invalid expression: Unmatched parentheses.\n"); exit(1); } postfix[j++] = pop(&s); } postfix[j] = '\0'; } int main() { char infix[MAX_SIZE]; char postfix[MAX_SIZE]; printf("Enter an infix expression: "); fgets(infix, MAX_SIZE, stdin); infix[strlen(infix) - 1] = '\0'; infixToPostfix(infix, postfix); printf("Postfix expression: %s\n", postfix); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值