1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
//I am needing some help figuring out what I am doing wrong.
//Quadratic.h
//----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// class Quadratic declaration
class Quadratic
{
private:
double a, b, c;
public:
//constructor to initialize the private variables
//post: Initialize the values of a, b, and c with 0's
Quadratic();
//Function to set the value of the member variables
//pre: The parameters are values to be assigned to variables a, b, and c
//post: Initialize the values of a, b, and c with passed values
void set(double cdblA, double cdblB, double cdblC);
//Function to get the value of the member variable a
//post: Return the value stored in variable a
double get_a() const;
//Function to get the value of the member variable b
//post: Return the value stored in variable b
double get_b() const;
//Function to get the value of the member variable c
//post: Return the value stored in variable c
double get_c() const;
//Function to evaluate the value of the quadratic expression
//for given value of x
//pre: The parameter is value x
//post: Evaluate the quadratic expression and return the value
double evaluate(double x) const;
//Function to find the number of real roots to the quadratic equation
//post: Return the number of real roots
int real_root () const;
//Function to find larger real root of the quadratic equation
//post: Return the larger real root
double larger_root () const;
//Function to find smaller real root of the quadratic equation
//post: Return the smaller real root
double smaller_root () const;
void display() const;
Quadratic operator + (const Quadratic & rhs);
Quadratic operator * (const double);
};
//------------------------------Quadratic.cpp Program------------------------------
//--------------------------------------------------------------------------------------
#include "Quadratic.h"
#include <assert.h>
#include <cmath>
//constructor to initialize the private variables
//pre: values to be assigned (zeros) to a, b, and c are passed
//post: Initialize the values of a, b, and c with 0's
Quadratic::Quadratic() : a(0), b(0), c(0) {}
//Function to set the value of the member variables
//pre: values to be assigned to a, b, and c are passed
//post: Initialize the values of a, b, and c with passed values
void Quadratic::set(double cdblA, double cdblB, double cdblC)
{
a = cdblA;
b = cdblB;
c = cdblC;
}
//Function to get the value of the member variable a
//post: Return the value stored in variable a
double Quadratic::get_a() const
{
return a;
}
//Function to get the value of the member variable b
//post: Return the value stored in variable b
double Quadratic::get_b() const
{
return b;
}
//Function to get the value of the member variable c
//post: Return the value stored in variable c
double Quadratic::get_c() const
{
return c;
}
//Function to evaluate the value of the quadratic expression
//for given value of x
//pre: The parameter is value x
//post: Evaluate the quadratic expression and return the value
double Quadratic::evaluate(double x) const
{
return (a * x * x) + (b * x) + c;
}
//Function to add two quadratic expressions
//pre: The parameter the objects q1 and q2
//post: Add q1 and q2 and store the result in object q
Quadratic operator+(const Quadratic & q1, const Quadratic & q2)
{
double a = q1.get_a() + q2.get_a();
double b = q1.get_b() + q2.get_b();
double c = q1.get_c() + q2.get_c();
Quadratic q;
q.set(a, b, c);
return q;
}
//Function to multiply the quadratic expression with a constant
//pre: r has been assigned
//post: Multiply q with a constant r and store it in q1, return q1
Quadratic operator*(double r, const Quadratic & q )
{
double a = r * q.get_a();
double b = r * q.get_b();
double c = r * q.get_c();
Quadratic q1;
q1.set(a, b, c);
return q1;
}
//Function to find the number of real roots to the quadratic equation
//post: Return the number of real roots
int Quadratic::real_root() const
{
double x1 = 0.0;
double x2 = 0.0;
int real_root;
//If a, b and c are all zero, then every value of x is a real root.
if(a == 0 && b == 0 && c == 0)
{
real_root = -1;
}
//If a and b are zero and c is non-zero, then there are no real roots.
else if(a == 0 && b == 0 && c != 0)
{
real_root = 0;
}
//If a is zero and b is non-zero, then the only real root is x = -c/b.
else if(a == 0 && b != 0)
{
real_root = 1;
}
//If a is non-zero and b^2 < 4ac, then there are no real roots.
else if(a != 0 && b*b < (4*a*c))
{
real_root = 0;
}
//If a is non-zero and b^2 = 4ac, then there is one real root x = -b/2a.
else if(a != 0 && b*b == (4*a*c))
{
real_root = 1;
}
//If a is non-zero and b2 > 4ac, then there are two real roots.
else if(a != 0 && b*b > (4*a*c))
{
real_root = 2;
}
return real_root;
}
//Function to find larger real root of the quadratic equation
//post: Return the larger real root
double Quadratic::larger_root() const
{
double larger;
assert(real_root()>0);
//If a is zero and b is non-zero, then the only real root is x = -c/b.
if(a == 0 && b != 0)
larger = -c/b;
else if(a != 0 && b*b == (4*a*c))
larger = -b/2*a;
//If a is non-zero and b2 > 4ac, then there are two real roots.
else if(a != 0 && b*b > (4*a*c))
larger = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
return larger; }
//Function to find smaller real root of the quadratic equation
//post: Return the smaller real root
double Quadratic::smaller_root()const
{ double smaller;
assert(real_root()>0);
//If a is zero and b is non-zero, then the only real root is x = -c/b.
if(a == 0 && b != 0)
smaller = -c/b;
else if(a != 0 && b*b == (4*a*c))
smaller = -b/2*a;
//If a is non-zero and b2 > 4ac, then there are two real roots.
else if(a != 0 && b*b > (4*a*c))
smaller = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);
return smaller; }
//Function to display quadratic equation
//post: display the quadratic equation in standard form
void Quadratic::display() const
{ cout << a << " X^2 + "<< b << " X + " <<c; }
|