給定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