【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++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c