题目说了很多,但方法很简单,就是变换一下‘+’和‘*’的优先级,然后分别输出就OK!注意long long 就没问题了!
水过~~
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<algorithm>
using namespace std;
char str[1000],oper[1000];
long long num[1000];
int len;
long long Complete(char first,char last)
{
int top = 0;
memset(num,0,sizeof(num));
for(int i = 0;i < len;i++)
{
if(isdigit(str[i]))
num[top] = num[top] * 10 + (str[i]-'0');
else if(str[i] == first)
{
int nextnum = 0,j = i+1;
bool flag = true;
for( ;j < len;j++)
{
if(isdigit(str[j]))
{
nextnum = nextnum * 10 + (str[j]-'0');
flag = false;
}
else if(!flag) break;
}
i = j-1;
num[top] = first=='+' ? num[top] + nextnum : num[top] * nextnum;
}
else if(str[i]==last) top++;
}
long long res;
if(first=='+')
{
res = 1;
for(int i = 0;i <= top;i++)
res *= num[i];
}
else
{
res = 0;
for(int i = 0;i <= top;i++)
res += num[i];
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int cases;
scanf("%d",&cases);
getchar();
while(cases--)
{
gets(str);
len = strlen(str);
printf("The maximum and minimum are %lld and %lld.\n",Complete('+','*'),Complete('*','+'));
}
return 0;
}