西南交通大学数据结构实验报告 基于链表的多项式乘法

废话小作文:

进入大学也是第二年了,感觉时间匆匆就从眼前划过去了。说来也是感慨,到了大二却并没有感觉到自己达到了以前自己想象中应该达到的高度,反而是自己进入大学的浅薄的新鲜感已经被磨灭得差不多了。这个学期开学已经两周了,我却一次课也没有认真听过,作业也是慢慢堆积下来了。每次写作业前都得花不少的时间去让自己努力学会新的知识,效率极其低下。不知道说什么好。

发这个实验报告是为了记录我自己的成长记录,也是为了普济众生。我希望有一天我的某个学弟学妹想在大学里面一展宏图的时候能够找到我的个人博客账号,不至于像我这样迷茫,找不到前进的方向。希望以后有人在迷茫的时候至少能找到我的脚步,不管最后通向的地方是好是坏,总归来说还是有好的意义的。

如果大家要借鉴本文代码。烦请看懂原理过后自己修改一番。


实验内容及要求:

从字符文件输入两个多项式的非零系数及对应的指数,建立多项式的链式存储结构,计算这两个多项式的乘积,输出乘积多项式的全部非零系数及对应的指数到另一字符文件中。

要求输入输出字符文件中的数据格式自拟;编程语言采用C/C++。

实验目的:掌握单向链表的基本操作以及基于链表的多项式加法与乘法。


本程序设计文件读写,故数据格式约定见main.cpp的开头注释

CMakeLists.txt:

cmake_minimum_required(VERSION 3.23)
project(DataStr_work_1)

set(CMAKE_CXX_STANDARD 14)

add_executable(DataStr_work_1 main.cpp list.cpp list.h data_read.cpp data_read.h)

list.h:

//
// Created by JellyfishKnight on 2022/9/19.
//

#ifndef DATASTR_WORK_1_LIST_H
#define DATASTR_WORK_1_LIST_H

typedef struct Node{
    int value;
    int position;
    Node* ptr;
}Node;

class List{
private:
    Node* head;
public:
    explicit List(int len);

    void free();

    Node* get_head();

    ~List();
};



#endif //DATASTR_WORK_1_LIST_H

list.cpp:

//
// Created by JellyfishKnight on 2022/9/19.
//

#include "list.h"
#include "iostream"

using namespace std;

List::List(int len) {
    head = new Node();
    head->position = -1;
    head->value = -1;
    head->ptr = new Node();
    Node* ptr = head->ptr;
    for (int i = 0; i < len - 1; i++) {
        ptr->ptr = new Node();
        ptr->position = i;
        ptr->value = 0;
        ptr = ptr->ptr;
    }
    ptr->position = len - 1;
    ptr->value = 0;
}

void List::free() {
    Node* ptr = head->ptr;
    while (ptr) {
        delete head;
        head = ptr;
        ptr = ptr->ptr;
    }
    delete head;
}

List::~List() {
    free();
}

Node *List::get_head() {
    return head;
}

data_read.h:

//
// Created by JellyfishKnight on 2022/9/19.
//

#ifndef DATASTR_WORK_1_DATA_READ_H
#define DATASTR_WORK_1_DATA_READ_H

#include "iostream"
#include "list.h"

using namespace std;

bool read(Node* head1, Node* head2, int& length1, int& length2, const string& root);

bool print(Node* head, const string& root);

#endif //DATASTR_WORK_1_DATA_READ_H

data_read.cpp:

//
// Created by JellyfishKnight on 2022/9/19.
//
#include "list.h"
#include <fstream>
#include "iostream"

using namespace std;

bool read(Node* head1, Node* head2, int& length1, int& length2, const string& root) {
    ifstream in_file;
    in_file.open(root, ios::in);
    if (!in_file.is_open() || !head2 || !head1 || root.empty()) {
        return false;
    }
    Node* ptr = head1->ptr;
    in_file >> length1;
    in_file >> length2;
    for (int i = 0, data; in_file >> data; i++) {
        if (i == length1) {
            ptr = head2->ptr;
        }
        if (i < length1) {
            ptr->value = data;
            ptr->position = i;
            ptr = ptr->ptr;
        } else {
            ptr->value = data;
            ptr->position = i - length1;
            ptr = ptr->ptr;
        }
    }
    return true;
}

bool print(Node* head, const string& root) {
    ofstream out_file;
    out_file.open(root, ios::out | ios::trunc);
    if (!out_file.is_open() || root.empty() || !head) {
        return false;
    }
    Node* ptr = head->ptr;
    while (ptr) {
        out_file << ptr->value;
        out_file << " ";
        ptr = ptr->ptr;
    }
    return true;
}


main.cpp:

#include <iostream>
#include "list.h"
#include "data_read.h"

using namespace std;

/*
 * 规定数据读取格式
 * 第一行为两个多项式的长度
 * 单个多项式作为一行
 * 从0次方开始输入,若有为0的项则补0
 * 所有输入均为对应项的系数
 */
void multiplication(Node* head1, Node* head2, Node* head3) {
    Node* ptr1, *ptr2, *ptr3;
    ptr1 = head1->ptr;
    ptr2 = head2->ptr;
    ptr3 = head3->ptr;
    while (ptr1) {
        if (ptr1->value == 0) {
            ptr1 = ptr1->ptr;
            continue;
        }
        while (ptr2) {
            if (ptr1->value * ptr2->value == 0) {
                ptr2 = ptr2->ptr;
                continue;
            }
            int coefficient = ptr1->value * ptr2->value;
            int index = ptr1->position + ptr2->position;
            for (int i = 0; i < index; i++) {
                ptr3 = ptr3->ptr;
            }
            ptr3->value += coefficient;
            ptr3 = head3->ptr;
            ptr2 = ptr2->ptr;
        }
        ptr2 = head2->ptr;
        ptr1 = ptr1->ptr;
    }
}


int main() {
    string in_root = "../input.txt";
    string out_root = "../output.txt";
    List list1(100);
    List list2(100);
    int length1, length2;
    read(list1.get_head(), list2.get_head(), length1, length2, in_root);
    List list3(length1 + length2);
    multiplication(list1.get_head(), list2.get_head(), list3.get_head());
    print(list3.get_head(), out_root);
    return 0;
}

格式约定:

输入的第一行为两个多项式的长度,第二三行就是多项式。其中多项式的格式为:按照从左到右依次是0到n次方的顺序输入其各项的系数即可。


若有问题请指正,谢谢。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jellyfish Knight

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

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

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

打赏作者

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

抵扣说明:

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

余额充值