[Cpp] view plaincopyprint? /*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Qiu xuewei
* Completion date: January 1, May 20, 2013
* Version: v1.0
* Input Description: None
* Problem description: unary quadratic equations
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
/*
Design a class of one-dimensional equations to obtain the solutions of the equations, such as ax + B = 0.
For example, when the input is 3x-8 = 0, the solution of the Output Equation is x = 2.66667;
Another example is: when the input is 5 s + 18 = 0, the solution of the Output Equation is s =-3.6;
*/
# Include <iostream>
Using namespace std;
Class CEquation
{
Public:
CEquation (double a1 = 0, double b1 = 0 );
Friend istream & operator> (istream & in, CEquation & c );
Friend ostream & operator <(ostream & output, CEquation & c );
Double solve ();
Char getx ();
Private:
Double a, B;
Double x;
Char s;
};
CEquation: CEquation (double a1, double b1)
{
A = a1;
B = b1;
}
Istream & operator> (istream & in, CEquation & c)
{
Char ac, bc, xc, l; // ac = "+/-", xc = "a-z symbol", bc = "= ";
Cin> c. a> xc> ac> c. B> bc> l; // ax + B = 0
If (ac! = '+' & Ac! = '-') | Bc! = ') // Logic or "|" logical and "&"
{
If (xc <'A' | xc> 'Z' | l! = '0 ')
Cout <"format incorrect! ";
}
If (ac = '-')
{
C. B =-c. B;
}
C. s = xc;
Return in;
}
Ostream & operator <(ostream & output, CEquation & c)
{
Cout <c. a <c. s;
If (c. B> 0) cout <"+ ";
Cout <c. B <"= 0" <endl;
Return output;
}
Double CEquation: solve ()
{
Double x;
If (a = 0)
{
Cout <"equations without solutions" <endl;
Return 0;
}
X =-B/;
Return x;
}
Char CEquation: getx ()
{
Return s;
}
Int main ()
{
CEquation e;
Cout <"enter a one-dimensional equation (input format: 3x-8 = 0/5 s + 18 = 0 ):";
Cin> e; // In the two tests, enter 3x-8 = 0 and 5S + 18 = 0 respectively.
Cout <"equation:" <e;
Cout <"the solution of the equation is:" <e. getx () <"=" <e. solve () <endl;
Return 0;
}
/*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Qiu xuewei
* Completion date: January 1, May 20, 2013
* Version: v1.0
* Input Description: None
* Problem description: unary quadratic equations
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
/*
Design a class of one-dimensional equations to obtain the solutions of the equations, such as ax + B = 0.
For example, when the input is 3x-8 = 0, the solution of the Output Equation is x = 2.66667;
Another example is: when the input is 5 s + 18 = 0, the solution of the Output Equation is s =-3.6;
*/
# Include <iostream>
Using namespace std;
Class CEquation
{
Public:
CEquation (double a1 = 0, double b1 = 0 );
Friend istream & operator> (istream & in, CEquation & c );
Friend ostream & operator <(ostream & output, CEquation & c );
Double solve ();
Char getx ();
Private:
Double a, B;
Double x;
Char s;
};
CEquation: CEquation (double a1, double b1)
{
A = a1;
B = b1;
}
Istream & operator> (istream & in, CEquation & c)
{
Char ac, bc, xc, l; // ac = "+/-", xc = "a-z symbol", bc = "= ";
Cin> c. a> xc> ac> c. B> bc> l; // ax + B = 0
If (ac! = '+' & Ac! = '-') | Bc! = ') // Logic or "|" logical and "&"
{
If (xc <'A' | xc> 'Z' | l! = '0 ')
Cout <"format incorrect! ";
}
If (ac = '-')
{
C. B =-c. B;
}
C. s = xc;
Return in;
}
Ostream & operator <(ostream & output, CEquation & c)
{
Cout <c. a <c. s;
If (c. B> 0) cout <"+ ";
Cout <c. B <"= 0" <endl;
Return output;
}
Double CEquation: solve ()
{
Double x;
If (a = 0)
{
Cout <"equations without solutions" <endl;
Return 0;
}
X =-B/;
Return x;
}
Char CEquation: getx ()
{
Return s;
}
Int main ()
{
CEquation e;
Cout <"enter a one-dimensional equation (input format: 3x-8 = 0/5 s + 18 = 0 ):";
Cin> e; // In the two tests, enter 3x-8 = 0 and 5S + 18 = 0 respectively.
Cout <"equation:" <e;
Cout <"the solution of the equation is:" <e. getx () <"=" <e. solve () <endl;
Return 0;
}