/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 5 月 11 日
* 版 本 号:v1.0
*
* 问题描述:
* 输入描述:
* 程序输出:
*/
#include <iostream>
using namespace std;
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)
{
aa=a;
bb=b;
}
istream &operator >> (istream &in,CEquation &e)
{
char ch1,ch2,ch3;
int z;
while(1)
{
in>>e.a>>ch1>>ch2>>e.b>>ch3>>z;
if(ch1>='a'&&ch1<='z')
if((ch2=='+'||ch2=='-')&&ch3=='='&&z==0)
break;
cout<<"格式不正确,请重新输入!"<<endl;
}
e.unknown=ch1;
if(ch2=='-')
e.b=-e.b;
return in;
}
ostream &operator << (ostream &out,CEquation &e)
{
out<<e.a<<e.unknown;
if(e.b>=0)
cout<<"-";
out<<e.b<<"=0"<<endl;
return out;
}
double CEquation::Solve()
{
if(a==0)
{
if(b==0)
cout<<"任意实数均为该方程的解。"<<endl;
else
cout<<"该方程无解!"<<endl;
return 0;
}
else
return (-b/a);
}
char CEquation::getUnknown()
{
return unknown;
}
int main()
{
CEquation e;
cout<<"请输入方程(格式:ax-b=0,a、b为常数,x处是代表未知数的字母):";
cin>>e; //在两次测试中,分别输入3x-8=0和50s+180=0
cout<<"方程为:"<<e;
cout<<"方程的解为:"<<e.getUnknown()<<"="<<e.Solve()<<endl; //对两次测试,分别输出x=...和s=...
e.Solve();
}