Question:
A one-variable quadratic expression is an arithmetic expression of the form
a
x
2
+
b
x
+
c
ax^2+bx+c
ax2+bx+c, where a, b, and c are some fixed numbers (called the coefficients) and x is a variable that can take on different values. Specify, design, and implement a class that can store information about a quadratic expression. The default constructor should set all three coefficients to zero, and another member function should allow you to change these coefficients. There should be constant member functions to retrieve the current values of the coefficients. There should also be a member function to allow you to “evaluate” the quadratic expression at a particular value of
x
x
x (i.e., the function has one parameter
x
x
x, and returns the value of the expression
a
x
2
+
b
x
+
c
ax^2+bx+c
ax2+bx+c).
Also overload the following operators (as non-member functions) to perform these indicated operations:
quadratic operator +(
const quadratic& q1;
const quadratic& q2
);
// Postcondition: The return value is the
// quadratic expression obtained by adding
// q1 and q2. For example, the c coefficient
// of the return value is the sum of q1's
// coefficient and q2's coefficient.
quadratic operator *(
double r;
const quadratic& q
);
// Postcondition: The return value is the
// quadratic expression obtained by
// multiplying each of q's
// coefficients by the number r.
Notice that the left argument of the overloaded operator ∗ * ∗ is a double number (rather than a quadratic expression). This allows expressions such as 3.14 ∗ q 3.14*q 3.14∗q, where q q q is a quadratic expression.
My answer:
quadratic.h
#ifndef QUADRATIC_H
#define QUADRATIC_H
class quadratic{
public:
quadratic(){
a = b = c = 0;
};
void set_coefficients(double m, double n, double q);
double get_a() const;
double get_b() const;
double get_c() const;
double evaluate_exp(double x);
private:
double a, b, c;
};
quadratic operator +(const quadratic& q1, const quadratic& q2);
quadratic operator *(double r, const quadratic& q);
#endif
quadratic.cpp
#include "quadratic.h"
#include <cmath>
void quadratic::set_coefficients(double m, double n, double q){
a = m;
b = n;
c = q;
}
double quadratic::get_a() const{
return a;
}
double quadratic::get_b() const{
return b;
}
double quadratic::get_c() const{
return c;
}
double quadratic::evaluate_exp(double x){
return a * pow(x, 2) + b * x + c;
}
quadratic operator +(const quadratic& q1, const quadratic& q2){
quadratic q3;
q3.set_coefficients(q1.get_a() + q2.get_a(), q1.get_b() + q2.get_b(), q1.get_c() + q2.get_c());
return q3;
}
quadratic operator *(double r, const quadratic& q){
quadratic q1;
q1.set_coefficients(r * q.get_a(), r * q.get_b(), r * q.get_c());
return q1;
}
main.cpp
#include <iostream>
#include "quadratic.h"
void quadratic_info(quadratic);
int main(){
quadratic a;
a.set_coefficients(8, 2, 3);
quadratic_info(a);
std::cout << "when x = 3, gets " << a.evaluate_exp(3) << std::endl;
quadratic b;
b.set_coefficients(4.4, 3, -1);
quadratic_info(b);
std::cout << "when x = 3, gets " << b.evaluate_exp(3) << std::endl;
quadratic c;
c = a + b;
quadratic_info(c);
std::cout << "when x = 6, gets " << c.evaluate_exp(6) << std::endl;
c = 3 * c;
quadratic_info(c);
std::cout << "when x = -2, gets " << c.evaluate_exp(-2) << std::endl;
return 0;
}
void quadratic_info(quadratic a){
std::cout << "quadratic info:" << std::endl;
std::cout << "a = " << a.get_a() << ", b = " << a.get_b() << ", c = " << a.get_c() << "." << std::endl;
}
结果:
Reference:
整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p121