给定a = 1,b = 5和c = 6的值,x的值应该是-2和-3,但下面的程序给出了x的值为6和-11,这是不正确的。如果有人能够弄清楚这个程序有什么问题,我会很感激。查找二次方程的x值的程序
#include<iostream.h>
#include<conio.h>
int main()
{
char reply;
int a,b,c,q,z;
do
{
cout<<"Enter the value of a: ";
cin>>a;
cout<<"\nEnter the value of b: ";
cin>>b;
cout<<"\nEnter the value of c: ";
cin>>c;
q=(-b-(b*b-4*a*c)sqrt(b))/2/a;
z=(-b+(b*b-4*a*c)sqrt(b))/2/a;
cout<<"\nThe values of x are "<<q<<" and "<<z;
cout<<"\nDo you want to find another values of x(y/n)?";
cin>>reply;
}
while(reply=='y');
getch();
return 0;
}
当你在调试器中运行你的代码时,你学到了什么,或者增加了额外的输出以显示中间计算的值? – 2011-12-18 15:33:24
如果'a'为零或非常小,会发生什么?如果用户输入字符串会发生什么? – 2011-12-18 15:35:45
*你认为'^'做了什么? – 2011-12-18 15:36:16