关于c语言子函数的问题?
题目是编一个程序,求两数的和,差,积,商。并分别用四个子函数运算,在主函数中调用。我写的程序原代码如下:
include <stdio.h>
#include <dos.h>
main()
{
float jia(float,float);
float jian(float,float);
float cheng(float,float);
float chu(float,float);
float a,b,c;
printf("Input a and b:\n ");
scanf("%f %f,&a,&b\n");
c=jia (a,b);
printf("%f+%f=%f\n",a,b,c);
c=jian(a,b);
printf("%f-%f=%f\n",a,b,c);
c=cheng(a,b);
printf("%f*5f=%f",a,b,c);
if (b!=0)
{c=chu(a,b);
printf("%f/%f=%f",a,b,c);
}
else printf("The b is zero!");
}
float jia(float x,float y)
{
float h;
h=x+y;
return(h);
}
float jian(float xa,float ya)
{
float ha;
ha=xa-ya;
return (ha);
}
float cheng(float xb,float yb)
{
float hb;
hb=xb*yb;
return (hb);
}
float chu(float xc,float yc)
{
float hc;
hc=xc/yc;
return (hc);
}
编译时出现警告提示:a,b可以在定义以前被main()调用。
这是什么原因啊,哪里不对了?
参考答案:#include <stdio.h>
#include <dos.h>
//main()
void main() //我的编译器需要用void
{
float jia(float,float);
float jian(float,float);
float cheng(float,float);
float chu(float,float);
float a,b,c;
printf("Input a and b:\n ");
//scanf("%f %f,&a,&b\n");
scanf("%f %f",&a,&b);//格式!注意""的位置;scanf中不能有\n吧?
c=jia (a,b);
printf("%f+%f=%f\n",a,b,c);
c=jian(a,b);
printf("%f-%f=%f\n",a,b,c);
c=cheng(a,b);
//printf("%f*5f=%f",a,b,c);
printf("%f*%f=%f\n",a,b,c);//“5”-〉“%”;再来个换行吧
//if (b!=0) {c=chu(a,b);
if (b!=0) {c=chu(a,b);//";"用半角
printf("%f/%f=%f",a,b,c);}
else printf("The b is zero!");
}
float jia(float x,float y)
{
float h;
h=x+y;
return(h);
}
float jian(float xa,float ya)
{
float ha;
ha=xa-ya;
return (ha);
}
float cheng(float xb,float yb)
{
float hb;
hb=xb*yb;
return (hb);
}
float chu(float xc,float yc)
{
float hc;
hc=xc/yc;
return (hc);
}
//你说我容易吗?