函数问题
引用函数编写一个求两个电阻的串联值或并联值的程序。代码如下:
#include<stdio.h>
main()
{
float series(),parallel();
float r1,r2,rt;
char sp;
printf("Enter resistor r1,r2:");
scanf("%f %f",&r1,&r2);
printf("Then enter 's' or 'p':");
while((sp=getchar())=='\n')
;
if(sp=='s')
rt=series(r1,r2);
else if(sp=='p')
rt=parallel(r1,r2);
printf("The total resistance=%f ohms\n");
}
float series(r1,r2)
float r1,r2;
{
float rt;
rt=r1+r2;
return(rt);
}
float parallel(float r1,float r2)
{
float rt;
rt=(r1*r2)/(r1+r2);
return(rt);
}
在vc6中运行是得不到正确结果,在devcpp5中无法通过编译。
请问问题怎么解决,Thank you
----------------解决方案--------------------------------------------------------
函数没声明。
----------------解决方案--------------------------------------------------------
#include<stdio.h>
main()
{
float series(float r1,float r2);
float parallel(float r1,float r2);
float r1,r2,rt;
char sp;
printf("Enter resistor r1,r2:");
scanf("%f %f",&r1,&r2);
printf("Then enter 's' or 'p':");
sp=getchar();
if(sp=='s')
rt=series(r1,r2);
else if(sp=='p')
rt=parallel(r1,r2);
printf("The total resistance=%f ohms\n",rt);
getch();
}
float series(float r1,float r2)
{
float rt;
rt=r1+r2;
return(rt);
}
float parallel(float r1,float r2)
{
float rt;
rt=(r1*r2)/(r1+r2);
return(rt);
}
[[it] 本帖最后由 longxies 于 2008-2-5 18:59 编辑 [/it]]
----------------解决方案--------------------------------------------------------