天天看点

第九周实验报告5

#include <iostream.h>
#include"stdlib.h"    
class CEquation
{
private:
	double a;     // 未知数系数
	double b;     // 常数项
	char unknown; // 未知数的符号
public:
	CEquation(double aa=0,double bb=0);
	friend istream &operator >> (istream &in,CEquation &e);
	friend ostream &operator << (ostream &out,CEquation &e);
	double Solve();
	char getUnknown();
};
CEquation::CEquation(double aa,double bb)
{
	a=aa;
	b=bb;
}
istream &operator >> (istream &in,CEquation &e)//在两次测试中,分别输入3x-8=0和5s+18=0
{
	double u;
	char l;
	in>>e.a >>e.unknown>>e.b>>l>>u;
	return in;
}
ostream &operator << (ostream &out,CEquation &e)
{
	if(e.b >=0)
	{
		out<<e.a <<'x'<<'+'<<e.b<<'='<<'0'<<endl;
	}
	else
	{
	out<<e.a <<'x'<<e.b<<'='<<'0'<<endl; 
	}
	return out;
}
double CEquation::Solve()
{
	return ((-b)/a);
}

char CEquation::getUnknown()
{
	return unknown;
}
int main()
{
	CEquation e,m;
	cout<<"请输入一元一次方程(输入格式:3x-8=0):";
	cin>>e;   
	cout<<"方程为:"<<e;
	cout<<"方程的解为:"<<e.getUnknown()<<"="<<e.Solve()<<endl; //对两次测试,分别输出x=...和s=...
	cout<<"请输入一元一次方程(输入格式:5s+18=0):";
	cin>>m;   
	cout<<"方程为:"<<m;
	cout<<"方程的解为:"<<m.getUnknown()<<"="<<m.Solve()<<endl; //对两次测试,分别输出x=...和s=...
	m.Solve ();
	return 0;
}
           
第九周实验报告5