您的位置:首页 > 其它

第九周项目五方程也是类

2015-05-10 16:38 519 查看
/*
*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:曾晓
*完成日期:2015年 5月 10日
*版本号: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)
{
a=aa;
b=bb;
}
istream &operator >> (istream &in,CEquation &e)
{
char c1,c2,c3,c4;
in>>e.a>>c1>>c2>>e.b>>c3>>c4;
while (1)
{
if(c1>='a' && c1<='z')
if((c2=='+' || c2=='-') && (c3=='=') && (c4=='0') )
break;
cout<<"您的输入不规范,请重新输入!"<<endl;
}
if(c2=='-')
e.b=-e.b;
e.unknown=c1;
return in;
}
ostream &operator << (ostream &out,CEquation &e)
{
out<<e.a<<e.unknown;
if(e.b>=0)
out<<e.b;
else
out<<-e.b;
out<<"=0";
return out;
}
double CEquation::Solve()
{
double x;
if(a==0)
{
if(b==0)
cout<<"任意一个实数均为方程的解!"<<endl;
else
cout<<"所求方程无解!"<<endl;
}
x=-b/a;
return x;

}
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();
}

运行结果:



  我真是醉了 今天是不适合敲代码吗  好多状况  

 再不克服惰性就要挂科了 ....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: