您的位置:首页 > 理论基础 > 数据结构算法

PAT数据结构_02-线性结构2 一元多项式的乘法与加法运算 (20分)

2016-11-27 20:10 976 查看
题目地址: https://pta.patest.cn/pta/test/1342/exam/4/question/19209

基于链表的实现

#include "stdio.h"
#include "stdlib.h"
#include <iostream>
using namespace std;

// 数学术语https://en.wikipedia.org/wiki/Product_(mathematics)
// Polynomial 多项式
typedef struct PolyNode *Polynomial;
struct PolyNode
{
// 系数 coefficients
// 指数 expon
int coef;
int expon;
Polynomial next;
};

void Attach(int c, int e, Polynomial *pRear){
Polynomial p;
p = (Polynomial)malloc(sizeof(struct PolyNode));
p->coef = c;
p->expon = e;
p->next = NULL;

(*pRear)->next = p; //传来的是地址;操作时,需要用结构体指针操作
*pRear = p; // *pRear = p; 或者 pRear = &p

// pRear 是 Polynomial * 类型,即PolyNode **类型;

// pRear->next = p;
// error: member reference base type 'Polynomial' (aka 'PolyNode *') is not a structure or union
// pRear = p;
// error: assigning to 'Polynomial *' (aka 'PolyNode **') from incompatible type 'Polynomial' (aka 'PolyNode *'); take the address with &
}

void PrintPoly(Polynomial P){
int flag = 0; //调整格式用,第一项前不输出空格

if(!P){
printf("0 0\n");
return;
}

while (P){
if(!flag){
flag = 1;
}else{
printf(" ");
}
printf("%d %d", P->coef, P->expon);
P = P->next;
}
printf("\n");
}

Polynomial ReadPoly(){
Polynomial p, rear, tmp;
int c, e, N;

scanf("%d", &N);
p = (Polynomial) malloc(sizeof(struct PolyNode)); //链表头空结点
p->next = NULL; //生成链表时,用一个临时的空结点,方便attach
rear = p;
while(N--){
scanf("%d %d", &c, &e);
Attach(c, e, &rear); //将当前项插入多项式尾部
}

//删除临时生成的头结点
tmp = p;
p = p->next;
free(tmp);

return p;
}

Polynomial Add(Polynomial P1, Polynomial P2){

Polynomial t1 = P1;
Polynomial t2 = P2;

Polynomial p, rear, tmp;
p = (Polynomial) malloc(sizeof(struct PolyNode));
p->next = NULL; //合并的链表,由一个不含data的header开始
rear = p;

while(t1 && t2){
// cout<<"\nt1: "<<t1->coef<<" "<<t1->expon<<endl;
// cout<<"\nt2: "<<t2->coef<<" "<<t2->expon<<endl;

if(t1->expon == t2->expon){
int c = t1->coef + t2->coef;
int e = t1->expon;
if(c != 0){
Attach(c, e, &rear);
}
t1 = t1->next;
t2 = t2->next;
}else if(t1->expon > t2->expon){
int c = t1->coef;
int e = t1->expon;
Attach(c, e, &rear);
t1 = t1->next;

}else if(t1->expon < t2->expon){
int c = t2->coef;
int e = t2->expon;
Attach(c, e, &rear);
t2 = t2->next;
}
}
while(t1){
Attach(t1->coef, t1->expon, &rear);
t1 = t1->next;
}
while(t2){
Attach(t2->coef, t2->expon, &rear);
t2 = t2->next;
}

tmp = p; p = p->next; free(tmp); //将p指向真实的数据node

return p;
}

Polynomial Mult(Polynomial P1, Polynomial P2){

if(!P1 || !P2)
return NULL;

Polynomial t1 = P1;
Polynomial t2 = P2;
int c, e;

Polynomial p, rear, tmp;
p = (Polynomial) malloc(sizeof(struct PolyNode));
p->next = NULL; //合并的链表,由一个不含data的header开始
rear = p;

// 构造初始多项式, 先用P1度第一项乘以P2,得到P
while(t2){
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &rear);
t2 = t2->next;
}

//接下来的每个 P1单项*P2, 找到合适位置插入P
t1 = t1->next;
while(t1){
// cout<<"\nt1: "<<t1->coef<<" "<<t1->expon<<endl;
t2 = P2; //t1后挪后,t2重置

rear = p;
while(t2){
// cout<<"\nt2: "<<t2->coef<<" "<<t2->expon<<endl;

e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
//找插入点,若下一个点的项数仍大于要插入的项,则后移rear
while(rear->next && rear->next->expon > e){
rear = rear->next;
}
//若存在对应指数,则判断相加后是否为0
if(rear->next && rear->next->expon == e){
if(rear->next->coef + c){
rear->next->coef += c;
}else{ //若相加后为0,删掉node
tmp = rear -> next;
rear->next = tmp->next;
free(tmp);
}
}else{
//若不存在对应指数,生成新结点,加在rear之后,rear->next之前
tmp = (Polynomial)malloc(sizeof(struct PolyNode));
tmp->coef = c;
tmp->expon = e;
tmp->next = rear->next;
rear->next = tmp;
rear = rear->next;
}
t2 = t2->next;
}
t1 = t1->next;
}

tmp = p; p = p->next; free(tmp); //将p指向真实的数据node
return p;
}

int main(){

Polynomial p1, p2, pProduct, pSum;
// 读入多项式1
p1 = ReadPoly();
// 读入多项式2
p2 = ReadPoly();
// PrintPoly(p1);
// PrintPoly(p2);

// 生成乘积多项式并输出
// (a+b)*(c+d) = ac+ad+bc+bd
pProduct = Mult(p1, p2);
PrintPoly(pProduct);
// 生成和多项式并输出
pSum = Add(p1, p2);
PrintPoly(pSum);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: