思路比较清晰
1.从命令行读入方程
2.解析字符串,解析出几个参数a/b,d/c,f/e
3.Rational类定义了分数的运算,代入运算即可
这个小程序的结构
\---main.cpp 程序的入口
|---rational.h 分数类的声明
|---Rational.cpp分数类的实现
|---myException.h定义几个异常类
|---test.cpp单独使用,测试Rational类
代码如下
main.cpp
#include "rational.h" #include "Rational.cpp" using namespace std; int main(){ while(1){ cout << "输入方程:形如a/b x + d/c = f/e"<<endl; char Arr[50]; cin.getline(Arr, 50); string equation(Arr); // cout << equation <<endl; // a/b x + d/c = f/e int pos_x = equation.find('x',0); int pos_p = equation.find('+',0); int pos_e = equation.find('=',0); Rational coe( equation.substr(0,pos_x) );// a/b Rational b( equation.substr(pos_p+1,pos_e - pos_p) );// d/c Rational c( equation.substr(pos_e+1,equation.length()-pos_e) );// f/e // cout<<"coe = "<<coe<<endl; // cout<<"b = "<<b<<endl; // cout<<"c = "<<c<<endl; if(coe.getNumer() == 0 && b.isEqual(c)) cout<<"x ∈ Q"<<endl; else if(coe.getNumer() == 0 && !b.isEqual(c)) cout<<"无解"<<endl; else cout<<"解得 x = "<<(c-b)/coe<<endl<<endl; system("pause"); system("cls"); } }
rational.h
#ifndef Rational_ #define Rational_ #include "myException.h" #include<iostream> using namespace std; class Rational{ public: Rational(int x, int y);//传入分子分母构造分数 Rational(string s);//从字符串构造分数 Rational nega();//相反数 Rational reci();//倒数 bool isEqual(Rational that); Rational add(Rational that);//加法 Rational sub(Rational that);//减法 Rational mul(Rational that);//乘法 Rational div(Rational that);//除法 void Output(ostream &out)const;//输出方法 int getNumer(){return numer;} int getDenom(){return denom;} private: int numer;//分子 int denom;//分母 int gcd(int a, int b) const { return (b==0) ? a : gcd(b, a % b); }//最大公约数 }; #endif // Rational_define
Rational.cpp
#include "rational.h" #include "myException.h" #include<cstdlib> using namespace std; Rational::Rational(int x, int y){//传入分子分母构造分数 if(y==0) throw new illegalDenominatorValue(); numer = x; denom = y; } //从字符串构造分数 Rational::Rational(string s){ int pos = s.find('/',0); if(pos <= 0 || pos >= s.length()-1) throw new illegalInputExpression(); numer = atoi( s.substr(0,pos).c_str() ); denom = atoi( s.substr(pos+1,s.length()-pos-1).c_str() ); if(denom == 0 ) throw new illegalDenominatorValue(); } //相反数 Rational Rational::nega(){ return Rational(-numer, denom); } //倒数 Rational Rational::reci(){ if(numer == 0) throw new NoReciprocal(); return Rational(denom,numer); } //相等 bool Rational::isEqual(Rational that){ return numer * that.denom == denom * that.numer; } //加法 Rational Rational::add(Rational that){ return Rational(numer * that.denom + denom * that.numer,denom * that.denom); } //减法 Rational Rational::sub(Rational that){ return add(that.nega()); } //乘法 Rational Rational::mul(Rational that){ return Rational(numer * that.numer, denom * that.denom); } //除法 Rational Rational::div(Rational that){ return mul(that.reci()); } //输出 void Rational::Output(ostream &out)const{ int c = gcd(numer,denom); // int c = 1; out<<numer/c<<"/"<<denom/c; } //输出操作符重载 ostream& operator<<(ostream& out, const Rational& r){ r.Output(out); return out; } //操作符重载 Rational operator +(Rational a, Rational b){ return a.add(b); } Rational operator -(Rational a, Rational b){ return a.sub(b); } Rational operator *(Rational a, Rational b){ return a.mul(b); } Rational operator /(Rational a, Rational b){ return a.div(b); }
myException.h
// exception classes for various error types #ifndef myExceptions_ #define myExceptions_ #include <string> #include<iostream> using namespace std; // illegal Denominator value class illegalDenominatorValue { public: illegalDenominatorValue(string theMessage = "Denominator must be nonezero!"){ message = theMessage; } void outputMessage() {cout << message << endl;} private: string message; }; // illegal input Expression class illegalInputExpression { public: illegalInputExpression(string theMessage = "Illegal input Expression"){ message = theMessage; } void outputMessage() {cout << message << endl;} private: string message; }; //倒数 class NoReciprocal { public: NoReciprocal(string theMessage = "No Reciprocal with numerator=0"){ message = theMessage; } void outputMessage() {cout << message << endl;} private: string message; }; #endif
test.cpp
#include "rational.h" #include "Rational.cpp" using namespace std; int main(){ Rational a(1,2); Rational b("1/4"); //Rational c("1/0"); //Rational c("1/"); cout<<"a = \t"<< a <<endl; cout<<"b = \t"<< b <<endl; cout<<"-a = \t"<< a.nega()<<endl; cout<<"1/b =\t"<< b.reci()<<endl; cout<<"a.add(b) =\t"<< a.add(b)<<endl; cout<<"a.sub(b) =\t"<< a.sub(b)<<endl; cout<<"a.mul(b) =\t"<< a.mul(b)<<endl; cout<<"a.div(b) =\t"<< a.div(b)<<endl; cout<<"a+b =\t"<< a+b<<endl; cout<<"a-b =\t"<< a-b<<endl; cout<<"a*b =\t"<< a*b<<endl; cout<<"a/c =\t"<< a/b<<endl; return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 17:47:03