通过链表实现的多项式乘法【C++】
2014-10-31 04:41
344 查看
被某喵发给我让我看的代码丑到哭。。。。就随手再写一个吧。。。
话说最近一直在用C#。突然换回C++好不爽。竟然有点嫌弃C++了。。。
废话不多说。直接上代码吧。基本没有注释。不过挺水的也挺好理解。
哦对。题目是这样的。。。
![](https://img-blog.csdn.net/20141031044336781?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQmVhckN1dGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
话说最近一直在用C#。突然换回C++好不爽。竟然有点嫌弃C++了。。。
废话不多说。直接上代码吧。基本没有注释。不过挺水的也挺好理解。
哦对。题目是这样的。。。
#include<iostream> using namespace std; struct Term { Term *Pre, *Next; int Exp; double Coef; }; class polynomial { public: Term *Head,*Tail; polynomial(); ~polynomial(); void Add(double Coef, int Exp); void Sort(); void Merge(); void inputPoly(); void outputPoly(); friend const polynomial operator*(const polynomial& polyA, const polynomial& polyB); private: }; polynomial::polynomial() { Head = new Term; Head->Pre = nullptr; Head->Coef = 0; Head->Exp = 1; Head->Next = NULL; Tail = Head; } polynomial::~polynomial() { } void polynomial::Add(double Coef, int Exp) { Term *term = new Term; term->Pre = Tail; term->Coef = Coef; term->Exp = Exp; term->Next = NULL; Tail->Next = term; Tail = term; } void polynomial::inputPoly() { double coef; int exp; cout << "请输入多项式的系数和指数,当指数过小(小于-99)时将自动结束输入" << endl; while (true) { cin >> coef >> exp; if (exp < -99) { break; } this->Add(coef, exp); } this->Merge(); } void polynomial::outputPoly() { Term *Current = Head; bool First = true; cout << "多项式为:"; while (Current->Next != NULL) { Current = Current->Next; if (First) { First = false; if (Current->Coef != 1) { if (Current->Exp == 0) { cout << "1"; } else if (Current->Exp == 1) { cout << Current->Coef << "x"; } else { cout << Current->Coef << "x^" << Current->Exp; } } else { if (Current->Exp == 0) { cout << "1"; } else if (Current->Exp == 1) { cout << "x"; } else { cout << "x^" << Current->Exp; } } } else { if (Current->Coef > 0) { if (Current->Coef != 1) { if (Current->Exp == 0) { cout << "+" << Current->Coef; } else if (Current->Exp == 1) { cout << "+" << Current->Coef << "x"; } else { cout << "+" << Current->Coef << "x^" << Current->Exp; } } else { if (Current->Exp == 0) { cout << "+" << Current->Coef; } else if (Current->Exp == 1) { cout << "+" << "x"; } else { cout << "+" << "x^" << Current->Exp; } } } else { if (Current->Coef != -1) { if (Current->Exp == 0) { cout << Current->Coef; } else if (Current->Exp == 1) { cout << Current->Coef << "x"; } else { cout << Current->Coef << "x^" << Current->Exp; } } else { if (Current->Exp == 0) { cout << Current->Coef; } else if (Current->Exp == 1) { cout << "-x"; } else { cout << "-x^" << Current->Exp; } } } } } cout << endl; } //按指数排序 void polynomial::Sort() { double tempCoef; int tempExp; int ActionNum = 0; do { Term *Current = Head; ActionNum = 0; while (Current->Next != NULL) { Current = Current->Next; if (Current->Next == NULL) { break; } if (Current->Exp > Current->Next->Exp) { tempCoef = Current->Coef; Current->Coef = Current->Next->Coef; Current->Next->Coef = tempCoef; tempExp = Current->Exp; Current->Exp = Current->Next->Exp; Current->Next->Exp = tempExp; ActionNum++; } } } while (ActionNum > 0); } //合并多项式 void polynomial::Merge() { this->Sort(); Term *Current = Head; bool Action = false; while (Current->Next != NULL) { if (!Action) { Current = Current->Next; } Action = false; if (Current->Next == NULL) { Tail = Current; break; } if (Current->Exp == Current->Next->Exp) { Current->Coef = Current->Coef + Current->Next->Coef; if (Current->Next->Next == NULL) { Current->Next = NULL; Tail = Current; break; } else { Current->Next->Next->Pre = Current; Current->Next = Current->Next->Next; } Action = true; if (Current->Coef == 0) { Term *temp = Current->Next; Current->Next->Pre = Current->Pre; Current->Pre->Next = Current->Next; Current = temp; } } if (Current->Next == NULL) { Tail = Current; } } } //*运算符重载 polynomial const operator*(const polynomial& polyA, const polynomial& polyB) { polynomial polyC; Term *CurrentA = polyA.Head; Term *CurrentB = polyB.Head; while (CurrentA->Next != NULL) { CurrentB = polyB.Head; CurrentA = CurrentA->Next; while (CurrentB->Next != NULL) { CurrentB = CurrentB->Next; polyC.Add(CurrentA->Coef * CurrentB->Coef, CurrentA->Exp + CurrentB->Exp); } } polyC.Merge(); return polyC; } int main() { polynomial ah,bh,ch; ah.inputPoly(); ah.outputPoly(); bh.inputPoly(); bh.outputPoly(); ch = ah * bh; ch.outputPoly(); system("pause"); return 0; }
相关文章推荐
- 链表实现多项式加法和乘法(C语言实现)
- 链表实现多项式加法和乘法
- 多项式的加法和乘法算术运算的C++实现
- 我用c++写的一个多项式的处理器的核心部分和输出部分。实现多项式的加法,减法,乘法,除法,及其求余。
- 一元多项式的乘法与加法运算——链表实现
- C++ 单链表基本操作分析与实现 链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结
- c++ 数据结构 用单链表实现多项式的类定义
- 数据结构_链表_多项式求导_C++实现
- PAT 一元多项式的乘法与加法运算(链表 c++版)
- 数据结构:C++链表类及一元多项式的实现时遇到的困难
- 【数据结构】C++链表实现一元多项式的加法
- 链表实现多项式的加法和乘法
- C++大作业之链表实现的高精度加法,减法,和数组实现的高精度乘法。
- 【c++版数据结构】之用带头节点的单链表实现一元多项式(C语言版)
- 链表实现多项式加法和乘法
- 多项式乘法与加法运算(链表实现)
- 数据结构第二次上机实验【链表实现多项式的加法和乘法】
- 链表实现多项式的加法和乘法
- C++链表AT&T代码,通过Ubuntu实现生成(Linux内核分析笔记)
- 数据结构_链表_稀疏多项式求值_C++实现