3.2 栈与表达式求值

3.2 栈与表达式求值_哔哩哔哩_bilibili

 

U597840 中缀转后缀 - 洛谷

//U597840 中缀转后缀 链栈版 C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

// 链栈结构定义
typedef struct LiNode{
    int data;
    struct LiNode *next;
}LiNode,*LiStack;

//初始化栈
void InitStack(LiStack &L){
    L=(LiNode*)malloc(sizeof(LiNode));
    L->next=NULL;
}

//判断栈是否为空
bool StackEmpty(LiStack L){
    return L->next==NULL;
}

//入栈
void Push(LiStack &L,char e){
    LiNode *s=(LiNode*)malloc(sizeof(LiNode));
    s->data=e;
    s->next=L->next;
    L->next=s;
}

//出栈
void Pop(LiStack &L){
    LiNode *s=L->next;
    L->next=s->next;
    free(s);
}

//读栈顶
int Top(LiStack L){
    return L->next->data;
}

int cnt=0;

//运算符的优先级
int Priority(char op){ 
    if(op=='(') return 0;
    if(op=='+' || op=='-') return 1;    
    if(op=='*' || op=='/') return 2;
}

//中缀转后缀
void Transform(char infix[], char suffix[]){ 
    LiStack S; //声明链栈
    InitStack(S);
    int len=strlen(infix);
    for(int i=0; i<len; i++){
        char c=infix[i]; //取中缀字符
        if(c>='A' && c<='Z'||c>='a' && c<='z'){ //若是数字,直接入串
            suffix[cnt++]=c;
        }
        else{ //若不是数字
            if(StackEmpty(S)) Push(S,c); //栈空入栈
            else if(c=='(') Push(S,c); //左括号入栈
            else if(c==')'){ //右括号,只要栈顶不是左括号,就弹出入串
                while(Top(S)!='('){
                    suffix[cnt++]=Top(S);
                    Pop(S);
                }
                Pop(S); //弹出左括号
            }
            else{ //是+-*/
                while(Priority(Top(S))>=Priority(c)){ //栈顶优先级≥当前运算符,则弹出入串
                    suffix[cnt++]=Top(S);
                    Pop(S);
                    if(StackEmpty(S)) break; //栈空停止
                }
                Push(S,c); //把当前运算符入栈
            }
        }
    }
    while(!StackEmpty(S)){ //最后,如果栈不空,则弹出所有元素并入串
        suffix[cnt++]=Top(S);
        Pop(S);
    }
}

int main(){
    char infix[260],suffix[260];
    scanf("%s",infix);
    Transform(infix,suffix);
    for(int i=0;i<cnt;i++) printf("%c",suffix[i]);
    return 0;
}

 

//U597840 中缀转后缀 C++版
#include<iostream>
#include<string>
#include<stack>
using namespace std;

//运算符的优先级
int Priority(char op){ 
    if(op=='(') return 0;
    if(op=='+' || op=='-') return 1;    
    if(op=='*' || op=='/') return 2;
}

//中缀转后缀
void Transform(string &infix, string &suffix){
    stack<char> S;
    for(int i=0; i<infix.size(); i++){
        char c=infix[i]; //取中缀字符
        if(c>='A' && c<='Z'||c>='a' && c<='z'){ //若是数字,直接入串
            suffix+=c;
        }
        else{ //若不是数字
            if(S.empty()) S.push(c); //栈空入栈
            else if(c=='(') S.push(c); //左括号入栈
            else if(c==')'){ //右括号,只要栈顶不是左括号,就弹出入串
                while(S.top()!='('){
                    suffix+=S.top();
                    S.pop();
                }
                S.pop(); //弹出左括号
            }
            else{ //是+-*/
                while(Priority(S.top())>=Priority(c)){ //栈顶优先级≥当前运算符,则弹出入串
                    suffix+=S.top();
                    S.pop();
                    if(S.empty()) break; //栈空停止
                }
                S.push(c); //把当前运算符入栈
            }
        }
    }
    while(!S.empty()){ //最后,如果栈不空,则弹出所有元素并入串
        suffix+=S.top();
        S.pop();
    }
}

int main(){
    string infix;
    string suffix;
    cin>>infix;
    Transform(infix,suffix);
    cout<<suffix;
    return 0;
}
 

 

P1449 后缀表达式 - 洛谷

//P1449 后缀表达式 C++版
#include<iostream>
#include<string>
#include<stack>
using namespace std;

int Calculate(string str){
    stack<int> S; //C++栈
    int num=0;
    for(int i=0,a,b; i<str.length(); i++){
        char c=str[i];
        if(c=='@') break;
        if(c<='9'&&c>='0'){ //取数字
            num=num*10+c-'0';
        }        
        else if(c=='.'){ //数字入栈
            S.push(num);
            num=0;
        }
        else{ //弹出两数,运算结果入栈
            if(c=='+') a=S.top(),S.pop(),b=S.top(),S.pop(), S.push(b+a);
            if(c=='-') a=S.top(),S.pop(),b=S.top(),S.pop(), S.push(b-a);
            if(c=='*') a=S.top(),S.pop(),b=S.top(),S.pop(), S.push(b*a);
            if(c=='/') a=S.top(),S.pop(),b=S.top(),S.pop(), S.push(b/a);
        }
    }
    return S.top();    
}
int main(){
    string str;  
    cin>>str;
    cout<<Calculate(str);
    return 0;
}

 

.

posted @ 2026-09-16 07:06  董晓  阅读(15)  评论(0)    收藏  举报