C++面向对象+顺序表实现多项式的一些基本操作(附源代码)

文件结构 : 

文件名字用途
CmakeList.txtcmake文件
how.md简述思路以及其他说明
main.cpp主测试程序
Polynomial.cpp核心实现文件
Polynomial.h核心头文件

Polynomial.cpp

//
// Created by A Luck Boy on 2023/1/14.
//
#include "Polynomial.h"

// 创建销毁
Poly::Poly(int size) {
    // new int[]
    // 创建一个int型数组,数组大小是在[]中指定,
    maxSize = size;
    index = new float[maxSize];
    length = 0;
    print("创建成功!\n");
}

Poly::~Poly(){
    delete[] index;
    print("销毁成功!\n");
} ;

// 等于0的多项式(类似于判空)
bool Poly::isZero() const{
    // 长度为空或者每个系数都是0,那么我就把这个多项式视为0值
    return length == 0 || result(1) == 0;
}

// 尾部添加系数
void Poly::append(float number){
    // 如果满了就扩容
    if (length == maxSize){
        addSize(10);
    }else{}
    index[length]  = number;
    length++;
};

// 打印多项式
void Poly::console_log() {
    if (isZero()){
        printf("f(x) = 0\n");
    } else{
        printf("f(x) = ");
        for (int i=0;i<length;i++){
            if (index[i] != 0){
                // 这里没写加号,不过思路清晰,就没打算写
                printf("%fx^%d   ", index[i], i);
            } else{ continue;}
        }
        printf("\n");
    }
};

// 多项式合并(即系数运算)
void Poly::add(const Poly& example){
    if (length<example.length){
        addSize(example.length-length);
        for (int i=0;i<length;i++){
            example.index[i] += index[i];
        }
        index = example.index;
        length = example.length;
    } else{
        for (int i=0;i<length;i++){
            index[i] += example.index[i];
        }
    }
};

// 多项式结果运算
float Poly::result(float x) const{
    if (length == 0){
        return 0;
    }else{
        float _sum = 0;
        for (int i=0;i<length;i++){
            _sum += index[i] * pow(x, i);
        }
        return _sum;
    }
};

// 自动扩容
void Poly::addSize(int size){
    maxSize += size;
    float* another_index = new float [length+size];
    for (int i=0;i<length;i++  ){
        another_index[i ] = index[i];
    }
    delete[] index;
    index = another_index;
};

Polynomial.h

//
// Created by A Luck Boy on 2023/1/14.
//
#include <cstdio>
#include <cmath>
#define Waring printf("此处代码有问题!");
#define main int main
#define print printf

class Poly{
public:
    // 基础变量
    float *index;
    int length;
    int maxSize;
    // methods
    // 创建销毁
    Poly(int size=10);

    ~Poly() ;

    // 等于0的多项式(类似于判空,但又不是)
    bool isZero() const;

    // 尾部添加系数
    void append(float number);

    // 打印多项式
    void console_log();

    // 多项式合并(即系数运算)
    void add(const Poly& example);

    // 多项式结果运算
    float result(float x) const;

private:
    // 自动扩容
    void addSize(int size);

};

main.cpp

//
// Created by A Luck Boy on 2023/1/14.
//
#include "Polynomial.h"

main(){
    // 这里我就不提供插入、删除等等等额外常用操作
    Poly poly1, poly2, poly3;
    // 等于0的多项式(类似于判空,但又不是)
    if (poly1.isZero() || poly2.isZero()){
        print("两个确实都是空的!\n");
    } else{
        Waring
    }
    // 给第一个多项式添加5个不同数,第二个三个0,再看看
    poly1.append(6);
    poly1.append(-2);
    poly1.append(3);
    poly1.append(55);
    poly1.append(0.5);

    poly2.append(0);
    poly2.append(0);
    poly2.append(0);
    if (poly1.isZero()){
        print("poly1是空的\n");
        Waring
    } else{
        print("程序正确\n");
    }
    if (poly2.isZero()){
        print("程序正确\n");

    } else{
        Waring
    }
    poly3.append(2);
    poly3.append(0) ;
    poly3.append(4) ;

    // 打印多项式
    // 按理说,poly1和3可以正常打印,2则会为0,让我们验证一下
    poly1.console_log();
    poly2.console_log();
    poly3.console_log();
    // 多项式合并(即系数运算)
    // 1、3加到1上面
    poly1.add(poly3);
    poly1.console_log();
    // 换过来试试
    poly3.add(poly1);
    poly3.console_log();
    print("口算一次了,是对的\n");
    // 多项式结果运算,为了自己算一次,重新弄个好算的
    Poly poly4;
    poly4.append(1);
    poly4.append(1);
    poly4.append(1);
    poly4.append(1);
    poly4.append(1);
    // 2的幂次方前五项和,等比数列,首项为1
    print("2的幂次方前五项和,等比数列,首项为1 ,和: %f\n ", poly4.result(2));
}

终端运行结果

创建成功!
创建成功!
创建成功!
两个确实都是空的!
程序正确
程序正确
f(x) = 6.000000x^0   -2.000000x^1   3.000000x^2   55.000000x^3   0.500000x^4   
f(x) = 0
f(x) = 2.000000x^0   4.000000x^2   
f(x) = 8.000000x^0   -2.000000x^1   7.000000x^2   55.000000x^3   0.500000x^4   
f(x) = 10.000000x^0   -2.000000x^1   11.000000x^2   55.000000x^3   0.500000x^4   
口算一次了,是对的
创建成功!
2的幂次方前五项和,等比数列,首项为1 ,和: 31.000000
 销毁成功!
销毁成功!
销毁成功!
销毁成功!

源代码链接 : 

Poly · PythonnotJava/自己实现的数据结构与算法合集 - 码云 - 开源中国 (gitee.com)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

PythonNotJava

若您有别的建议,请在评论区留言

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

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

打赏作者

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

抵扣说明:

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

余额充值