Class of quadratic equation

Source: Internet
Author: User
/** Copyright (c) 2013, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Qiu xuewei * Completion Date: July 15, May 20, 2013 * version: v1.0 * input Description: none * Problem description: Mona quadratic equation * program output: * Problem Analysis: * Algorithm Design: slightly * // * design a class of one-dimensional equation to obtain the solution of an equation, such as ax + B = 0. For example, when the input is 3x-8 = 0, the solution of the Output Equation is x = 2.66667. For example, 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;} I Stream & 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! = ') // The logic or "|" corresponds to "&" {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 <"equation unsolvable" <endl; return 0 ;}x =-B/a; return x ;}char CEquation: getx () {return s ;} int main () {CEquation e; cout <"Enter the 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 cout <"equation:" <e; cout <"the solution of the equation is:" <e. getx () <"=" <e. solve () <endl; return 0 ;}

 

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.