一元多项式的乘法与加法运算——C++(链表)
时间:2021-07-29
本文章向大家介绍一元多项式的乘法与加法运算——C++(链表),主要包括一元多项式的乘法与加法运算——C++(链表)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <iostream> using namespace std; typedef struct Node { int coef; int exp; struct Node *next; } * List; List Read(); void Print(List L); List GetMutiply(List L1, List L2); List GetSum(List L1, List L2); int Count(List L); List Sort(List L); List Sort(List L) { List head = L; List temp; int cou = Count(L); int coef; int exp; int tcoef; while (cou--) { temp = head->next; while (temp->next) { if (temp->exp < temp->next->exp) { coef = temp->coef; exp = temp->exp; temp->exp = temp->next->exp; temp->coef = temp->next->coef; temp->next->coef = coef; temp->next->exp = exp; temp = temp->next; } else if (temp->exp == temp->next->exp) { tcoef = temp->coef + temp->next->coef; temp->coef = tcoef; temp->next = temp->next->next; temp = temp->next; } else { temp = temp->next; } } } return head; } List Read() { List L = (struct Node *)malloc(sizeof(Node)); List head; List add = NULL; L->next = NULL; head = L; int n, coef, exp; cin >> n; if (n != 0) { while (n--) { cin >> coef >> exp; if (coef != 0) { add = (struct Node *)malloc(sizeof(Node)); add->coef = coef; add->exp = exp; L->next = add; L = add; } } L->next = NULL; } return head; } void Print(List L) { int count = Count(L); if (L->next) { L = L->next; while (L->next) { if (L->coef != 0) { cout << L->coef << " " << L->exp << " "; L = L->next; } else { L = L->next; } } cout << L->coef << " " << L->exp << endl; } else if (count == 0) { cout << 0 << " " << 0 << endl; } else { cout << 0 << " " << 0; } } List GetSum(List L1, List L2) { List head = (Node *)malloc(sizeof(Node)), temp1, temp2, newlist, add = NULL; head->next = NULL; newlist = head; temp1 = L1->next; temp2 = L2->next; int tcoef; while (temp1 && temp2) { if (temp1->exp > temp2->exp) { add = (struct Node *)malloc(sizeof(Node)); add->coef = temp1->coef; add->exp = temp1->exp; newlist->next = add; newlist = add; temp1 = temp1->next; } else if (temp1->exp < temp2->exp) { add = (struct Node *)malloc(sizeof(Node)); add->coef = temp2->coef; add->exp = temp2->exp; newlist->next = add; newlist = add; temp2 = temp2->next; } else { if (temp1->coef + temp2->coef != 0) { tcoef = temp1->coef + temp2->coef; add = (struct Node *)malloc(sizeof(Node)); add->coef = tcoef; add->exp = temp1->exp; newlist->next = add; newlist = add; } temp1 = temp1->next; temp2 = temp2->next; } } if (temp1) newlist->next = temp1; else newlist->next = temp2; return head; } List GetMutiply(List L1, List L2) { List head = (Node *)malloc(sizeof(Node)), temp1 = L1->next, temp2 = L2->next, newlist, add = NULL; head->next = NULL; newlist = head; int tcoef, texp; int count1, count2; if (temp1 && temp2) { count1 = Count(L1); count2 = Count(L2); for (int i = count1; i > 0; i--) { for (int j = count2; j > 0; j--) { tcoef = temp1->coef * temp2->coef; texp = temp1->exp + temp2->exp; add = (struct Node *)malloc(sizeof(Node)); add->coef = tcoef; add->exp = texp; newlist->next = add; newlist = add; temp2 = temp2->next; } temp1 = temp1->next; temp2 = L2->next; } } head = Sort(head); return head; } int Count(List L) { int count = 0; List t = L->next; while (t) { count++; t = t->next; } return count; } int main() { List L1 = Read(); List L2 = Read(); List L = GetMutiply(L1, L2); Print(L); L = GetSum(L1, L2); Print(L); }
原文地址:https://www.cnblogs.com/Transirizo/p/15075300.html
- Word2Vec教程-Skip-Gram模型模型“伪”任务关于模型的更多细节隐藏层输出层
- React第三方组件6(状态管理之Mobx的使用②TodoList上)
- The 9th Zhejiang Provincial Collegiate Programming Contest
- P1789 【Mc生存】插火把
- FOJFOJ有奖月赛-2012年4月(校赛热身赛)-解题报告总结
- poj 1316 Self Numbers
- React第三方组件6(状态管理之Mobx的使用①简单使用)
- poj 1088 滑雪
- 八数码难题解法大全
- React第三方组件5(状态管理之Redux的使用⑥Redux DevTools)
- React第三方组件5(状态管理之Redux的使用⑤异步操作)
- HDU - 1846 Brave Game
- React多页面应用4(webpack4 提取第三方包及公共组件)
- zoj 2420
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法