Quadratic exprssion

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.14q, 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;
}

结果:
picture

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值