#include<stdio.h>#include<math.h>float x(int a,int b,float d);float y(int a,int b,float d);int main(){int a,b,c;float d;scanf("%d%d%d",&a,&b,&c);d=b*b-4*a*c;if(d>=0){x(a,b,d);}else{y(a,b,d);}return 0;}float x(int a,int b,float d){float p,q;p=(-b+sqrt(d))/2/a;q=(-b-sqrt(d))/2/a;printf("x1=%.3f x2=%.3f",p,q);}float y(int a,int b,float d){float p,q;p=(-b)/2/a;q=sqrt(-d)/2/a;printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",p,q,p,q); }
2 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int a,b,c;
int t;
//t就是b^2-4ac判断它和0的大小决定是解否是复数
cout<<"推出请按Ctrl+c,输入a,b、c的值\n";
//Ctrl+c是 windows下的文件结束符.在命令行下不是拷贝快捷键
a=b=c=t=0;
//初始化
cin>>a>>b>>c;
//输入数据
if(a==0){printf("input error!\n");continue;}
//如果输入二次项系数为零显示告诉用户
t=b*b-4*a*c;
if(t==0)
cout<<"x1=x2="<<(-b/2a)<<endl;
if (t<0){
cout<<"x1="<<(-b/2.0/a)<<"+"<<(sqrt(-t)/2/a)<<"i"<<setprecision(3)<<endl;
cout<<"x2="<<(-b/2.0/a)<<"-"<<(sqrt(-t)/2/a)<<"i"<<setprecision(3)<<endl;
}
else {
cout<<"x1="<<(-b/2.0/a+sqrt(t)/2/a)<<setprecision(3)<<endl;
cout<<"x2="<<(-b/2.0/a-sqrt(t)/2/a)<<setprecision(3)<<endl;
}
return 0;}
添加回答
举报
0/150
提交
取消