【C++实验】多项式加减

2024-06-23 00:52
文章标签 c++ 实验 多项式 加减

本文主要是介绍【C++实验】多项式加减,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:一元多项式运算

基本要求:

    (1) 输入并建立多项式;

    (2) 输出多项式;

    (3) 多项式加法

    (4) 多项式减法。

测试数据:


 代码展示:

#include<iostream>
using namespace std;
class LinkedNode
{
public:LinkedNode(double COEF, double INDEX, LinkedNode *ptr = NULL){this->coef = COEF;this->index = INDEX;this->next = ptr;}double coef;double index;LinkedNode *next;
};class LinkedList
{
public:LinkedList(){this->head = NULL;this->tail = NULL;}void create_polumerization(); //创建多项式  并且进行排序和重复次方的合并 void add_polumerization(LinkedList *p, char a); //多项式的运算 void print_result(LinkedList *p);  //打印输出多项式 void sort_polumerization();  //多项式的	排序	 void delete_samenode();//相同次方进行合并 
private:LinkedNode *head;LinkedNode *tail;
};
void LinkedList::create_polumerization()
{double index, coef;cout << "开始输入多项式(默认系数 为0时结束)" << endl;for (int i = 0;;i++){printf("\n请输入第%d项的系数:", i + 1);cin >> coef;if (coef == 0) break;printf("\n请输入第%d项的指数:", i + 1);cin >> index;if (tail == NULL){tail = new LinkedNode(coef, index);head = tail;}else{LinkedNode *temp = new LinkedNode(coef, index);tail->next = temp;tail = temp;}}//进行排序sort_polumerization();delete_samenode();
};
void LinkedList::sort_polumerization()
{LinkedNode *pre=NULL, *p=NULL, *t = NULL;if (head != NULL){while (head->next != t){pre = head;p = head->next;while (p != t){if (pre->index > p->index){swap(pre->coef, p->coef);swap(pre->index, p->index);pre = p;p = p->next;}else{pre = p;p = p->next;}}t = pre;}}};
void LinkedList::delete_samenode()
{if (head == NULL) return;for (LinkedNode* temp = head;temp != NULL && temp->next != NULL;){if (temp->index == temp->next->index){LinkedNode *p = temp;temp = temp->next;while (p->index == temp->index){p->coef += temp->coef;p->next = temp->next;delete temp;temp = p->next;}}else temp = temp->next;}
}
void LinkedList::print_result(LinkedList *p)
{double count = 0;for (LinkedNode *temp = p->head->next;temp != NULL;temp = temp->next){if (temp == p->head->next){if (temp->coef == 0) continue;else{count += temp->coef;if(temp->index!=0) cout << temp->coef << "x^" << temp->index;else cout << temp->coef;}}else{if (temp->coef == 0) continue;else{if (temp->coef > 0){count += temp->coef;if(temp->index!=0) cout << "+" << temp->coef << "x^" << temp->index;else cout<<"+" << temp->coef;}else{count += temp->coef;if(temp->index!=0) cout << temp->coef << "x^" << temp->index;else cout << temp->coef;}}}}if (count == 0) cout << count;
}
void LinkedList::add_polumerization(LinkedList *p, char a)
{LinkedNode *h1 = this->head;LinkedNode *h2 = p->head;LinkedList *result = new LinkedList();LinkedNode *h = new LinkedNode(0, 0);result->head = h;result->tail = result->head;cout << endl;if (a == '+'){while (h1 != NULL && h2 != NULL) {if (h1->index < h2->index){LinkedNode *temp = new LinkedNode(h1->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;}else if (h2->index < h1->index){LinkedNode *temp = new LinkedNode(h2->coef, h2->index);result->tail->next = temp;result->tail = temp;h2 = h2->next;}else {LinkedNode *temp = new LinkedNode(h1->coef + h2->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;h2 = h2->next;}}while (h1 != NULL){LinkedNode *temp = new LinkedNode(h1->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;}while (h2 != NULL){LinkedNode *temp = new LinkedNode(h2->coef, h2->index);result->tail->next = temp;result->tail = temp;h2 = h2->next;}}else{while (h1 != NULL && h2 != NULL){if (h1->index < h2->index){LinkedNode *temp = new LinkedNode(h1->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;}else if (h2->index < h1->index){LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);result->tail->next = temp;result->tail = temp;h2 = h2->next;}else {LinkedNode *temp = new LinkedNode(h1->coef - h2->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;h2 = h2->next;}}while (h1 != NULL){LinkedNode *temp = new LinkedNode(h1->coef, h1->index);result->tail->next = temp;result->tail = temp;h1 = h1->next;}while (h2 != NULL){LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);result->tail->next = temp;result->tail = temp;h2 = h2->next;}}print_result(result);
}
int main()
{char a;LinkedList l1;cout << "开始输入第一个多项式" << endl;l1.create_polumerization();cout << endl << "开始输入第二个多项式" << endl;LinkedList l2;l2.create_polumerization();cout << "请输入两个多项式之间的运算关系('+'或'-'):";cin >> a;l1.add_polumerization(&l2, a);
}

运行结果

这篇关于【C++实验】多项式加减的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1085874

相关文章

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关