秦九韶算法好像
就这样吧,分析过程是类似于迭代的方法实现的,
参考博文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;
}