C语言 关于调用函数出现error 不允许 dllimport的问题

#include <stdio.h>
#include <stdlib.h>
char Menu (void);
float sum (float a1,float b1);
float sub (float a2,float b2);
float mul (float a3,float b3);
float div (float a4,float b4);
main()
{
char ch;
float sum1,sub1,mul1,div1,x,y;
printf ("Please choose:\n");
Menu();
while ((ch=getchar())!='#')
{
fflush (stdin);

switch (ch)
{
case '1':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
sum1= sum(x,y);
printf ("so the result is %f\n",sum1);
break;
case '2':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
sub1= sub(x,y);
printf ("so the result is %f\n",sub1);
break;
case '3':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
mul1= mul(x,y);
printf ("so the result is %f\n",mul1);
break;
case '4':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
div1= div(x,y);
printf ("so the result is %f\n",div1);
break;
}
fflush (stdin);
Menu();
}
system ("pause");
}
char Menu (void)
{
printf (" 1 加法\n");
printf (" 2 减法\n");
printf (" 3 乘法\n");
printf (" 4 除法\n");
printf (" # 结束\n");
}
float sum (float a1,float b1)
{
float sum;
sum=a1+b1;
return sum;
}
float sub (float a2,float b2)
{
float sub;
sub=a2-b2;
return sub;
}
float mul (float a3,float b3)
{
float mul;
mul=a3*b3;
return mul;
}
float div (float a4,float b4)
{
float div;
div=a4/b4;
return div;
}
问题错误错误 error C2371: “div”: 重定义;不同的基类型
error C2491: “div”: 不允许 dllimport 函数 的定义
warning C4028: 形参 1 与声明不同

但是当我把div 修改成di的时候这个程序都对了 请教下div要怎么定义

div函数已经在系统中定义过了,可以在stdlib.h头文件中找到函数声明为:
_CRTIMP div_t __cdecl div(int, int);
因此,要把函数名改成其他的名字,如div_f。另外,对于除法,还要考虑分母为0的情况。修改后的代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TINY 0.001
#define LARGE 10000000
void Menu (void);
float sum (float a1,float b1);
float sub (float a2,float b2);
float mul (float a3,float b3);
float div_f (float a4,float b4);
main()
{
char ch;
float sum1,sub1,mul1,div1,x,y;
printf ("Please choose:\n");
Menu();
while ((ch=getchar())!='#')
{
fflush (stdin);

switch (ch)
{
case '1':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
sum1= sum(x,y);
printf ("so the result is %f\n",sum1);
break;
case '2':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
sub1= sub(x,y);
printf ("so the result is %f\n",sub1);
break;
case '3':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
mul1= mul(x,y);
printf ("so the result is %f\n",mul1);
break;
case '4':
printf ("Please input two numbers:");
scanf ("%f%f",&x,&y);
div1= div_f(x,y);
printf ("so the result is %f\n",div1);
break;
}
fflush (stdin);
Menu();
}
system ("pause");
}
void Menu (void)
{
printf (" 1 加法\n");
printf (" 2 减法\n");
printf (" 3 乘法\n");
printf (" 4 除法\n");
printf (" # 结束\n");
}
float sum (float a1,float b1)
{
float sum;
sum=a1+b1;
return sum;
}
float sub (float a2,float b2)
{
float sub;
sub=a2-b2;
return sub;
}
float mul (float a3,float b3)
{
float mul;
mul=a3*b3;
return mul;
}
float div_f (float a4,float b4)
{
float div;
if(fabs(b4)<TINY)
div=LARGE;
else
div=a4/b4;
return div;
}追问

前面那个我懂了 也就是说在程序中出现了div这个代码 所以重复吗
还有if(fabs(b4)<TINY)
div=LARGE;
只有fabs又是什么意思?麻烦你讲解一下 谢谢

追答

fabs()函数是对浮点型数据求绝对值,如果对整型数据求绝对值,就使用abs()函数。
if(fabs(b4)<TINY)语句是当b4比一个很小的数都小时,则认为b4为0,因为浮点型数据不能直接与0比较。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-01
函数div跟变量div重名了,两个之一换个名字就哦了~追问

你能说详细一点吗 因为其他的几个函数怎么都没有错呀 我还是这样命名的 我也没看到重名的呀?

第2个回答  2011-11-01
函数div跟变量div重名了
相似回答