- 论坛徽章:
- 0
|
程序代码如下:
#include<stdio.h>
#include<math.h>
#include<limits.h>
#define pi 3.141592653
//正态函数式
float fun(float x)
{
float val;
val=(1/sqrt(2*pi))*exp(-x*x/2);
return val;
}
//使用梯形求积分的方法,其中a代表积分下限,b代表积分上限,n代表分得的梯形数目
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;
}
//循环完成正态分布数据的生成和存入
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);
}
//循环完成正态分布数据读入到文件中去。
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);
}
出现的问题如下:
Compiling...
01.cpp
D:\编程\正态分布\正态分布\01.cpp(10) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
D:\编程\正态分布\正态分布\01.cpp(26) : warning C4244: 'return' : conversion from 'float' to 'long', possible loss of data
D:\编程\正态分布\正态分布\01.cpp(47) : warning C4305: 'argument' : truncation from 'const unsigned long' to 'float'
D:\编程\正态分布\正态分布\01.cpp(47) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
D:\编程\正态分布\正态分布\01.cpp(47) : warning C4244: '=' : conversion from 'long' to 'float', possible loss of data
D:\编程\正态分布\正态分布\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
执行 cl.exe 时出错.
01.obj - 1 error(s), 0 warning(s)
请问,为什么使用fprintf函数,会出现“cannot convert from ‘int’ to ‘struct_iobuf’。”这样的问题呢?我在其他程序中使用fprintf函数也是这样写的,并没有出现这样的问题啊。有人说要强制转换类型,我想知道应该在哪里、怎么强制转换类型呢? |
|