IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 洛谷P1175 表达式的转换(栈的应用) -> 正文阅读

[数据结构与算法]洛谷P1175 表达式的转换(栈的应用)

洛谷 P1175 表达式的转换

链接.
难度:提高+/省选-
标签:模拟,字符串,线性结构,栈

题意:

给定一个中缀表达式,让我们输出转化为后缀表达式后计算的每一步。
image-20210725220714196

题解:

1.将中缀表达式转化为后缀表达式。

2.利用栈,应用后缀表达式的运算法则实现运算。

#include<iostream>
#include<cmath>
#include<vector>
#include<map>
#include <cctype>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<list>
#define mem(a,n) memset(a,n,sizeof a)
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define SYS system("pause");
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

ll gcd(ll a,ll b){ll d; while(b){ d=b; b=a%b; a=d; } return a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll qpow(ll x, ll y) { ll ans = 1; for (; y > 0; y >>= 1) { if (y & 1)ans *= x; x *= x; } return ans;}
ll qpow(ll x, ll y, int MOD) { ll ans = 1; for (; y > 0; y >>= 1) { if (y & 1)ans = ans*x%MOD; x = x*x%MOD; } return ans;}
void exgcd(int a,int b,int &x,int &y){ if(b==0){ x=1;y=0;return; } exgcd(b,a%b,x,y); int temp=y; y=x-(a/b)*y, x=temp; return;}
int downcheck(int l, int r){ while (l < r){ int mid = l + r >> 1; if ("check(mid)") r = mid; else l = mid + 1; } return l;}
int upcheck(int l, int r){ while (l < r){ int mid = l + r + 1 >> 1; if ("check(mid)") l = mid; else r = mid - 1;} return l;}
int doublecheck(int l,int r){ while(r-l>1e-8){ int mid=(l+r)>>1; if("check(mid)") l=mid; else r=mid;} return l; }

const int N = 4e5+10,M=20;
const int inf=0x3f3f3f3f;
const int mod=1e9+7; 
const double pi = acos(-1.0);
int n,t,m,x;
string str[1000];
string res="";

 //这个函数,用于比较运算符号的优先级
int priority(const char& ch) {  
    switch(ch) {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    case '^':
        return 3;
    case '(':
    case ')':
        return 0;
    }
}

//将中缀表达式转化为后缀表达式
string chance(string s){
    stack<char>ch;
    for (int i = 0; i < s.size();i++){
        if(isdigit(s[i])) res += s[i];
        else if(s[i]=='(')  ch.push(s[i]);
        else if(s[i]==')'){
            while(ch.top()!='('){
                res += ch.top();
                ch.pop();
            }
            ch.pop();
        }
        else{
              while (!ch.empty() && priority(ch.top()) >= priority(s[i])){
                  res += ch.top();
                  ch.pop();
            }
            ch.push(s[i]);
        }
    }
    while (!ch.empty())   // 最后如果栈中还有剩余的字符,直接弹出并输出
        res += ch.top(), ch.pop();
    return res;
}

//计算各个运算符
int calcNum(const int& a, const int& b, const int& symbol) {
    switch(symbol) {
    case '+':
        return a + b;
    case '-':
        return a - b;
    case '*':
        return a * b;
    case '/':
        return a / b;
    case '^':
        return (int)pow(a, b);
    }
}

//计算后缀表达式的计算结果
void calc(const string& s) {
    list<int> st;
     for (int i = 0; i < s.size(); i ++)
        cout << s[i] << ' ';
        cout << endl;    
    for (int i = 0; i < s.size(); i++) {
        if (isdigit(s[i]))  st.push_back(s[i] - '0');
        else {                
            int a, b;  
            a = st.back();
            st.pop_back();
            b = st.back();
            st.pop_back();
            st.push_back(calcNum(b, a, s[i])); 
            for (list<int>::iterator it = st.begin(); it != st.end(); ++it)
                cout << *it << ' ';           
            for (register int j = i + 1; j < s.size(); j ++)
                cout << s[j] << ' ';           
            cout << endl;
        }
    }
}

int main(){
    IOS
    string s;
    cin >> s;
    chance(s);
    calc(res);
    //SYS
    return 0;
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-26 12:18:24  更:2021-07-26 12:19:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 17:03:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码