函数调用的问题!

treeheni 2008-03-09 03:28:40
如下程序,调用函数来求解二元一次方程得解。在我的电脑上运行时无乱数如什么数出现的结果只有一个:The root=0
还有就是会提示我重定义错误!
我的电脑是64位的。
请大家帮帮我,解决这个问题!!
#include<stdio.h>
#include<math.h>
float a,b,c,m;
void main()
{

printf("Input three index a,b,c please:\n");
scanf("%d,%d,%d",&a,&b,&c);
m=b*b-4*a*c;
if(m<0)
noroot();
else if(m>0)
tworoots();
else oneroot();

}
void noroot(void)
{
printf("There is no root when m<0");

}
void oneroot()
{
float root=-b/(2*a);
printf("The root=%d",root);
}
void tworoots()
{
float roots1=((-b)+sqrt(m))/(2*a);
float roots2=((-b)-sqrt(m))/(2*a);
printf("The two roots are:roots1=%d,roots2=%d",roots1,roots2);

}
...全文
160 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
treeheni 2008-03-23
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 softking1 的回复:]
不信我吗!?哈
[/Quote]
哥们经过你的调试后程序在我的机子上能够正常运行了!!!我还想问一下,这个提示是怎么回事:Type dismatch in redeclaration of“noroot”?
敬请指教!!!
softking1 2008-03-19
  • 打赏
  • 举报
回复
不信我吗!?哈
softking1 2008-03-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>
void noroot(void);
void oneroot();
void tworoots();
float a,b,c,m;
void main()
{

printf("Input three index a,b,c please:\n");
scanf("%f,%f,%f",&a,&b,&c);
m=b*b-4*a*c;
if(m <0)
noroot();
else if(m> 0)
tworoots();
else oneroot();

}
void noroot(void)
{
printf("There is no root when m <0");

}
void oneroot()
{
double root=-b/(2*a);
printf("The root=%f",root);
}
void tworoots()
{
double roots1=((-b)+sqrt(m))/(2*a);
double roots2=((-b)-sqrt(m))/(2*a);
printf("The two roots are:roots1=%f,roots2=%f",roots1,roots2);

}
我调过了!绝对没问题的哈
treeheni 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Minkey 的回复:]
scanf("%d,%d,%d",&a,&b,&c);
------------------------
scanf("%f,%f,%f",&a,&b,&c);

还有就是会提示我重定义错误!
---------------------------
C语言倾向于"相信"程序员,即它假定调用而未声明的函数程序员会在别的地方给出完备的定义.但它会假定这种
函数返回类型是int.所以说,楼主在main函数中调用oneroot()、noroot()、tworoot()时,编译器假定他们是
返回int类型的函数.所以在看到void oneroot()等定义…
[/Quote]
提示是这样的:Type dismatch in redeclaration of“noroot”。
treeheni 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Minkey 的回复:]
scanf("%d,%d,%d",&a,&b,&c);
------------------------
scanf("%f,%f,%f",&a,&b,&c);

还有就是会提示我重定义错误!
---------------------------
C语言倾向于"相信"程序员,即它假定调用而未声明的函数程序员会在别的地方给出完备的定义.但它会假定这种
函数返回类型是int.所以说,楼主在main函数中调用oneroot()、noroot()、tworoot()时,编译器假定他们是
返回int类型的函数.所以在看到void oneroot()等定义…
[/Quote]
Minkey 兄:
你对于函数返回类型的看法我是完全同意的。TC确实也提示我函数需要一个返回值(注意这只是一个警告!)。

void noroot(void)
{ *
printf("There is no root when m <0");
} 但是在上面函数的“*”处TC也确实提醒我是重定义错误,我在一个英文C网站上看过关于这个问题的大概解释,说可能是因为64位系统的原因。这个问题我是最困惑的,问过许多人不过都没有一个令人满意并解决这个问题。这个问题是急需解决的!!!!!!
treeheni 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Supercaller 的回复:]
其实楼主所给的题目是对二元一次方程求解而代码部分是求一元二次方程的解
[/Quote]
没怎么看明白~~~
treeheni 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 arong1234 的回复:]
主要错误是这一句
scanf("%d,%d,%d",&a,&b,&c);

注意:这样得输入假定后面三个参数是int*类型得,你传得是float*,输入进a,b,c不是你想要得东西

其次:输入时三个数必须以逗号分割。
[/Quote]
哥们叫你这么以提醒我真是恍然大悟啊,这个错误犯的太丢人了实在是~~~~~谢谢!!!
Minkey 2008-03-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 arong1234 的回复:]
楼上说的不对,这不是信任的问题,而是在于对于不定参数的返回值,从原理上就无法确定其参数类型,这是不得已而为的
当这个不定参数列表被访问时,实际上它才有下面代码访问
int * p = VA_ARG(ap; int*);
这样无论后面参数类型是啥,都被强制转换成int*,不会去判断真实类型。
[/Quote]
我想阿荣兄弟误会我的意思了.
我的意思是说,C中没有声明返回类型的函数将被默认为返回int.这跟函数的参数没有关系的.
至于你说的VA_ARG指的的应该是<stdarg.h>中的va_arg吧?这只是一个,
而不是一个函数,自然没有类型限制了:
<stdarg.h>
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
Supercaller 2008-03-09
  • 打赏
  • 举报
回复
其实楼主所给的题目是对二元一次方程求解而代码部分是求一元二次方程的解
Supercaller 2008-03-09
  • 打赏
  • 举报
回复
其实楼主所给的题目是对二元一次方程求解而代码部分是求一元二次方程的解
arong1234 2008-03-09
  • 打赏
  • 举报
回复
楼上说的不对,这不是信任的问题,而是在于对于不定参数的返回值,从原理上就无法确定其参数类型,这是不得已而为的
当这个不定参数列表被访问时,实际上它才有下面代码访问

int * p = VA_ARG(ap; int*);
这样无论后面参数类型是啥,都被强制转换成int*,不会去判断真实类型。
Minkey 2008-03-09
  • 打赏
  • 举报
回复
scanf("%d,%d,%d",&a,&b,&c);
------------------------
scanf("%f,%f,%f",&a,&b,&c);

还有就是会提示我重定义错误!
---------------------------
C语言倾向于"相信"程序员,即它假定调用而未声明的函数程序员会在别的地方给出完备的定义.但它会假定这种
函数返回类型是int.所以说,楼主在main函数中调用oneroot()、noroot()、tworoot()时,编译器假定他们是
返回int类型的函数.所以在看到void oneroot()等定义的时候会给出警告.不过,这可不是重定义.编译器顶多
会给出个返回类型不匹配的警告而已吧.
arong1234 2008-03-09
  • 打赏
  • 举报
回复
主要错误是这一句
scanf("%d,%d,%d",&a,&b,&c);

注意:这样得输入假定后面三个参数是int*类型得,你传得是float*,输入进a,b,c不是你想要得东西

其次:输入时三个数必须以逗号分割。
kboby246 2008-03-09
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>
float a,b,c,m;

void noroot(void);
void oneroot(void);
void tworoots(void);

int main( void)
{

printf("Input three index a,b,c please:\n");
scanf("%f,%f,%f",&a,&b,&c);
m=b*b-4*a*c;
printf("m=%f", m);
if(m <0)
noroot();
else if(m> 0)
tworoots();
else oneroot();

system("pause");
return 0;
}

void noroot(void)
{
printf("There is no root when m <0");
}

void oneroot(void)
{
float root=-b/(2*a);
printf("The root=%d",root);
}

void tworoots(void)
{
float roots1=((-b)+sqrt(m))/(2*a);
float roots2=((-b)-sqrt(m))/(2*a);
printf("The two roots are:roots1=%f,roots2=%f",roots1,roots2);

}

注意;scanf("%f,%f,%f",&a,&b,&c);注意;格式直接有逗号,所以输入时也应该加上,如输入;1,-5,6
或者改为scanf("%f%f%f",&a,&b,&c);
zeloas 2008-03-09
  • 打赏
  • 举报
回复
把函数实现添加到main函数前面,或者在main函数前添加函数声明
liujinxing 2008-03-09
  • 打赏
  • 举报
回复
#include <stdio.h> 
#include <math.h>
void noroot(void)
float a,b,c,m;
void main()
{

printf("Input three index a,b,c please:\n");
scanf("%d,%d,%d",&a,&b,&c);
m=b*b-4*a*c;
if(m <0)
noroot();
else if(m> 0)
tworoots();
else oneroot();

}
void noroot(void)
{
printf("There is no root when m <0");

}
void oneroot()
{
float root=-b/(2*a);
printf("The root=%d",root);
}
void tworoots()
{
float roots1=((-b)+sqrt(m))/(2*a);
float roots2=((-b)-sqrt(m))/(2*a);
printf("The two roots are:roots1=%d,roots2=%d",roots1,roots2);

}
qiuchengw 2008-03-09
  • 打赏
  • 举报
回复
调用函数前,函数必须是已经声明了。
用户 昵称 2008-03-09
  • 打赏
  • 举报
回复
// test9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <math.h>
float a,b,c,m;
void noroot(void)
{
printf("There is no root when m <0");

}
void oneroot()
{
float root=-b/(2*a);
printf("The root=%d",root);
}
void tworoots()
{
float roots1=( float )( ((-b)+sqrt(m))/(2*a) );
float roots2=( float )( ((-b)-sqrt(m))/(2*a) );
printf("The two roots are:roots1=%d,roots2=%d",roots1,roots2);

}
void main()
{

printf("Input three index a,b,c please:\n");
scanf("%f %f %f",&a,&b,&c);
m=b*b-4*a*c;
if(m <0)
noroot();
else if(m> 0)
tworoots();
else
oneroot();

}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧