當前位置:首頁 » 編程語言 » c語言積分怎麼編代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言積分怎麼編代碼

發布時間: 2022-05-20 01:25:57

❶ 積分在c語言里怎麼實現

10^3
=1000在c語言里是不對的,^在c語言里是按位異或運算符。。lz應該是把vb和c弄混了吧。。vb中10^3
=1000是對的。。
c語言中,10的3次方是1e3,但用e來表示10的次方前提是e前後都是常數,若lz的a在之前被定義為常數,則10ea是對的,不然則要通過循環或函數來實現。。
簡單一點函數
pow10(a)就可以表示10的a次方,但是這樣用,前面一定要加#include「math.h」,因為這個函數是定義在這個頭文件之下的。。
lz念在我大晚上,,還是情人節大晚上給你碼字的份上,給我最佳答案吧。。。

❷ 怎麼用C語言表示積分

源代碼如下:

#include#includefloat f1(float x)

{

return(1.0+x);

}

float f2(float x)

{

return(2.0*x+3.0);

}

float f3(float x)
{
return(exp(x)+1);
}

float f4(float x)
{
return(pow(1+x,2));

}

float f5(float x)
{

return(pow(x,3));

}

float fsimp(float a,float b,float (*p)(float))

{

float c,s;

c=(a+b)/2;

s=(b-a)/6*(p(a)+4*p(c)+p(b));

return s;

}

int main()

{

float a,b;

printf("請輸入積分下限a的值:");

scanf("%f",&a);

printf("請輸入積分上限b的值:");

scanf("%f",&b);

printf("%f ",fsimp(a,b,f1));

}

(2)c語言積分怎麼編代碼擴展閱讀

1、對應於一個積分式要有一段程序,可以改變程序的一小部分來改變所要求的積分式。

2、除數不能位0。

3、兩個整數相除,結果仍是整數。

4、若被除數其中有一個為浮點數或者兩個都為浮點數,則結果為浮點類型。操作數必須為整數,不能是浮點數。

❸ 怎樣編寫c語言積分函數

積分分為兩種,數值積分,公式積分。

  1. 公式積分:部分函數可以直接用公式求得其不定積分函數。C語言中可以直接用積分公式寫出其積分函數。

  2. 數值積分:按照積分的定義,設置積分范圍的步長,用梯形面積累加求得其積分。

    以【f(x)=x*sin(x) 從1到2的積分】為例:

    #include<math.h>
    #include<stdio.h>
    doubleintegral(double(*fun)(doublex),doublea,doubleb,int,n){
    doubles,h,y;
    inti;
    s=(fun(a)+fun(b))/2;
    h=(b-a)/n;/*積分步長*/
    for(i=1;i<n;i++)
    s=s+fun(a+i*h);
    y=s*h;
    returny;/*返回積分值*/
    }
    doublef(doublex){
    return(x*sinx)/*修改此處可以改變被積函數*/
    }
    intmain(){
    doubley;
    y=integral(f,1.0,2.0,150);/*修改此處可以改變積分上下限和步數,步長=(上限-下限)/步數*/
    printf("y=%f ",y);
    return0;
    }

❹ 在C語言中,積分函數怎麼寫

把積分算式算出來,然後用C寫。
比如[a,b]的積分上限和下限,對x積分,就是x^2/2, 代入a,b

❺ 用C語言編寫一個求定積分的程序,用它分別求以下5個函數的定積分:

#include<stdio.h>
#include<math.h>
float f1(float x)
{
return(1.0+x);
}

float f2(float x)
{
return(2.0*x+3.0);
}

float f3(float x)
{
return(exp(x)+1);
}

float f4(float x)
{
return(pow(1+x,2));
}

float f5(float x)
{
return(pow(x,3));
}

float fsimp(float a,float b,float (*p)(float))
{
float c,s;
c=(a+b)/2;
s=(b-a)/6*(p(a)+4*p(c)+p(b));
return s;
}

int main()
{
float a,b;
printf("請輸入積分下限a的值:");
scanf("%f",&a);
printf("請輸入積分上限b的值:");
scanf("%f",&b);
printf("%f\n",fsimp(a,b,f1));
printf("%f\n",fsimp(a,b,f2));
printf("%f\n",fsimp(a,b,f3));
printf("%f\n",fsimp(a,b,f4));
printf("%f\n",fsimp(a,b,f5));
}

❻ 用c語言編寫求一個定積分

給一個辛普森求積分的例子。


1. 關於辛普森法(Simpson『s Rule)[1]

辛普森法是數值方法求積分的一種方法,它與梯形法和矩形法不同,辛普森法用二次曲線去逼近實際的積分,如下圖所示:

#include<stdio.h>
#include<math.h>

typedefdouble(*funcptr)(doublea);

doubleintegrate_simpson(funcptrf,floatlower,floatupper,intn)
{
inti;
floata,b;
floath;
doubleresult;

printf("%f%f%d ",lower,upper,n);
h=(upper-lower)/n;
printf("%f ",h);

result=0;
a=lower;
b=a+h;
for(i=0;i<n;i++)
{
doubletmp;

tmp=(b-a)/6*(f(a)+4*f((a+b)/2)+f(b));
printf("%f%f%f ",a,b,tmp);
result=result+tmp;
a=b;
b=a+h;
}
returnresult;
}

doublef(doublea)
{
returnexp(a);
}

voidmain(void)
{
doublere;
re=integrate_simpson(f,0,1,100);
printf("%f ",re);
}


3. 輸出樣式

moose@debian:~$ ./a.out
0.000000 1.000000 100
0.010000
0.000000 0.010000 0.010050
0.010000 0.020000 0.010151
0.020000 0.030000 0.010253
0.030000 0.040000 0.010356
0.040000 0.050000 0.010460
0.050000 0.060000 0.010565
0.060000 0.070000 0.010672
0.070000 0.080000 0.010779
0.080000 0.090000 0.010887
0.090000 0.100000 0.010997
0.100000 0.110000 0.011107
0.110000 0.120000 0.011219
0.120000 0.130000 0.011332
0.130000 0.140000 0.011445
0.140000 0.150000 0.011560
0.150000 0.160000 0.011677
0.160000 0.170000 0.011794
0.170000 0.180000 0.011913
0.180000 0.190000 0.012032
0.190000 0.200000 0.012153
0.200000 0.210000 0.012275
0.210000 0.220000 0.012399
0.220000 0.230000 0.012523
0.230000 0.240000 0.012649
0.240000 0.250000 0.012776
0.250000 0.260000 0.012905
0.260000 0.270000 0.013034
0.270000 0.280000 0.013165
0.280000 0.290000 0.013298
0.290000 0.300000 0.013431
0.300000 0.310000 0.013566
0.310000 0.320000 0.013703
0.320000 0.330000 0.013840
0.330000 0.340000 0.013979
0.340000 0.350000 0.014120
0.350000 0.360000 0.014262
0.360000 0.370000 0.014405
0.370000 0.380000 0.014550
0.380000 0.390000 0.014696
0.390000 0.400000 0.014844
0.400000 0.410000 0.014993
0.410000 0.420000 0.015144
0.420000 0.430000 0.015296
0.430000 0.440000 0.015450
0.440000 0.450000 0.015605
0.450000 0.460000 0.015762
0.460000 0.470000 0.015920
0.470000 0.480000 0.016080
0.480000 0.490000 0.016242
0.490000 0.500000 0.016405
0.500000 0.510000 0.016570
0.510000 0.520000 0.016736
0.520000 0.530000 0.016905
0.530000 0.540000 0.017075
0.540000 0.550000 0.017246
0.550000 0.560000 0.017419
0.560000 0.570000 0.017595
0.570000 0.580000 0.017771
0.580000 0.590000 0.017950
0.590000 0.600000 0.018130
0.600000 0.610000 0.018313
0.610000 0.620000 0.018497
0.620000 0.630000 0.018683
0.630000 0.640000 0.018870
0.640000 0.650000 0.019060
0.650000 0.660000 0.019251
0.660000 0.670000 0.019445
0.670000 0.680000 0.019640
0.680000 0.690000 0.019838
0.690000 0.700000 0.020037
0.700000 0.710000 0.020239
0.710000 0.720000 0.020442
0.720000 0.730000 0.020647
0.730000 0.740000 0.020855
0.740000 0.750000 0.021064
0.750000 0.760000 0.021276
0.760000 0.770000 0.021490
0.770000 0.780000 0.021706
0.780000 0.790000 0.021924
0.790000 0.800000 0.022144
0.800000 0.810000 0.022367
0.810000 0.820000 0.022592
0.820000 0.830000 0.022819
0.830000 0.839999 0.023048
0.839999 0.849999 0.023280
0.849999 0.859999 0.023514
0.859999 0.869999 0.023750
0.869999 0.879999 0.023989
0.879999 0.889999 0.024230
0.889999 0.899999 0.024473
0.899999 0.909999 0.024719
0.909999 0.919999 0.024968
0.919999 0.929999 0.025219
0.929999 0.939999 0.025472
0.939999 0.949999 0.025728
0.949999 0.959999 0.025987
0.959999 0.969999 0.026248
0.969999 0.979999 0.026512
0.979999 0.989999 0.026778
0.989999 0.999999 0.027047
1.718280


另:

matlab中用內置的int求積分的結果:

1.718281828459046

matlab中用前述simpson法求積分的結果:

1.718281828465257


4. 注意

本例中直接調用了標准math庫里的exp(),實際上可以通過改寫函數f(),將exp也做數值求解。


❼ C語言編程如何編寫積分公式

#include<iostream.h>
//定義結構類型
structstudent
{
intnum;
charname[20];
floatgrade;
};
voidmain(void)
{
//聲明數組
inti,size;
charstr[]="Thisisastring.";
intint_values[]={51,23,2,44,45,0,11};
floatfloat_values[]={15.1,13.3,22.2,10.4,1.5};
studentst_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};

//顯示char類型數組元素及其大小
size=sizeof(str)/sizeof(char);
cout<<"Numberofelementsinstr:";
cout<<size<<endl;
for(i=0;i<size;i++){
cout<<str[i];
}
cout<<endl;

//顯示int類型數組元素及其大小
size=sizeof(int_values)/sizeof(int);
cout<<"Numberofelementsinint_values:";
cout<<size<<endl;
for(i=0;i<size;i++){
cout<<int_values[i]<<"";
}
cout<<endl;

//顯示float類型數組元素及其大小
size=sizeof(float_values)/sizeof(float);
cout<<"Numberofelementsinfloat_values:";
cout<<size<<endl;
for(i=0;i<size;i++){
cout<<float_values[i]<<"";
}
cout<<endl;

//顯示student類型數組元素及其大小
size=sizeof(st_arr)/sizeof(student);
cout<<"Numberofelementsinst_arr:";
cout<<size<<endl;
for(i=0;i<size;i++){
cout<<st_arr[i].num<<"";
cout<<st_arr[i].name<<"";
cout<<st_arr[i].grade<<endl;
}
}
#include<iostream.h>
//add()函數的定義,其有返回值
doubleadd(doublex,doubley)
{
doublez;
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl;
return(z);
}

main()
{
doublea=0.5,b=1.0;

//以不同參數形式調用函數add()
cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl;
cout<<"add(a,b)="<<add(a,b)<<endl;
cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl;
cout<<"----------------------"<<endl;

//以表達式方式調用函數add()
doublec=2*add(a,b);
cout<<"c="<<c<<endl;
cout<<"----------------------"<<endl;

//以語句式方式調用函數add()
add(2*a,b);
cout<<"----------------------"<<endl;

//用其他類型參數調用函數add()
intn=1,m=2;
cout<<"add("<<n<<","<<m<<")="<<add(n,m)<<endl;
}
#include<iostream.h>
//定義符號函數sgn(),其返回值為int類型
intsgn(doublex)
{
if(x>0)return(1);//返回出口1
if(x<0)return(-1);//返回出口2
return(0);//返回出口3
}
//main()函數定義
main()
{
doublex;
inti;
for(i=0;i<=2;i++){
cout<<"x=";
cin>>x;
cout<<"sgn("<<x<<")="<<sgn(x)<<endl;
}
}

❽ C語言:編程求出圖片上的定積分近似值,咋寫

代碼文本:

#include "stdio.h"

#include "math.h"

int main(int argc,char *argv[]){

double s,x,d=0.000001,pi=3.1415926,t=1/3.0;

for(s=0.0,x=pi/2;x<=pi;x+=d)

s+=sqrt(x)/(1-pow(x,t))*d;

printf("%f ",s);

return 0;

}

❾ 用C語言編寫一個求定積分的程序

這是辛普森積分法。
給你寫了fun_1( ),fun_2(),請自己添加另外幾個被積函數。
調用方法 t=fsimp(a,b,eps,fun_i);
a,b --上下限,eps -- 迭代精度要求。
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
double fun_1(double x)
{
return 1.0 + x ;
}
double fun_2(double x)
{
return 2.0 * x + 3.0 ;
}

double fsimp(double a,double b,double eps, double (*P)(double))
{
int n,k;
double h,t1,t2,s1,s2,ep,p,x;
n=1; h=b-a;
t1=h*(P(a)+P(b))/2.0;
s1=t1;
ep=eps+1.0;
while (ep>=eps)
{
p=0.0;
for (k=0;k<=n-1;k++)
{
x=a+(k+0.5)*h;
p=p+P(x);
}
t2=(t1+h*p)/2.0;
s2=(4.0*t2-t1)/3.0;
ep=fabs(s2-s1);
t1=t2; s1=s2; n=n+n; h=h/2.0;
}
return(s2);
}
void main()
{
double a,b,eps,t;
a=0.0; b=3.141592653589793238; eps=0.0000001;
// a definite integral by Simpson Method.
t=fsimp(a,b,eps,fun_1);
printf("%g\n",t);
t=fsimp(a,b,eps,fun_2);
printf("%g\n",t);
// ...
printf("\n Press any key to quit...");
getch();
}

❿ 怎麼用c語言寫一個算積分的程序啊

1、積分應該是整數吧。
2、int jifen()
{
// 實現你的積分代碼就行。
}