这些都是emmmm
http://www.tyvj.cn/p/1041
http://www.tyvj.cn/p/1042
http://codevs.cn/problem/2178/
描述
给出一个表达式,其中运算符仅包含+,-,*,/,^要求求出表达式的最终值数据可能会出现括号情况 还有可能出现多余括号情况数据保证不会出现>maxlongint的数据。数据可能回出现负数情况
样例输入 Sample Input
(2+2)^(1+1)
样例输出 Sample Output
16
分析
真的是超级麻烦emmmm,今天考试的时候看了看接着就弃了。。。说实话这个真不应该弃掉,但是真的不会了w
需要开long long,不开的话炸了一个点,真是麻烦
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
typedef long long LL;
char ch[10005];
LL num[10005], top1;
LL opt[10005], top2;
void get_num()
{
if (!top2)
return;
LL y = num[top1--], x = num[top1--];
LL pos = opt[top2--];
if (pos == 5) x = pow(x, y);
if (pos == 4) x = x / y;
if (pos == 3) x = x * y;
if (pos == 2) x = x - y;
if (pos == 1) x = x + y;
num[++top1] = x;
}
int main()
{
scanf("%s", ch);
LL n = strlen(ch);
for (int i = 0; i < n;)
{
if (ch[i] >= '0' && ch[i] <= '9')
{
LL x = 0;
while(ch[i] >= '0' && ch[i] <= '9')
{
x = x * 10 + ch[i] - '0';
i++;
}
num[++top1] = x;
}
if (ch[i] == '(')
{
opt[++top2] = 0;
i++;
}
if (ch[i] == '+')
{
while(top2 && opt[top2] != 0) get_num();
opt[++top2] = 1; i++;
}
if (ch[i] == '-')
{
if (i == 0 || ch[i - 1] == '(')
{
int x = 0; i++;
while(ch[i] >= '0' && ch[i] <= '9')
{
x = x * 10 - (ch[i] - '0');
i++;
}
num[++top1] = x;
}
else
{
while(top2 && opt[top2] != 0)
get_num();
opt[++top2] = 2;
i++;
}
}
if (ch[i] == '*')
{
while(top2 && opt[top2] > 2)
get_num();
opt[++top2] = 3;
i++;
}
if (ch[i] == '/')
{
while(top2 && opt[top2] > 2)
get_num();
opt[++top2] = 4;
i++;
}
if (ch[i] == '^')
{
while(top2 && opt[top2] > 4)
get_num();
opt[++top2] = 5;
i++;
}
if (ch[i] == ')')
{
while(top2 && opt[top2] != 0)
get_num();
top2--;
i++;
}
}
while(top2)
{
if (opt[top2] == 0)
top2--;
else
get_num();
}
printf("%lld\n", num[top1]);
return 0;
}
然后是std:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=100000+5;
typedef long long ll;
char opt[MAXN],ch[MAXN];
ll sta[MAXN];
int tpo,tp,len;
ll pow(ll x,ll a)
{
if(a==0) return 1;
if(a==1) return x;
if(a<0) return 0;
ll t=pow(x,a>>1);
t*=t;
if(a&1) t*=x;
return t;
}
inline int prty(char c)//priority
{
switch(c)
{
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':return 3;
case '(':return 0;
default :return -1;
}
}
inline ll calc(ll b,char c,ll a)
{
switch(c)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
case '^':return pow(a,b);
}
}
int main()
{
freopen("formula.in","r",stdin);
freopen("formula.out","w",stdout);
scanf("%s",ch+1);
len=strlen(ch+1);
ch[0]='(',ch[++len]=')';
for(int i=0;i<=len;++i)
{
if((ch[i]=='-'&&ch[i-1]=='(')||ch[i]>='0'&&ch[i]<='9')
{
bool f=0;
if(ch[i]=='-')
f=1,++i;
ll t;
for(t=0;ch[i]>='0'&&ch[i]<='9';++i)
t=(t<<1)+(t<<3)+ch[i]-'0';
--i;
if(f) t*=-1;
sta[++tp]=t;
}
else if(ch[i]==')')
{
while(opt[tpo]!='(')
{
ll t=calc(sta[tp],opt[tpo],sta[tp-1]);
--tp,--tpo;
sta[tp]=t;
}
--tpo;
}
else
{
while(ch[i]!='('&&prty(opt[tpo])>=prty(ch[i]))
{
ll t=calc(sta[tp],opt[tpo],sta[tp-1]);
--tp,--tpo;
sta[tp]=t;
}
opt[++tpo]=ch[i];
}
}
printf("%lld",sta[1]);
fclose(stdin);
fclose(stdout);
return 0;
}