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里都不用加.
代码如下:
#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元二次方程 查错.](/uploads/image/z/8335741-13-1.jpg?t=C%E8%AF%AD%E8%A8%80%E8%A7%A31%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B+%E6%9F%A5%E9%94%99.)
#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中少了逗号.修改后你再编译一下看行不行!
#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中少了逗号.修改后你再编译一下看行不行!