Class of quadratic equation

Source: Internet
Author: User

[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;
}

 

 

 

 

 

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.