C++语言题目1

IT小小龙 posted @ 2014年4月03日 16:05 in C++ with tags 面向对象 一元二次方程 , 1307 阅读
//为二元方程式ax**2+bx+c=0设计一个名为QuadraticEquation的类,这个类包括
//1、代表三个系数的私有域数据a、b、c
//2、一个参数为a、b、c的构造方法
//3、a、b、c的三个get方法
//4、一个名为getDiscriminant()的方法返回判别式结果:b**2-4ac
//5、一个名为getRoot1()和getRoot2()的方法返回等式的两个根
//这两个方法在判别式为非负数的时候返回计算结果,如果判别式为负数,这两个方法返回0.

//实现该类,编写一个测试程序,任给a、b、c的值创建一个QuadraticEquation对象,
//显示判别式结果。如果判别式为正数,输出该方程的两个根;如果判别式为0,输出一个根;
//如果判别式为负数,输出“该方程无根”。


#include"iostream"
#include"cmath"
using namespace std;
class QuadraticEquation
{
private:
  double a;
  double b;
  double c;
public:
  QuadraticEquation(double tempa, double tempb, double tempc)
  {
    a = tempa;
    b = tempb;
    c = tempc;
  }
  double geta()
  {
    return a;
  }
  double getb()
  {
    return b;
  }
  double getc()
  {
    return c;
  }
  double getDiscriminant()
  {
    return getb()*getb()-4*geta()*getc();
  }
  double getRoot1()
  {
    if(getDiscriminant()>=0)
      {
	return (-getb()+sqrt(getDiscriminant()))/(2*geta());
      }
    else
      {
	return 0;
      }
  }
  double getRoot2()
  {
    if(getDiscriminant()>=0)
      {
	return (-getb()-sqrt(getDiscriminant()))/(2*geta());
      }
    else
      {
	return 0;
      }
  }
};

int main()
{
  QuadraticEquation q(1,-8,15);
  cout<<"方程系数a为:"<<q.geta()<<endl;
  cout<<"方程系数b为:"<<q.getb()<<endl;
  cout<<"方程系数c为:"<<q.getc()<<endl;
  cout<<"判别式结果为:"<<q.getDiscriminant()<<endl;
  if(q.getDiscriminant()>0)
    {
      cout<<"方程的根为:x1="<<q.getRoot1()<<",x2="<<q.getRoot2()<<endl;
    }
  else if(q.getDiscriminant()==0)
    {
      cout<<"方程的根为:x1=x2="<<q.getRoot1()<<endl;
    }
  else
    {
      cout<<"该方程无根"<<endl;
    }
  return 0;
}
Avatar_small
依云 说:
2014年4月03日 16:38

这是 C 语言么 Orz............

Avatar_small
依云 说:
2014年4月03日 20:43

@IT小小龙: 那我招程序员,JD 上写「招优秀 C 程序员啦」,等应聘者来了,扔给他一张全是 Objective-C 的笔试题。「是的,这是 Objective-C,因为 Objective-C 完全兼容 C,所以没有单分出来,放在一起了。而且我们这边的机器上都是 Xcode,所以 C 和 Objective-C 都可以跑出来。」——这不滑稽么?

Avatar_small
IT小小龙 说:
2014年4月04日 22:04

@依云: 好吧,认真点好,我改成C++的分类了。

Avatar_small
依云 说:
2014年4月04日 22:28

@IT小小龙: 我被这个坑过。淘宝校招 JD 上明明只说了 C 结果去了考场各种 vector map 扑面而来……

Avatar_small
IT小小龙 说:
2014年4月04日 22:36

@依云: 好的,谢谢你的提醒,本来没想当回事,因为这些题目主要是给学弟学妹上机考试一点参考,没要弄得很正式。不过,现在想想,还是认真点好些,养成一个好的习惯嘛。 :)

Avatar_small
Challan 280 说:
2022年8月05日 21:57

Challan 280 is a very important Income tax related form used for Income tax payment of different types. Every year people across India have to file their Income tax. Sometimes you also have to make Income tax payment of different forms such as Advance Tax payable on occasions.While it may use for self-assessment tax and the tax for regular assessments, distributed profits over a land property or rents as well. Challan 280 The government of India has now made the online TIN NSDL website a blessing for tax payers. With the help of Challan 280 or ITNS 280 you can now pay your pending taxes or dues for current or previous years as well.

Avatar_small
IInd PUC Important Q 说:
2022年8月24日 12:55

Karnataka PUC Question Paper 2023 Download – KSEEB 1st & 2nd PUC Important Question Paper 2023, The Department of Pre-University Education, IInd PUC Important Question Paper 2023 Government of Karnataka PUE, has been announced the Previous Question Paper for II PUC examination for the year 2023. The examination will begin in the month of March 2023 and will continue till March last week 2023.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter