Nineth Week item Five-My equation class

Source: Internet
Author: User

Design a one-dimensional equation class to find the solution of the equation such as ax+b=0.
For example: When input 3x-8=0, the solution of the output equation is x=2.66667;
Again such as: when input 5s+18=0, the solution of the output equation is s=-3.6;
The reference interface is as follows:

The reference code is:

Class cequation{private:double A;     Unknown coefficient double b;     Constant term char unknown; Symbol public:cequation representing unknown (double aa=0,double bb=0); friend IStream &operator >> (IStream &in,cequation & amp;e); friend Ostream &operator << (ostream &out,cequation &e);d ouble Solve (); Char Getunknown ();}; int main () {cequation e;cout<< "Please enter the equation (format: ax-b=0,a, B is constant, X is the letter representing the unknown):";cin>>e;   In two Tests, the equations for the input 3x-8=0 and 50s+180=0cout<< are: "The solution of the <<e;cout<<" equation is: "<<e.getunknown () <<" = " <<e.solve () <<endl; Two Tests, respectively output x= ... and S=...e.solve ();}

Code

/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Author: Lenkidong * Completion Date: May 8, 2015 * version number: v1.0*/#include "iostream" using namespace Std;class Cequation{pri     Vate:double A;     Unknown coefficient double b; Constant term char unknown;    Symbol public:cequation representing unknown (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) {}//input equation istream &operator >> (IStream &in,    Cequation &e) {char ch1,ch2,ch3,ch4;        while (1) {cin>>e.a>>ch1>>ch2>>e.b>>ch3>>ch4; if (ch1>= ' a ' && ch1<= ' z ') if ((ch2== ' + ' | | ch2== '-') && ch3== ' = ' && ch4== ' 0 ') Bre        Ak    cout<< "The input equation format does not conform to the specification, please re-enter \ n";    } if (ch2== '-') e.b=-e.b;    E.unknown=ch1; return in;} // Output Equation ostream &operator << (ostream &out,cequation &e) {cout<<e.a<<e.unknown;    if (e.b>=0) cout<< "+";    cout<<e.b<< "=0" <<endl; return out;}    Solve double cequation::solve () {double x; if (a==0) {if (b==0) cout<< "Any real number is the solution of the equation.        "<<endl; else cout<< "equation has no solution.        "<<endl;    return 0;    } x=-b/a; return x;} Char Cequation::getunknown () {return unknown;}    int main () {cequation E;    cout<< "Please enter the equation (format: ax-b=0,a, B is constant, X is the letter representing the unknown):";   cin>>e;    In two Tests, input 3x-8=0 and 50s+180=0 cout<< "equation as:" <<e; The solution of the cout<< "equation is:" <<e.getunknown () << "=" <<e.solve () <<endl; Two Tests, respectively output x= ... and s= ... e.solve ();}


Operation Result:

Learning experience:

Good study Day Day up


Nineth Week item Five-My equation class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.