现在的位置: 首页 > 综合 > 正文

工大机试17题

2014年01月01日 ⁄ 综合 ⁄ 共 562字 ⁄ 字号 评论关闭

编程求解一元二次方程ax2+bx+c=0的根。要求:设计完备的测试数据集,考虑a, b, c各种取值对根的影响。

// problem17.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 double a,b,c;
 double x1,x2;
 cout<<"一元二次方程的形式为aX^2+bX+c=0,请输入a b
c,其中a≠0"<<endl;
 double Daita,sqrDaita;
 cin>>a>>b>>c;
 Daita=b*b-4*a*c;
 if(Daita<0)
 {
  cout<<"方程无解"<<endl;

 }
 else if(Daita==0)
 {
  cout<<"有两个相同解 ";
  x1=-b/(2*a);
  cout<<"
x1=x2="<<x1<<endl;

 }
 else
 {
  cout<<"有两个不同解";
    
sqrDaita=sqrt(Daita);
  x1=(sqrDaita-b)/(2*a);
  x2=(-sqrDaita-b)/(2*a);
  cout<<"x1="<<x1<<"x2="<<x2<<endl;

 }

 return 0;
}

 

抱歉!评论已关闭.