秦九韶算法好像
就這樣吧,分析過程是類似於迭代的方法實現的,
參考博文https://blog.csdn.net/qq_34907362/article/details/80066465
源碼
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
//#define X 3
int solve(string str);
int main(){
string str1 = "+4*3^3-5*3^2+6*3^1-7*3^0";
cout<<solve(str1);
return 0;
}
int solve(string str){
int len=str.size();
int x,a[1000],dex=0,temp=0,flag=1;
for(int i=0; i<len; i++){
if(str[i]>='0' && str[i]<='9'){
temp=str[i]-'0';
}
else if(str[i] == '+'){
temp=0;
flag=1;
continue;
}
else if(str[i] == '-'){
temp=0;
flag=-1;
continue;
}
else if(str[i] == '^'){
x=temp;
continue;
}
else{
a[dex++]=temp*flag;
temp=0;
}
}
temp=a[0];
for (int i=1; i<dex; i++) //如果這裏底數是-3 所以把 x 變爲-x即可
{
temp=temp*x+a[i];
}
return temp;
}