C 逆波兰表达式求值

1.栈的顺序存储结构

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 20
typedef struct stack
{
    int data[N];
    int top;
}STACK;
void push(STACK *stack,int d);
int pop(STACK *stack);
int operate(int a,int b,char op);
int main(){
    char word[N];
    STACK stack;
    stack.top=0;
    int d1,d2,d3;
    while(scanf("%s",word)==1 && word[0]!='#'){
        if(isdigit(word[0])){
            d1=atoi(word);
            push(&stack,d1);
        }else{
            d1=pop(&stack);
            d2=pop(&stack);
            d3=operate(d1,d2,word[0]);
            push(&stack,d3);
        }
    }
    d1=pop(&stack);
    printf("%d\n",d1);
    return 0;
}
void push(STACK *stack,int d){
    memcpy(&stack->data[stack->top],&d,sizeof(int));
    stack->top+=1;
}
int pop(STACK *stack){
    stack->top-=1;
    return stack->data[stack->top];
}
int operate(int a,int b,char op){
    int res;
    switch(op)
    {
    case'+':
        res=a+b;
        break;
    case'-':
        res=a-b;
        break;
    case'*':
        res=a*b;
        break;
    case'/':
        res=a/b;
        break;

    }
    return res;
}

2.栈的链式存储结构

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 20
typedef struct stack
{
    int data;
    struct stack *next;
}STACK;
STACK *push(STACK *top,int d);
STACK *pop(STACK *top);
int operate(int a,int b,char op);
int main(){
    char word[N];
    STACK *top=NULL;
    int d1,d2,d3;
    while(scanf("%s",word)==1 && word[0]!='#'){
        if(isdigit(word[0])){
            d1=atoi(word);
            top=push(top,d1);
        }else{
            d1=top->data;
            top=pop(top);
            d2=top->data;//当d2=8时,top指向最后一个节点
            top=pop(top);//d2=8,接着top=top->next=NULL,最后一个节点所占内存被释放
            d3=operate(d1,d2,word[0]);
            top=push(top,d3);//最终结果占一个节点,整个链表只剩最终结果的节点
        }
    }
    d1=top->data;
    printf("%d\n",d1);
    pop(top);//释放最后一个节点所占内存
    return 0;
}
STACK *push(STACK *top,int d){
    STACK *p=NULL;
    p=(STACK *)malloc(sizeof(STACK));
    p->data=d;
    p->next=top;
    top=p;
    return top;
}
STACK *pop(STACK *top){
    STACK *p;
    if(top==NULL){
        return NULL;
    }else{
        p=top;
        top=top->next;
        free(p);
    }
    return top;
}
int operate(int a,int b,char op){
    int res;
    switch(op)
    {
    case'+':
        res=a+b;
        break;
    case'-':
        res=a-b;
        break;
    case'*':
        res=a*b;
        break;
    case'/':
        res=a/b;
        break;

    }
    return res;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
逆波兰表达式(Reverse Polish Notation,RPN)是一种将运算符放在操作数之后的数学表达法,可以用来表示算术表达式。在C语言中,我们可以使用栈来求解逆波兰表达式。 以下是一个用C语言实现逆波兰表达式求值的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_SIZE 100 // 栈的最大容量 // 定义栈结构 typedef struct { double data[MAX_SIZE]; int top; } Stack; // 初始化栈 void initStack(Stack *stack) { stack->top = -1; } // 判断栈是否为空 int isEmpty(Stack *stack) { return stack->top == -1; } // 判断栈是否已满 int isFull(Stack *stack) { return stack->top == MAX_SIZE - 1; } // 入栈 void push(Stack *stack, double value) { if (isFull(stack)) { printf("Stack is full\n"); exit(1); } stack->top++; stack->data[stack->top] = value; } // 出栈 double pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); exit(1); } double value = stack->data[stack->top]; stack->top--; return value; } // 逆波兰表达式求值 double evaluateRPN(char *expression) { Stack stack; initStack(&stack); char *token = expression; double operand1, operand2, result; while (*token != '\0') { if (isdigit(*token)) { push(&stack, atof(token)); } else if (*token == '+' || *token == '-' || *token == '*' || *token == '/') { operand2 = pop(&stack); operand1 = pop(&stack); switch (*token) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } push(&stack, result); } // 移动到下一个token while (*token != ' ' && *token != '\0') { token++; } if (*token == ' ') { token++; } } return pop(&stack); } int main() { char expression[] = "5 3 4 * + 2 /"; double result = evaluateRPN(expression); printf("Result: %.2f\n", result); return 0; } ``` 以上代码可以通过使用栈来实现逆波兰表达式的求值。在主函数中,我们定义了一个逆波兰表达式"5 3 4 * + 2 /",并调用`evaluateRPN`函数来求解该表达式的值。最终输出结果为"Result: 7.50"。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值