永发信息网

C++一题请教高手帮忙解决

答案:5  悬赏:40  手机版
解决时间 2021-04-16 19:23
  • 提问者网友:心牵心
  • 2021-04-15 22:15
C++一题请教高手帮忙解决
最佳答案
  • 五星知识达人网友:三千妖杀
  • 2021-04-15 22:47
以下是完整代码。。参考一下。。

#include<iostream>

#include<cmath>

using namespace std;

void fun(double a, double b, double c)

{

double d = b*b - 4*a*c;

double x1,x2;

if (d < 0)

{

cout <<"方程无实根\n";

}

else if (d == 0.0)

{

cout <<"方程有两个相同的实根:\n";

x1 = -b/(2*a);

cout << "x1=x2=" << x1 << endl;

}

else if (d > 0)

{

cout <<"方程有两个不同的实根:\n";

x1 = (-b+sqrt(d))/(2*a);

x2 = (-b-sqrt(d))/(2*a);

cout << "x1=" << x1 << ",x2=" << x2 << endl;

}

}





void main()
{
double a, b, c;
cout << "请输入一元二次方程的三个系数:\n";
cout <<"a=";
cin >> a;
cout <<"b=";
cin >> b;
cout <<"c=";
cin >> c;
cout << endl;
fun(a,b,c);
}

全部回答
  • 1楼网友:毛毛
  • 2021-04-16 01:58
#include "stdafx.h" #include "iostream.h" #include "math.h" void fun(double a,double b,double c) { double x1=(-b+sqrt(pow(b,2)-4*a*c))/2*a; double x2=(-b-sqrt(pow(b,2)-4*a*c))/2*a; cout<<"x1:"<<x1<<endl; cout<<"x2:"<<x2<<endl; } int main(int argc, char* argv[]) { double a,b,c; int i=0; while(pow(b,2)<4*a*c||i==0) { if(i) cout<<"error! enter again!"<<endl; cout<<"enter a:"<<endl; cin>>a; cout<<"enter b:"<<endl; cin>>b; cout<<"enter c:"<<endl; cin>>c; ++i; } fun(a,b,c); return 0; }
  • 2楼网友:长青诗
  • 2021-04-16 01:41
#include <math.h> #include <stdio.h> void fun( double a, double b, double c ) { double delta = b*b-4*a*c; double x1, x2; if ( delta < 0 ) { printf( "\n方程无实根" ); } else { x1 = ( -b + sqrt( delta ) ) / 2*a; x2 = ( -b - sqrt( delta ) ) / 2*a; if ( delta != 0 ) { printf( "\n方程有两个不同的实根:\n\n" ); printf( "x1=%.2f x2=%.2f\n\n", x1, x2 ); } else { printf( "\n方程有两个相同的实根:\n\n" ); printf( "x1=x2=%.2f\n\n", x1 ); } } } int main() { double a, b, c; printf( "请输入一元二次方程的三个系数:\n" ); scanf( "a=%lf, b=%lf, c=%lf", &a, &b, &c ); fun( a, b, c ); }
  • 3楼网友:持酒劝斜阳
  • 2021-04-16 00:33
#include <iostream> #include <stdio.h> #include <math.h> using namespace std; int main() { void fun(double a,double b,double c); double a,b,c; cout<<"请输入a,b,c的值:"<<endl; cin>>a>>b>>c; fun(a,b,c); system("PAUSE"); return 0; } void fun(double a,double b,double c) { float disc,x1,x2,p,q; disc=pow(b,2)-4*a*c; if(disc>=0) { p=-b/(2*a); q=sqrt(disc)/(2*a); x1=p+q; x2=p-q; cout<<"x1="<<x1<<" "<<"x2="<<x2; } else cout<<"Error"; }
  • 4楼网友:思契十里
  • 2021-04-15 23:41

#include<iostream.h> #include<math.h> void fun(double a, double b, double c) { double disc,x1,x2,p,q; if(fabs(a)<1e-6) cout<<"不是一元二次方程!"<<endl; else { disc=b*b-4*a*c; if(fabs(disc)<=1e-6) //判断是否有两个相等的实根 cout<<"x1-x2="<<-b/(2*a)<<endl; else { if(disc>1e-6) //判断有两个不相等的实根 { x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); cout<<"x1="<<x1<<endl; cout<<"x2="<<x2<<endl; } else { p=-b/(2*a); q=sqrt(fabs(disc))/(2*a); cout<<"x1="<<p<<"+"<<q<<"i"<<endl; cout<<"x2="<<p<<"-"<<q<<"i"<<endl; } } }

} void main() { double a,b,c; cout<<"请输入3个系数:"<<endl; cin>>a>>b>>c; fun(a,b,c); } //注意我们的disc定义成的是个实数所以不能直接的和0进行比较。1e-6表示的是一个很小的数。用这种方法来判断disc和0的关系

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯