首页 > 学院 > 开发设计 > 正文

计算

2019-11-10 22:00:59
字体:
来源:转载
供稿:网友

话说,小明也是够无聊的啊! 普通的中缀已经不能满足小明了,于是,他添加了一个^,(开方运算^_^,谢谢表情!)好厉害的样子啊!但是依旧难不倒我们的OIER,正如游戏所说,你这么厉害,还会错吗?

#include<stdio.h>#include<stdlib.h>#include<cstring>using namespace std;char s[1000001],c[1000001],zhan[1000001];long long top,lenc,calc[1000001];int bj(char a,char b){ if(a=='+') if(b=='+' || b==')' || b=='-' || b=='@')return 1; else return 0; if(a=='-') if(b=='+' || b==')' || b=='-' || b=='@')return 1; else return 0; if(a=='*') if(b=='(')return 0; else return 1; if(a=='/') if(b=='(')return 0; else return 1; if(a=='(') if(b==')')return 2; else return 0; if(a==')') if(b=='(')return 2; else return 0; if(a=='@')return 0; if(a=='^') if(b=='(')return 0; else return 1;}void zhuanhuan(char s[]){ int i=0; zhan[++top]='@'; while(i<strlen(s)){ if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='^') if(s[i-1]=='+' || s[i-1]=='*' || s[i-1]=='-' || s[i-1]=='/' || s[i-1]=='^'){ PRintf("NO/n");exit(0); } if(s[i-1]>='0' && s[i-1]<='9' &&(s[i]<'0' || s[i]>'9'))c[lenc++]=' '; if(s[i]>='0' && s[i]<='9')c[lenc++]=s[i]; else{ while(bj(zhan[top],s[i])==1 && top>1){ c[lenc++]=zhan[top--]; } if(bj(zhan[top],s[i])==0)zhan[++top]=s[i]; else if(top>1)top--; } i++; } if(top>1){ while(top>1){ c[lenc++]=zhan[top]; top--; } }}long long jc(long long a,long long b){ long long sum=1; for(long long i=1;i<=b;i++) sum*=a; return sum;}void cmp(){ long long i=0; while(i<lenc){ if(c[i]=='+')calc[--top]=calc[top]+calc[top+1]; else if(c[i]=='-')calc[--top]=calc[top]-calc[top+1]; else if(c[i]=='*')calc[--top]=calc[top]*calc[top+1]; else if(c[i]=='/')calc[--top]=calc[top]/calc[top+1]; else if(c[i]=='^')calc[--top]=jc(calc[top],calc[top+1]); else{ top++; long long x=0; while(c[i]>='0' && c[i]<='9')x=x*10+c[i++]-'0'; calc[top]=x;if(i+1==lenc)i--; } i++; }}int main(){ freopen("calc.in","r",stdin); freopen("calc.out","w",stdout); int i,j,k,n,m; gets(s); zhuanhuan(s); top=0; cmp(); printf("%lld/n",calc[1]); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表