求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。
求解为什么答案不对#include<stdio.h>#include<math.h>floatx(inta,intb,floatd);floaty(inta,intb,f...
求解为什么答案不对
#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);
} 展开
#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);
} 展开
4个回答
展开全部
#include <cstdio>
#include <cmath>
// b^2-4ac == 0
void fun1(double &a,double &b,double &c,double &d){
double ans = -b/(2*a);
printf("b^2-4ac == 0 , x1 = x2 = %lf.\n",ans);
}
// b^2-4ac > 0
void fun2(double &a,double &b,double &c,double &d){
double ans1,ans2;
ans1 = (-b+sqrt(d)) / (2*a);
ans2 = (-b-sqrt(d)) / (2*a);
printf("b^2-4ac > 0 , x1 = %lf , x2 = %lf.\n",ans1,ans2);
}
// b^2-4ac < 0
void fun3(double &a,double &b,double &c,double &d){
double real,imar;
real = -b/(2*a);
imar = sqrt(-d) / (2*a);
printf("b^2-4ac < 0 , x1 = %lf+%lfi , x2 = %lf-%lfi.\n",real,imar,real,imar);
}
int main(){
double a,b,c,d;
printf("please input a,b,c.\n");
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){
d = b*b-4*a*c;
if(d==0) fun1(a,b,c,d);
else if(d>0) fun2(a,b,c,d);
else fun3(a,b,c,d);
printf("please input a,b,c.\n");
}
}
#include <cmath>
// b^2-4ac == 0
void fun1(double &a,double &b,double &c,double &d){
double ans = -b/(2*a);
printf("b^2-4ac == 0 , x1 = x2 = %lf.\n",ans);
}
// b^2-4ac > 0
void fun2(double &a,double &b,double &c,double &d){
double ans1,ans2;
ans1 = (-b+sqrt(d)) / (2*a);
ans2 = (-b-sqrt(d)) / (2*a);
printf("b^2-4ac > 0 , x1 = %lf , x2 = %lf.\n",ans1,ans2);
}
// b^2-4ac < 0
void fun3(double &a,double &b,double &c,double &d){
double real,imar;
real = -b/(2*a);
imar = sqrt(-d) / (2*a);
printf("b^2-4ac < 0 , x1 = %lf+%lfi , x2 = %lf-%lfi.\n",real,imar,real,imar);
}
int main(){
double a,b,c,d;
printf("please input a,b,c.\n");
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){
d = b*b-4*a*c;
if(d==0) fun1(a,b,c,d);
else if(d>0) fun2(a,b,c,d);
else fun3(a,b,c,d);
printf("please input a,b,c.\n");
}
}
展开全部
#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;}
#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;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
引用ylcqucc的回答:
展开全部
最佳答案的这张代码图还少了一句代码,必须在第一行上面再加一行"void x(int a,int b,float d);"才可以运行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询