博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【全网最高端】中缀表达式转为后缀表达式以及求值(可用于负数,阶乘,高精度)...
阅读量:5905 次
发布时间:2019-06-19

本文共 6255 字,大约阅读时间需要 20 分钟。

代码里有注释。。。直接上代码。。。

#include
#define rep(i,k,n) for(int i=k;i<=n;i++)#define per(i,n,k) for(int i=n;i>=k;i--)#define pii pair
#define pb push_back#define mp make_pair#define re return#define se second#define fi firstusing namespace std;//---------------------------------------------------------------head----------------------------------------------------------------------struct BigInteger { typedef unsigned long long LL; static const int BASE = 100000000; static const int WIDTH = 8; vector
s; BigInteger& clean(){while(!s.back()&&s.size()>1)s.pop_back(); return *this;} BigInteger(LL num = 0) {*this = num;} BigInteger(string s) {*this = s;} BigInteger& operator = (long long num) { s.clear(); do { s.push_back(num % BASE); num /= BASE; } while (num > 0); return *this; } BigInteger& operator = (const string& str) { s.clear(); int x, len = (str.length() - 1) / WIDTH + 1; for (int i = 0; i < len; i++) { int end = str.length() - i*WIDTH; int start = max(0, end - WIDTH); sscanf(str.substr(start,end-start).c_str(), "%d", &x); s.push_back(x); } return (*this).clean(); } BigInteger operator + (const BigInteger& b) const { BigInteger c; c.s.clear(); for (int i = 0, g = 0; ; i++) { if (g == 0 && i >= s.size() && i >= b.s.size()) break; int x = g; if (i < s.size()) x += s[i]; if (i < b.s.size()) x += b.s[i]; c.s.push_back(x % BASE); g = x / BASE; } return c; } BigInteger operator - (const BigInteger& b) const { assert(b <= *this); // 减数不能大于被减数 BigInteger c; c.s.clear(); for (int i = 0, g = 0; ; i++) { if (g == 0 && i >= s.size() && i >= b.s.size()) break; int x = s[i] + g; if (i < b.s.size()) x -= b.s[i]; if (x < 0) {g = -1; x += BASE;} else g = 0; c.s.push_back(x); } return c.clean(); } BigInteger operator * (const BigInteger& b) const { int i, j; LL g; vector
v(s.size()+b.s.size(), 0); BigInteger c; c.s.clear(); for(i=0;i
= v.size()) break; LL x = v[i] + g; c.s.push_back(x % BASE); g = x / BASE; } return c.clean(); } BigInteger operator / (const BigInteger& b) const { assert(b > 0); // 除数必须大于0 BigInteger c = *this; // 商:主要是让c.s和(*this).s的vector一样大 BigInteger m; // 余数:初始化为0 for (int i = s.size()-1; i >= 0; i--) { m = m*BASE + s[i]; c.s[i] = bsearch(b, m); m -= b*c.s[i]; } return c.clean(); } BigInteger operator % (const BigInteger& b) const { //方法与除法相同 BigInteger c = *this; BigInteger m; for (int i = s.size()-1; i >= 0; i--) { m = m*BASE + s[i]; c.s[i] = bsearch(b, m); m -= b*c.s[i]; } return m; } int bsearch(const BigInteger& b, const BigInteger& m) const{ int L = 0, R = BASE-1, x; while (1) { x = (L+R)>>1; if (b*x<=m) {if (b*(x+1)>m) return x; else L = x;} else R = x; } } BigInteger& operator += (const BigInteger& b) {*this = *this + b; return *this;} BigInteger& operator -= (const BigInteger& b) {*this = *this - b; return *this;} BigInteger& operator *= (const BigInteger& b) {*this = *this * b; return *this;} BigInteger& operator /= (const BigInteger& b) {*this = *this / b; return *this;} BigInteger& operator %= (const BigInteger& b) {*this = *this % b; return *this;} bool operator < (const BigInteger& b) const { if (s.size() != b.s.size()) return s.size() < b.s.size(); for (int i = s.size()-1; i >= 0; i--) if (s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator >(const BigInteger& b) const{return b < *this;} bool operator<=(const BigInteger& b) const{return !(b < *this);} bool operator>=(const BigInteger& b) const{return !(*this < b);} bool operator!=(const BigInteger& b) const{return b < *this || *this < b;} bool operator==(const BigInteger& b) const{return !(b < *this) && !(b > *this);}};ostream& operator << (ostream& out, const BigInteger& x) { out << x.s.back(); for (int i = x.s.size()-2; i >= 0; i--) { char buf[20]; sprintf(buf, "%08d", x.s[i]); for (int j = 0; j < strlen(buf); j++) out << buf[j]; } return out;}istream& operator >> (istream& in, BigInteger& x) { string s; if (!(in >> s)) return in; x = s; return in;}stack
si;stack
sc;vector
vc;bool isnum[1005];bool flag=0;BigInteger pow(BigInteger a,BigInteger b){BigInteger base=a,ans=1;while(b!=0){if(b%2==1)ans*=base;base*=base;b/=2;}return ans;}BigInteger clas(char c)//符号优先级 { if(c=='(' || c==')') re 0; if(c=='+' || c=='-') re 1; if(c=='*' || c=='/') re 2; if(c=='^') re 3; re 4;}BigInteger cal(BigInteger a,BigInteger b,BigInteger c)//符号求值 { BigInteger ans=1; if(c=='+') re a+b; if(c=='-') re b-a; if(c=='*') re a*b; if(c=='/') re b/a; re pow(b,a);}int main(){ string s;cin>>s; s="("+s+")";//可以用来防止栈为空找top时的溢出情况 int n=(int)s.size(); rep(i,0,n-1) { if(s[i]=='(')//左括号直接压入 { sc.push(s[i]); continue; } if(s[i]==')')//找到右括号将栈清空到左括号为止 { char c=sc.top();sc.pop(); while(c!='(') { vc.pb(c); c=sc.top();sc.pop(); } continue; } if(s[i]>='0' && s[i]<='9')//数字 { BigInteger sum=0; while(s[i]>='0' && s[i]<='9') sum=sum*10+s[i++]-'0'; if(flag) sum=BigInteger(-1)*sum,flag=0; vc.pb(sum); isnum[vc.size()-1]=1; i--;continue; } if(s[i]=='-' && (clas(s[i-1])!=4 && s[i-1]!=')')) { flag=1; continue; } while(!sc.empty() && clas(s[i])<=clas(sc.top()))//如果是符号就找到第一个优先级比他小的 vc.pb(sc.top()),sc.pop(); sc.push(s[i]); } while(!sc.empty())//清空栈 vc.pb(sc.top()),sc.pop(); rep(i,0,vc.size()-1)//根据后缀表达式求值 { if(!isnum[i]) { BigInteger a=si.top();si.pop(); BigInteger b=si.top();si.pop(); BigInteger ans=cal(a,b,vc[i]); si.push(ans); } else si.push(vc[i]); } cout<

转载于:https://www.cnblogs.com/NightRaven/p/9802134.html

你可能感兴趣的文章
面向对象思想(第一天)
查看>>
微信小程序 js逻辑
查看>>
linux 安装 sftp
查看>>
openStack queens
查看>>
(转)EOSIO开发(四)- nodeos、keosd与cleos
查看>>
MVC5+EF6 入门完整教程八
查看>>
Java 设计模式专栏
查看>>
常用Mysql或者PostGresql或者Greenplum的语句总结。
查看>>
工控随笔_12_西门子_WinCC的VBS脚本_03_变量类型
查看>>
使用ASP.NET Atlas SortBehavior实现客户端排序
查看>>
图像滤镜处理算法:灰度、黑白、底片、浮雕
查看>>
Office文档出错的几种原因与解决方法
查看>>
正则表达式 学习笔记1.1
查看>>
AssetBundle进阶内存优化(Unity 4.x)
查看>>
《从零开始学Swift》学习笔记(Day 40)——析构函数
查看>>
Exchange2003-2010迁移系列之十,Exchange证书攻略
查看>>
extmail集群的邮件负载均衡方案 [lvs dns postfix]
查看>>
更改UIView的背景
查看>>
Prometheus安装部署以及配置
查看>>
Web开发之-DOM操作对象
查看>>