本文目錄一覽:
- 1、C語言如何實現積分運算
- 2、用C語言求積分
- 3、C語言編程如何編寫積分公式
C語言如何實現積分運算
#includestdio.h
#includemath.h
double integ(double a,double b)
{
double s,x,h;
int n=100,i;
h=fab(b-a)/n;
s=(sin(a)+sin(b))/2.0;
for(i=1;i=n-1;i++)
{
x=a+i*h;
s=s+sin(x);
}
s=s*h;
return s;
}
main()
{
double s;
s=integ(0.0,0.15);
printf(“s=%f\n”,s);
}
你自己跑下,可能有語法錯誤。呵呵。。
用C語言求積分
基本是這樣的,用梯形發求定積分,對應於一個積分式就要有一段程序,不過你可以改變程序的一小部分來改變你所要求的積分式。
以c為例:求f(x)=xsinx從1到2的積分
#include math.h
float integral(float(*fun)(float x),float a,float b,int,n)
{float s,h,y;
int i;
s=(fun(a)+fun(b))/2;
h=(b-a)/n; /*積分步長*/
for(i=1;in;i++)
s=s+fun(a+i*h);
y=s*h;
return y;/*返回積分值*/
}
float f(float x)
{return(x*sinx) /*修改此處可以改變被積函數*/
}
main()
{float y;
y=integral(f,1.0,2.0,150);/*修改此處可以改變積分上下限和步長*/
printf(“y=%f\n”,y);
}
C語言編程如何編寫積分公式
#includeiostream.h
//定義結構類型
struct student
{
int num;
char name[20];
float grade;
};
void main(void)
{
//聲明數組
int i,size;
char str[]=”This is a string.”;
int int_values[] = {51, 23, 2, 44, 45,0,11};
float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};
student st_arr[]={101,”WangLin”,92,102,”LiPing”,85,103,”ZhaoMin”,88};
//顯示char類型數組元素及其大小
size=sizeof(str) / sizeof(char);
cout”Number of elements in str: “;
coutsizeendl;
for(i=0;isize;i++) {
coutstr[i];
}
coutendl;
//顯示int類型數組元素及其大小
size=sizeof(int_values) / sizeof(int);
cout”Number of elements in int_values: “;
coutsizeendl;
for(i=0;isize;i++) {
coutint_values[i]” “;
}
coutendl;
//顯示float類型數組元素及其大小
size=sizeof(float_values) / sizeof(float);
cout”Number of elements in float_values: “;
coutsizeendl;
for(i=0;isize;i++) {
coutfloat_values[i]” “;
}
coutendl;
//顯示student類型數組元素及其大小
size=sizeof(st_arr) / sizeof(student);
cout”Number of elements in st_arr: “;
coutsizeendl;
for(i=0;isize;i++) {
coutst_arr[i].num” “;
coutst_arr[i].name” “;
coutst_arr[i].gradeendl;
}
}
#includeiostream.h
//add()函數的定義,其有返回值
double add(double x,double y)
{
double z;
z=x+y;
coutx”+”y”=”zendl;
return(z);
}
main()
{
double a=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()
double c=2*add(a,b);
cout”c=”cendl;
cout”———————-“endl;
//以語句式方式調用函數add()
add(2*a,b);
cout”———————-“endl;
//用其他類型參數調用函數add()
int n=1,m=2;
cout”add(“n”,”m”)=”add(n,m)endl;
}
#includeiostream.h
//定義符號函數sgn(),其返回值為int類型
int sgn(double x)
{
if (x0) return(1); //返回出口1
if (x0) return(-1); //返回出口2
return(0); //返回出口3
}
//main()函數定義
main()
{
double x;
int i;
for (i=0;i=2;i++) {
cout”x=”;
cinx;
cout”sgn(“x”)=”sgn(x)endl;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/199050.html