- 論壇徽章:
- 0
|
程序代碼如下:
#include<stdio.h>
#include<math.h>
#include<limits.h>
#define pi 3.141592653
//正態(tài)函數(shù)式
float fun(float x)
{
float val;
val=(1/sqrt(2*pi))*exp(-x*x/2);
return val;
}
//使用梯形求積分的方法,其中a代表積分下限,b代表積分上限,n代表分得的梯形數(shù)目
long integral(float(*fun)(float x),float a,float b, float n){
float s,h,y;
int i;
//步長
h=(b-a)/n;
s=0;
y=0;
//利用離散求和求積分
for(i=0;i<n;i++)
s+=(fun(a+(i+1)*h)+fun(a+i*h))/2;
y=s*h;
return y;
}
main()
{
int i,j;
float val[32][10];
FILE *fp;
fp=fopen("D:\01.xls","w");
//文件打開出錯處理
if(fp==NULL)
{printf("fail to open file!\n");
return -1;
}
//循環(huán)完成正態(tài)分布數(shù)據(jù)的生成和存入
for(j=0;(-3.0+j*0.1)<=3.0;j++)
for(i=0;i<10;i++){
val[j][i]=integral(fun,LONG_MIN,(-3.0+j*0.1+0.01*i),ULONG_MAX);
}
//循環(huán)完成正態(tài)分布數(shù)據(jù)讀入到文件中去。
for(j=0;(-3.0+0.1*j)<=3.0;j++){
for(i=0;i<10;i++)
{
fprintf(fp,"%c\t",val[j][i]);
}
fp=fprintf(fp,"\n");
}
fclose(fp);
}
出現(xiàn)的問題如下:
Compiling...
01.cpp
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(10) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(26) : warning C4244: 'return' : conversion from 'float' to 'long', possible loss of data
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(47) : warning C4305: 'argument' : truncation from 'const unsigned long' to 'float'
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(47) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(47) : warning C4244: '=' : conversion from 'long' to 'float', possible loss of data
D:\編程\正態(tài)分布\正態(tài)分布\01.cpp(56) : error C2440: '=' : cannot convert from 'int' to 'struct _iobuf *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
執(zhí)行 cl.exe 時出錯.
01.obj - 1 error(s), 0 warning(s)
請問,為什么使用fprintf函數(shù),會出現(xiàn)“cannot convert from ‘int’ to ‘struct_iobuf’!边@樣的問題呢?我在其他程序中使用fprintf函數(shù)也是這樣寫的,并沒有出現(xiàn)這樣的問題啊。有人說要強(qiáng)制轉(zhuǎn)換類型,我想知道應(yīng)該在哪里、怎么強(qiáng)制轉(zhuǎn)換類型呢? |
|