编写将两个多项式相加的函数
来源:互联网 发布:正则表达式语法php 编辑:程序博客网 时间:2024/06/05 02:32
数据结构与算法分析——C语言描述 习题3.6
#include "stdafx.h"#include"Polynomial.h"int main(){Polynomial P;P = (Polynomial)malloc(sizeof(Node));P->next = NULL;int m[3][2] = { {10,1000},{5,14},{1,0} };int n[4][2] = { {-2,1492},{ 3,1990},{11,1},{5,0}};PlusToPolyList(m, 3, n, 4, P);PrintPoly(P);return 0;}
#include<stdio.h>#include<stdlib.h>typedef struct node {int Coefficient;int Exponent;struct node* next;}Node;typedef Node* PtrToNode;typedef PtrToNode Polynomial;int IsEmpty(Polynomial P) {return P->next == NULL;}int IsLast(Polynomial P) {return P->next == NULL;}//创建多项式链表Polynomial CreatedPoly() {Polynomial head, p1, p2;int n = 0;p2 = p1 = (Polynomial)malloc(sizeof(Node));if (p1 == NULL) {printf("Can not created!\n");return NULL;}else {head = NULL;printf("请输入多项式:\n");scanf_s("%d%d", &(p1->Coefficient), &(p1->Exponent));while (p1->Coefficient != 0) {n++;if (n == 1) {head = p1;p2->next = NULL;}else {p2->next = p1;}p2 = p1;p1 = (Polynomial)malloc(sizeof(Node));scanf_s("%d%d", &(p1->Coefficient), &(p1->Exponent));}p2->next = NULL;free(p1);p1 = NULL;return head;}}//输出多项式void PrintPoly(Polynomial P) {P = P->next;while (P->next != NULL) {printf("(%d*X^%d)+", P->Coefficient, P->Exponent);P = P->next;}printf("(%d*X^%d)\n", P->Coefficient, P->Exponent);}//使用头插入法插入多项式。void PushPolyF(int Coefficient, int Exponent, Polynomial P) {Polynomial Tmp;Tmp = (Polynomial)malloc(sizeof(Node));Tmp->Coefficient = Coefficient;Tmp->Exponent = Exponent;Tmp->next = P->next;P->next = Tmp;}//对多项式进行从大到小的排序(因为是采用头插入法,则输出结构的时候,系数小的先输出)void SortPolyArray(int a[][2], int nums) {//使用冒泡排序法int Cof = 0;int Exp = 0;for (int i = 0; i < nums; i++) {for (int j = i + 1; j < nums; j++) {if (a[i][1] < a[j][1]) {Cof = a[i][0];Exp = a[i][1];a[i][0] = a[j][0];a[i][1] = a[j][1];a[j][0] = Cof;a[j][1] = Exp;}}}}//将多项式数组输出void PrintPolyArray(int m[][2], int count) {for (int i = 0; i < count; i++) {printf("(%d*X^%d)", m[i][0], m[i][1]);}}//将两个多项式中数相加并写入多项式链表中。void PlusToPolyList(int m[][2],int M, int n[][2],int N, Polynomial P) {SortPolyArray(m, M);SortPolyArray(n, N);int i = 0, j = 0;//任何一个多项式数组录入完毕后都会结束while(i < M) {if (m[i][1] > n[j][1]) {PushPolyF(m[i][0], m[i][1], P);i++;}else if (m[i][1] == n[j][1]) {PushPolyF((m[i][0])+(n[j][0]), m[i][1], P);i++;j++;}else {PushPolyF(n[j][0], n[j][1], P);j++;}}//将还未完成的数组继续录入if (i == M) {for(;j<N;j++)PushPolyF(n[j][0], n[j][1], P);}elsefor(;i<M;i++)PushPolyF(m[i][0], m[i][1], P);}
阅读全文
0 0
- 编写将两个多项式相加的函数
- 数据结构:求两个多项式的相加运算
- 两个多项式相加的程序(链表)
- 两个多项式相加!
- 两个多项式相加
- 两个多项式相加
- 两个一元多项式相加
- !求两个多项式相加运算
- 求两个多项式相加运算
- 两个多项式相加及相乘
- STL编程:两个多项式相加
- 链表形式 写出两个一元多项式相加的算法
- 两个多项式相乘和相加的java实现
- 一元多项式的相加
- 将两个链表的元素相加
- PAT-Advanced Level- 1002 两个多项式相加
- 链表之两个多项式相加
- 利用栈实现两个一元多项式相加
- SpringBoot入门-4-1(设置不显示某个字段信息)
- 北京工业大学——硕士研究生入学考试初试——数据结构历年真题(1995-2007)
- 上传AppStore出现 ERROR ITMS-90474,ERROR ITMS-90475错误
- shell 替换特殊字符‘\’ '_'等
- 在Eclipse中创建 Maven SpringMVC 项目 出现 cannot change version version web module 3.0
- 编写将两个多项式相加的函数
- 数据结构Map之TreeMap分析
- Eclipse中使用SVN
- Xshell显示评估期已过期解决方案
- Druid 介绍及配置
- spring AspectJ的Execution表达式
- 黄成佳|企业家精神到底为何物?
- 社交复兴! 社交附能的知识付费平台更值得加码
- 执行分布式检索过程