作业帮 > 综合 > 作业

C语言解1元二次方程 查错.

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/06/28 06:40:46
C语言解1元二次方程 查错.
代码如下:
#include
#include
main()
{
int a,b,c,x1,x2,y,z;
printf("please input coefficient");
scanf("%d%d%d",&a,&b,&c);
y=b*b-4*a*c;
if(y>=0)
{
z=sprt(y);
x1=(-b+z)/2a;
x2=(-b-z)/2a;
printf("the solution is x1=%d, x2=%d." x1 x2);
}
else
printf("there is no any solution");
getch();



}
开始用的DEV-C++.按F9,出现编译中对话框,但是等了许久都没结束.
然后又换WIN-TC复制代码编译,发现错误,可是我又不知道错在哪里了.
顺便问一下,为什么WIN-TC里面要加
getch();
才行啊?
DEV里都不用加.
C语言解1元二次方程 查错.
#include
#include
main()
{
int a,b,c,y;
double x1,x2,z;
printf("please input coefficient");
scanf("%d%d%d",&a,&b,&c);
y=b*b-4*a*c;
if(y>=0)
{
z=sprt(y);
x1=(-b+z)/(2*a);
x2=(-b-z)/(2*a);
printf("the solution is x1=%lf,x2=%lf.\n",x1,x2);
}
else
printf("there is no any solution");
getch();
}
z是计算根号应该定义成double型,相应的x1,x2也应定义成double型,x1中的2*a应该加括号,不然就变成先计算(-b+z)/2,然后再乘以a了;还有就是输出语句x1,x2中少了逗号.修改后你再编译一下看行不行!