#include < iostream > using namespace std; class CEquation { private: char c; double a; // 未知数系数 double b; // 常数项 char unknown; // 未知数的符号 public: CEquation ( double aa = 0, double bb = 0 ) : a ( aa ), b ( bb ) { } friend istream &operator >> ( istream &in, CEquation &e ); friend ostream &operator << ( ostream &out, CEquation &e ); double Solve( ); char getUnknown( ); }; istream & operator >> ( istream &in, CEquation &e ) { char d; int m; in >> e.a >> e.unknown >> e.c >> e.b >> d >> m; return in; } ostream &operator << ( ostream &out, CEquation &e ) { out << e.a << e.unknown << e.c << e.b << "+0"; return out; } double CEquation :: Solve( ) { double s; if ( c == '-') s = b / a; else s= - ( b / a ); return s; } char CEquation :: getUnknown( ) { return unknown; } int main( ) { CEquation e; cout << "请输入一元一次方程(输入格式:3x-8=0):"; cin >> e; //在两次测试中,分别输入3x-8=0和5s+18=0 cout << "方程为:" << e; cout << "方程的解为:" << e.getUnknown( ) << "=" << e.Solve( ) << endl; //对两次测试,分别输出x=...和s=... e.Solve( ); system ( "pause" ); return 0; } #include < iostream > using namespace std; class CEquation { private: char c; double a; // 未知数系数 double b; // 常数项 char unknown; // 未知数的符号 public: CEquation ( double aa = 0, double bb = 0 ) : a ( aa ), b ( bb ) { } friend istream &operator >> ( istream &in, CEquation &e ); friend ostream &operator << ( ostream &out, CEquation &e ); double Solve( ); char getUnknown( ); }; istream & operator >> ( istream &in, CEquation &e ) { char d; int m; in >> e.a >> e.unknown >> e.c >> e.b >> d >> m; return in; } ostream &operator << ( ostream &out, CEquation &e ) { out << e.a << e.unknown << e.c << e.b << "+0"; return out; } double CEquation :: Solve( ) { double s; if ( c == '-') s = b / a; else s= - ( b / a ); return s; } char CEquation :: getUnknown( ) { return unknown; } int main( ) { CEquation e; cout << "请输入一元一次方程(输入格式:3x-8=0):"; cin >> e; //在两次测试中,分别输入3x-8=0和5s+18=0 cout << "方程为:" << e; cout << "方程的解为:" << e.getUnknown( ) << "=" << e.Solve( ) << endl; //对两次测试,分别输出x=...和s=... e.Solve( ); system ( "pause" ); return 0; }