|
| 1 | +/* |
| 2 | + 顺序表的链式存储结构 |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <fstream> |
| 7 | +#include <stdlib.h> |
| 8 | +using namespace std; |
| 9 | +#define CreateNode(p) p=(Polyn)malloc(sizeof(PNode)) |
| 10 | +#define DeleteNode(p) free((void *)(p)) |
| 11 | +//结构体的定义 |
| 12 | +typedef struct Polynode |
| 13 | +{ |
| 14 | + |
| 15 | + int c; //系数 |
| 16 | + int e; //指数 |
| 17 | + struct Polynode *next; //指针 |
| 18 | +}PNode, *Polyn; |
| 19 | + |
| 20 | +ifstream is("input.txt"); //从文件input.txt读取数据 |
| 21 | +ofstream os("output.txt"); //从文件output.txt写入程序运行结果数据 |
| 22 | + |
| 23 | +//按照先入先出发创建顺序表,即先输入的数据存储在前面 |
| 24 | +Polyn create() |
| 25 | +{ |
| 26 | + |
| 27 | + Polyn last, p; |
| 28 | + Polyn h = new PNode; h->next = NULL; |
| 29 | + last = h; |
| 30 | + int c, e; |
| 31 | + while(is >> c >> e && c!= 0) |
| 32 | + { |
| 33 | + p = new PNode; |
| 34 | + p->c = c; |
| 35 | + p->e = e; |
| 36 | + last->next = p; |
| 37 | + last = p; |
| 38 | + } |
| 39 | + last->next = NULL; |
| 40 | + return h; |
| 41 | +} |
| 42 | +//打印出多项式 |
| 43 | +void print(Polyn h) |
| 44 | +{ |
| 45 | + Polyn p = h->next; |
| 46 | + while(p) |
| 47 | + { |
| 48 | + os << p->c << " " << p->e << endl; |
| 49 | + p = p->next; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +//按照指数升序排列,默认输入数据没有相同的指数 |
| 54 | +void upsort(Polyn h) |
| 55 | +{ |
| 56 | + Polyn q, s, pr, p; |
| 57 | + q = h->next; |
| 58 | + h->next = NULL; //断开头结点 |
| 59 | + while(q) |
| 60 | + { |
| 61 | + s = q;q = q->next; //摘下节点 |
| 62 | + pr = h; p = h->next; //初始化主从动指针 |
| 63 | + while(p && p->e < s->e ) |
| 64 | + { |
| 65 | + pr = p; |
| 66 | + p = p->next; |
| 67 | + } |
| 68 | + //following insert node *s |
| 69 | + pr->next = s; |
| 70 | + s->next = p; |
| 71 | + } |
| 72 | +} |
| 73 | +/* |
| 74 | +两个多项式相加算法类似于两个升序表的归并算法; |
| 75 | +以下算法考虑利用两个源多项式结点生成和式的结点; |
| 76 | +算法要求两个源多项式结点按指数e升序连接; |
| 77 | +算法结束后,两个源多项式成为空链表。 |
| 78 | +*/ |
| 79 | +Polyn add(Polyn h1, Polyn h2)//函数返回和式头指针 |
| 80 | +{ |
| 81 | + Polyn p1, p2, p3, h, p; |
| 82 | + p1 = h1->next; p2 = h2->next; |
| 83 | + CreateNode(h); p3 = h;//h为和式附加头结点指针 |
| 84 | + while(p1 && p2) |
| 85 | + { |
| 86 | + if(p1->e < p2->e) |
| 87 | + { |
| 88 | + p = p1; |
| 89 | + p1 = p1->next; |
| 90 | + }else if(p2->e < p1->e) |
| 91 | + { |
| 92 | + p = p2; |
| 93 | + p2 = p2->next; |
| 94 | + }else//p1->e==p2->e为真时 |
| 95 | + { |
| 96 | + p1->c = p1->c + p2->c; |
| 97 | + if( p1->c == 0)//p1->c==0应删除两个结点 |
| 98 | + { |
| 99 | + p = p1; p1 = p1->next; DeleteNode(p); |
| 100 | + p = p2; p2 = p2->next; DeleteNode(p); |
| 101 | + continue; |
| 102 | + } |
| 103 | + p = p2; p2 = p2->next; DeleteNode(p); |
| 104 | + p = p1; p1 = p1->next; |
| 105 | + } |
| 106 | + p3->next = p; p3 = p;//插入*p结点至和式末尾 |
| 107 | + } |
| 108 | + if(p1) |
| 109 | + p3->next = p1; |
| 110 | + else if(p2) |
| 111 | + p3->next = p2; |
| 112 | + else |
| 113 | + p2->next = NULL; |
| 114 | + return h; |
| 115 | +} |
| 116 | + |
| 117 | +/* |
| 118 | +//原理性算法如下 |
| 119 | +R(x)=0; |
| 120 | +for(i=0; i<n; i++) |
| 121 | +{ T(x)=P(x)cixdi; //T(x)结点数与P(x)相同 |
| 122 | +R(x)=R(x)+T(x); //利用加法算法 |
| 123 | +} |
| 124 | +*/ |
| 125 | +Polyn mul(Polyn hp, Polyn hq) |
| 126 | +{ |
| 127 | + Polyn hr, ht, p, q, pt; |
| 128 | + CreateNode(hr); hr->next = NULL;//R(x)=0; |
| 129 | + CreateNode(ht); ht->next = NULL;//T(x)=0; |
| 130 | + q = hq->next; |
| 131 | + while(q)//实现for(i=0; i<n; i++) |
| 132 | + { |
| 133 | + pt = ht; |
| 134 | + p = hp->next; |
| 135 | + while(p) |
| 136 | + { |
| 137 | + //以下实现T(x)=P(x)cixdi; |
| 138 | + CreateNode(pt->next); |
| 139 | + pt = pt->next; |
| 140 | + pt->c = p->c * q->c; |
| 141 | + pt->e = p->e + q->e; |
| 142 | + p = p->next; |
| 143 | + } |
| 144 | + pt->next = NULL; |
| 145 | + q = q->next; |
| 146 | + //以下实现R(x)=R(x)+T(x) |
| 147 | + p = add(hr, ht); |
| 148 | + DeleteNode(hr); |
| 149 | + hr = p; |
| 150 | + } |
| 151 | + DeleteNode(ht); |
| 152 | + return hr; |
| 153 | +} |
| 154 | +int main() |
| 155 | +{ |
| 156 | + |
| 157 | + int T = 3; //测试样例次数 |
| 158 | + while(T--) |
| 159 | + { |
| 160 | + Polyn hp, hq, hmul; |
| 161 | + hp = create(); |
| 162 | + hq = create(); |
| 163 | + upsort(hp); |
| 164 | + upsort(hq); |
| 165 | + os << "case" << 3-T << " :"<< endl; |
| 166 | + os << "hp排序后:" << endl; |
| 167 | + print(hp); |
| 168 | + os << "hp排序后:" << endl; |
| 169 | + print(hq); |
| 170 | + hmul = mul(hp, hq); |
| 171 | + os << "hp*hq的结果:" << endl; |
| 172 | + print(hmul); |
| 173 | + } |
| 174 | + return 0; |
| 175 | +} |
0 commit comments