大佬們,為什么這串代碼輸出結果什么也沒有?(萌新上路請多關照)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,x,y,z,x1,x2;
cin>>a>>b>>c;
y=b*b-4*a*c;
z=(-c)/b;
x1=((-b)+sqrt(y))/(2*a);
x2=((-b)-sqrt(y))/(2*a);
if(a!=0&&b!=0)
{ if(y>0)
{if(x1>x2)
cout<<"x1="<<x1<<",x2="<<x2<<endl;
else
x=x1,x1=x2,x2=x;
cout<<"x1="<<x1<<",x2="<<x2<<endl;
}
else
if(y==0)
cout<<"x="<<x1<<endl;
else
cout<<"無值"<<endl;
}
else
if(a==0&&b!=0)
cout<<"x="<<z<<endl;
else
cout<<"無值"<<endl;
return 0;
}
uj5u.com熱心網友回復:
供參考:#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,x,y,z,x1,x2;
cin>>a>>b>>c;
y=b*b-4*a*c;
if(a!=0&&b!=0)
{
x1=((-b)+sqrt(y))/(2*a);
x2=((-b)-sqrt(y))/(2*a);
if(y>0)
{
if(x1>x2)
cout<<"x1="<<x1<<",x2="<<x2<<endl;
else
{
x=x1,x1=x2,x2=x;
cout<<"x1="<<x1<<",x2="<<x2<<endl;
}
}
else
if(y==0)
cout<<"x="<<x1<<endl;
else
cout<<"無值"<<endl;
}
else
if(a==0&&b!=0)
{
z=(-c)/b;
cout<<"x="<<z<<endl;
}
else
cout<<"無值"<<endl;
return 0;
}
uj5u.com熱心網友回復:
修正樓上的:#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,x,y,z,x1,x2;
cin>>a>>b>>c;
y=b*b-4*a*c;
if(a!=0&&b!=0&&y>=0)
{
x1=((-b)+sqrt(y))/(2*a);
x2=((-b)-sqrt(y))/(2*a);
if(y>0)
{
if(x1>x2)
cout<<"x1="<<x1<<",x2="<<x2<<endl;
else
{
x=x1,x1=x2,x2=x;
cout<<"x1="<<x1<<",x2="<<x2<<endl;
}
}
else
cout<<"x="<<x1<<endl;
}
else
if(a==0&&b!=0)
{
z=(-c)/b;
cout<<"x="<<z<<endl;
}
else
cout<<"無值"<<endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268236.html
標籤:基礎類
