下面的程序求出结果后只能运行一次。
#include<iostream.h>
#include<math.h>
void main()
{
double a,b,c,d,e,x1,x2;
cout<<"请输入要求解的一元二次方程:"<<endl;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<a<<"*x*x"<<"+"<<b<<"*x"<<"+"<<c<<"=0"<<endl;
d=b*b-4*a*c;
if(d<0)
{
cout<<"There is no x."<<endl;
}
if(d==0)
{
cout<<"There is only x."<<endl;
x1=x2=(-b)/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
}
參考答案:int main()
{
while (true)
{
//计算
//。。。
//询问用户是否退出
//如果是就退出
}
}