Problem Description
We have learned how to obtain the value of a polynomial when we were a middle school student. If f(x) is a polynomial of degree n, we can let
If we have x, we can get f(x) easily. But a computer can not understand the expression like above. So we had better make a program to obtain f(x).
Input
There are multiple cases in this problem and ended by the EOF. In each case, there are two lines. One is an integer means x (0<=x<=10000), the other is an expression means f(x). All coefficients ai(0<=i<=n,1<=n<=10,-10000<=ai<=10000) are integers. A correct expression maybe likes
1003X5+234X4-12X3-2X2+987X-1000
Output
For each test case, there is only one integer means the value of f(x).
Sample Input
3
1003X5+234X4-12X3-2X2+987X-1000
Sample Output
264302
Notice that the writing habit of polynomial f(x) is usual such as
X6+2X5+3X4+4X3+5X^2+6X+7
-X7-5X6+3X5-5X4+20X3+2X2+3X+9
X+1
X^3+1
X^3
-X+1 etc. Any results of middle process are in the range from -1000000000 to 1000000000.
我是参考别人的代码,我只能说,我需要好好的学习 啊
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const nMax = 10100;
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
char s[nMax];
int x;
#define ll long long
ll go(int &i)
{
int a,b,c;
a = 1;
b = 0;
if(s[i] == '-') a*=-1,i++;
if(s[i] == '+') i++;
while(s[i]>='0' && s[i]<='9')
{
b = b*10 + s[i]-'0';
i++;
}
if(b==0) b = 1;//这句就是应证X+1
c = 0;
if(s[i] == 'X')
{
i++;
if(s[i]=='^')
{
i++; c = 0;
while(s[i]>='0' && s[i]<='9')
{
c = c*10 + s[i] - '0';
i ++;
}
}
else
{
c = 1;
}
}
ll ret = 0;
ret = (ll)a*b;
for(int i=0; i<c; i++) ret *= x;
return ret;
}
int main()
{
while(cin >> x >> s)
{
int i = 0;
int l = strlen(s);
ll ans = 0;
while(i<l)
{
ans += go(i);
}
cout << ans << endl;
}
return 0;
}