Equation(类与对象+构造)

aa

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

class Equation
{
protected:
	double a, b, c;
    
public:
	Equation();
	Equation(double a, double b, double c);
	void set(double a, double b, double c);
	void getR();
};

Equation::Equation() {
	a = 1,b = 1, c = 0;
}

Equation::Equation(double a,double b,double c)
{
	this->a = a;
	this->b = b;
	this->c = c;
}

void Equation::set(double a, double b, double c)
{
	this->a = a;
	this->b = b;
	this->c = c;
}

void Equation::getR()
{
	double hh1 = b * b - 4 * a * c;
	cout << fixed << setprecision(2);
  
	if (hh1 < 0)
    {
      	hh1 = -hh1;
      	double DA2 = fabs(sqrt(hh1) / (2 * a));
		double DA1 = -b / (2 * a);
		cout << "x1=" << DA1 << "+" << DA2 
          <<"i" << " " << "x2=" << DA1 
          << "-" << DA2 << "i" << endl;
	}else if(hh1 == 0)
    {
		double root = -b / (2 * a);
		cout << "x1=x2=" << root << endl;
	}else{
		double root1 = (-b + sqrt(hh1)) / (2 * a);
		double root2 = (-b - sqrt(hh1)) / (2 * a);
      
		cout << "x1=" << root1 << " " << "x2=" << root2 << endl;
	}
}

int main()
{
	int t;
	cin >> t;
  	double n1, n2, n3;
	while (t--)
    {
		cin >> n1 >> n2 >> n3;
		Equation obj(n1, n2, n3);
		obj.getR();
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值