思路:
網(wǎng)站建設公司,為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制網(wǎng)站建設服務,專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對成都宣傳片制作等多個行業(yè)擁有豐富的網(wǎng)站建設經(jīng)驗的網(wǎng)站建設公司。專業(yè)網(wǎng)站設計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)seo優(yōu)化排名優(yōu)化,H5建站,響應式網(wǎng)站。
因為這是個加法的多項式,用變量s累加,s的最初值是0,第1次向上加1!,第2次加2!,...,第n磁加n! s+=jc;階乘變量jc在循環(huán)中計算。
每項用循環(huán)變量i控制,第1次循環(huán)取值1,第2次循環(huán)取值2,...,第20次循環(huán)取值20,這個變量就是要求的階乘數(shù),i的值每次加1
階乘是個累乘的運算,用變量jc,初始值是1,第1次向上乘以循環(huán)變量值1,就是1!,第2次是前邊計算好的1!*2=2!,...,第20次就是19!*20=20!,jc*=i;
由于20!數(shù)據(jù)比較大,對于32bit的int型變量存放不下,可以考慮用double型數(shù)據(jù)計算,省得溢出。
因此程序主體算法為:
int i;
double s,jc;
s=0; jc=1;
for ( i=1;i=20;i++ ) { jc*=i; s+=jc; }
printf("%lf\n",s);
double PostCalculate::calculate(std::string *p,int size)
{
std::stackstring a;
std::string postExp[20];
int count=0; //對 后綴表達式 計數(shù)
for(int i=0;isize;i++)
{
if(!isSign(p)) //若不是符號 ,即為數(shù)字
{
postExp[count]=*p; //將數(shù)字直接放入后綴表達式
count++;
}
else if(*p=="+" ||*p=="-")
{
while(!a.empty() a.top()!="(") //彈出棧中元素直到 棧空 或者 左括號
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p); //將當前元素壓棧
}
else if(*p=="*" || *p=="/")
{
while(!a.empty() a.top()!="(" a.top()!="+" a.top()!="-")
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p);
}
else if(*p=="(")
{
a.push(*p);
}
else if(*p==")")
{
while(!a.empty() a.top()!="(")
{
postExp[count]=a.top();
count++;
a.pop();
}
if(!a.empty() a.top()=="(")
a.pop();
}
p++;
}
while(!a.empty())
{
postExp[count++]=a.top();
a.pop();
}
double result=postCal(postExp,count);
return result;
}
std::string PostCalculate::dispose(std::string first, std::string second, std::string op)
{
stringstream t;
double r;
double a=stringToDouble(first);
double b=stringToDouble(second);
if(op=="+")
r=a+b;
else if(op=="-")
r=a-b;
else if(op=="*")
r=a*b;
else if(op=="/")
r=a/b;
tr;
return t.str();
}
double PostCalculate::postCal(std::string *p,int size)
{
std::stackstring res;
for(int i=0;isize;i++)
{
if(!isSign(p)) //遇到數(shù)字 壓棧
res.push(*p);
else //遇到符號
{
string f=res.top(); //取第2操作數(shù)
res.pop();
string s=res.top();//取第1操作數(shù)
string r=dispose(s,f,*p);
res.push(r);
}
p++;
}
return stringToDouble(res.top());
}
inline bool PostCalculate::isSign(std::string* s)
{
return (*s=="+"|| *s=="-"|| *s=="*"|| *s=="/"|| *s=="("|| *s==")");
}
inline double PostCalculate::stringToDouble(std::string c) //通過流 實現(xiàn)轉(zhuǎn)換 String - Double
{
double temp;
stringstream a;
a.str(c);
atemp;
return temp;
}
棧的運用…建立兩個棧一個存數(shù)字一個存運算符…然后取出運算符然后比較優(yōu)先級…就是這個的思路…求采納……
應該是計算機上浮點數(shù)導致的吧。因為浮點數(shù)比較跟整數(shù)比較不一致的。
#include
stdio.h
double
polynomial(double
x)
{
return
4*x*x*x
-
x*x
+
7;
}
const
double
EPS=1e-5;
main()
{
double
x,
minx=1,
maxx=2,
step=0.1;
for(x
=
minx;
x
maxx+EPS;
x+=step)
{
printf("polynomial(%.2f)
=
%.2f\n",
x,
polynomial(x));
}
}
C:\test.exe
polynomial(1.00)
=
10.00
polynomial(1.10)
=
11.11
polynomial(1.20)
=
12.47
polynomial(1.30)
=
14.10
polynomial(1.40)
=
16.02
polynomial(1.50)
=
18.25
polynomial(1.60)
=
20.82
polynomial(1.70)
=
23.76
polynomial(1.80)
=
27.09
polynomial(1.90)
=
30.83
polynomial(2.00)
=
35.00
#include
int main(void)
{
int n;
int i,j;
float sum = 1.0;
float dex = 1.0;
printf("輸入一個數(shù):");
scanf("%d",n);
for (i = 2; i 0; --j)
{
dex*=j;
}
sum+=1/dex;
dex = 1.0;
}
printf("%.2f\n",sum);
return 0;
}
當前文章:c語言求多項式函數(shù)的值 c語言編程求多項式的值
分享URL:http://chinadenli.net/article46/dodoieg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、微信公眾號、面包屑導航、外貿(mào)建站、網(wǎng)站制作、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)