天天看點

第九周實驗報告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