1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include <stdio.h> #include <stdlib.h>
typedef int ElementType; typedef struct LNode *List;
struct LNode { ElementType coe; ElementType exp; List Next; };
void Insert(List L, ElementType coe, ElementType exp); List Multi(List p1, List p2); List Plus(List p1, List p2); int compare(List p1, List p2);
int main() { List p1, p2; List p; int num1, num2, coe, exp; int i; p1 = (List) malloc(sizeof(struct LNode)); p2 = (List) malloc(sizeof(struct LNode)); p1->Next = NULL; p2->Next = NULL;
scanf("%d", &num1); for (i = 0; i < num1; i++) { scanf("%d %d", &coe, &exp); Insert(p1, coe, exp); } scanf("%d", &num2); for (i = 0; i < num2; i++) { scanf("%d %d", &coe, &exp); Insert(p2, coe, exp); } p = Multi(p1->Next, p2->Next); while (p) { if (p->Next != NULL) { printf("%d %d ", p->coe, p->exp); } else { printf("%d %d\n", p->coe, p->exp); } p = p->Next; } p = Plus(p1->Next, p2->Next); if (p) { while (p) { if (p->Next != NULL) { printf("%d %d ", p->coe, p->exp); } else { printf("%d %d\n", p->coe, p->exp); } p = p->Next; } } else { printf("0 0\n"); } return 0; }
void Insert(List L, ElementType coe, ElementType exp) { List s, p; p = L;
while (p->Next) p = p->Next;
s = (List) malloc(sizeof(struct LNode)); s->Next = NULL; s->coe = coe; s->exp = exp;
p->Next = s; }
List Multi(List p1, List p2) { List p, p1a, p2a, s; int flag = 1; p = (List) malloc(sizeof(struct LNode)); p->Next = NULL; p1a = p1;
while (p1a) { p2a = p2; s = (List) malloc(sizeof(struct LNode)); s->Next = NULL;
while (p2a) { Insert(s, p1a->coe * p2a->coe, p1a->exp + p2a->exp); p2a = p2a->Next; } s = s->Next;
if (flag == 1) { p = p->Next;
flag = 0;
} p = Plus(p, s); p1a = p1a->Next; free(s); }
return p; }
int compare(List p1, List p2) { if (p1->exp > p2->exp) return 1; else if (p1->exp < p2->exp) return -1; else return 0; }
List Plus(List p1, List p2) { List p, p1a, p2a; int temp; p = (List) malloc(sizeof(struct LNode)); p->Next = NULL; p1a = p1; p2a = p2;
while (p1a && p2a) { temp = compare(p1a, p2a); switch (temp) { case 1: Insert(p, p1a->coe, p1a->exp); p1a = p1a->Next; break; case -1: Insert(p, p2a->coe, p2a->exp); p2a = p2a->Next; break; case 0: if ((p1a->coe + p2a->coe) == 0) { p1a = p1a->Next; p2a = p2a->Next; } else { Insert(p, p1a->coe + p2a->coe, p2a->exp); p1a = p1a->Next; p2a = p2a->Next; } break; default: break; } } while (p1a) { Insert(p, p1a->coe, p1a->exp); p1a = p1a->Next; } while (p2a) { Insert(p, p2a->coe, p2a->exp); p2a = p2a->Next; } p = p->Next; return p; }
|