本人对C语言的了解仅仅停留在大一课堂上老师的讲解,对C语言不是很熟悉。如代码有错误之处,还请各位斧正。
#include <stdio.h>
#include <malloc.h>
typedef struct multinomial Position;
typedef Position* List;
struct multinomial {
int coef;
int exp;
List next;
};
List first = NULL;
List second = NULL;
List addResult = NULL;
List subResult = NULL;
List mulResult = NULL;
// 合并同类项
List mergeLikeTerms(List head);
// 插入排序
List insertSort(List head);
// 根据用户输入的内容创建两个多项式
void createMultinomial();
// 实现相加操作
List additionalMultinomial();
// 实现相减操作
void subtractionMultinomial();
// 实现相乘操作
void multiplicationMultinomial();
// 实现顺序输出
void showByASC(List head);
// 实现逆序输出
void showByDesc(List head);
int main()
{
int num = 0;
while (1) {
printf("*******************多项式运算********************\n");
printf(" 1-多项式的相加\n");
printf(" 2-多项式的相减\n");
printf(" 3-多项式的相乘\n");
printf(" 退出(-1)\n");
printf(" 请您输入对应操作的数字:");
fflush(stdout);
scanf("%d", &num);
if (num < 0) {
break;
}
if (num != 1 && num != 2 && num != 3) {
printf("您输入的数字:%d, 请输入1 | 2 | 3\n", num);
continue;
}
printf("请根据提示初始化两个多项式:\n");
createMultinomial();
switch (num) {
case 1:
addResult = additionalMultinomial();
addResult = insertSort(addResult);
addResult = mergeLikeTerms(addResult);
printf("顺序输出:");
showByASC(addResult);
printf("逆序输出:");
showByDesc(addResult);
break;
case 2:
subtractionMultinomial();
break;
case 3:
multiplicationMultinomial();
break;
}
}
}
// x^4 - 3 x^2 + 5
List additionalMultinomial()
{
/*
* 相加的逻辑
*/
List head;
List helpFirst = first->next;
List helpSecond = second->next;
head = (List) malloc(sizeof(struct multinomial));
List helpAddResult = head;
while (helpFirst != NULL && helpSecond != NULL) {
List node = (List) malloc(sizeof(struct multinomial));
if (helpFirst->exp == helpSecond->exp) {
if (helpFirst->coef + helpSecond->coef == 0) {
helpFirst = helpFirst->next;
helpSecond = helpSecond->next;
continue;
}
node->exp = helpFirst->exp;
node->coef = helpFirst->coef + helpSecond->coef;
node->next = NULL;
helpFirst = helpFirst->next;
helpSecond = helpSecond->next;
} else if (helpFirst->exp < helpSecond->exp) {
node->exp = helpFirst->exp;
node->coef = helpFirst->coef;
node->next = NULL;
helpFirst = helpFirst->next;
} else {
node->exp = helpSecond->exp;
node->coef = helpSecond->coef;
node->next = NULL;
helpSecond = helpSecond->next;
}
helpAddResult->next = node;
helpAddResult = node;
}
if (helpFirst != NULL) helpAddResult->next = helpFirst;
if (helpSecond != NULL) helpAddResult->next = helpSecond;
return head;
}
// 实现相减操作
void subtractionMultinomial()
{
// 思路:遍历second,将系数全部取反,然后两个链表相加即可
List helpSecond = second->next;
while (helpSecond != NULL) {
helpSecond->coef = -helpSecond->coef;
helpSecond = helpSecond->next;
}
subResult = additionalMultinomial();
subResult = insertSort(subResult);
subResult = mergeLikeTerms(subResult);
printf("顺序输出:");
showByASC(subResult);
printf("逆序输出:");
showByDesc(subResult);
}
// 实现相乘操作
void multiplicationMultinomial()
{
List helpFirst = first->next;
List helpSecond = second->next;
mulResult = (List) malloc(sizeof(struct multinomial));
mulResult->next = NULL;
List help = mulResult;
while (helpFirst != NULL) {
while (helpSecond != NULL) {
List node = (List) malloc(sizeof(struct multinomial));
int coef = helpFirst->coef * helpSecond->coef;
int exp = helpFirst->exp + helpSecond->exp;
List cur = mulResult;
while (cur != NULL) {
if (cur->exp == exp) {
cur->coef += coef;
break;
}
cur = cur->next;
}
if (cur == NULL) {
node->coef = coef;
node->exp = exp;
node->next = NULL;
help->next = node;
help = node;
}
helpSecond = helpSecond->next;
}
helpFirst = helpFirst->next;
helpSecond = second->next;
}
help->next = NULL;
mulResult = insertSort(mulResult);
mulResult = mergeLikeTerms(mulResult);
printf("顺序输出:");
showByASC(mulResult);
printf("逆序输出:");
showByDesc(mulResult);
}
void createMultinomial()
{
printf("请输入根据提示输入系数和指数(如果系数为零代表退出)\n");
printf("构建第一个多项式:\n");
first = (List) malloc(sizeof(struct multinomial));
List helpFirst = first;
while (1) {
// 为first创建
List node = (List) malloc(sizeof(struct multinomial));
int coef = 0;
int exp = 0;
printf("请输入系数:");
fflush(stdout);
scanf("%d", &coef);
if (coef == 0) break;
printf("请输入指数:");
fflush(stdout);
scanf("%d", &exp);
node->coef = coef;
node->exp = exp;
node->next = NULL;
helpFirst->next = node;
helpFirst = helpFirst->next;
}
helpFirst->next = NULL;
// 为second创建
printf("请输入根据提示输入系数和指数(如果系数为零代表退出)\n");
printf("构建第二个多项式:\n");
second = (List) malloc(sizeof(struct multinomial));
List helpSecond = second;
while (1) {
// 为first创建
List node = (List) malloc(sizeof(struct multinomial));
int coef = 0;
int exp = 0;
printf("请输入系数:");
fflush(stdout);
scanf("%d", &coef);
if (coef == 0) break;
printf("请输入指数:");
fflush(stdout);
scanf("%d", &exp);
node->coef = coef;
node->exp = exp;
node->next = NULL;
helpSecond->next = node;
helpSecond = helpSecond->next;
}
helpSecond->next = NULL;
}
void showByDesc(List head)
{
List list[10];
List help = head->next;
List temp = (List) malloc(sizeof(struct multinomial));
List haha = temp;
int count = 0;
while (help != NULL) {
list[count++] = help;
help = help->next;
}
for (int i = count - 1; i >= 0; i--) {
List node = list[i];
haha->next = node;
haha = node;
}
haha->next = NULL;
showByASC(temp);
}
// // x^4 - 3 x^2 + 5
void showByASC(List head)
{
List help = head->next;
int flag = 0;
while (help != NULL) {
if (flag == 0) {
if (help->coef == 1) {
printf("x^%d", help->exp);
} else if (help->coef == -1) {
printf("-x^%d",help->exp);
} else if(help->exp == 0) {
printf(" %d ", help->coef);
} else {
printf("%d x^%d", help->coef, help->exp);
}
flag = 1;
} else {
if (help->coef > 0) {
printf(" + %d x^%d",help->coef, help->exp);
} else if (help->exp == 0) {
printf(" %d ", help->coef);
} else {
printf(" - %d x^%d", abs(help->coef), help->exp);
}
}
help = help->next;
}
printf("\n");
}
List insertSort(List head) {
if (head == NULL || head->next == NULL) {
return head;
}
Position dummy;
dummy.next = head;
List curr = head->next;
head->next = NULL;
while (curr != NULL) {
List prev = &dummy;
List next = curr->next;
while (prev->next != NULL && prev->next->exp < curr->exp) {
prev = prev->next;
}
curr->next = prev->next;
prev->next = curr;
curr = next;
}
return dummy.next;
}
// 合并同类项链表
List mergeLikeTerms(List head) {
if (head == NULL || head->next == NULL) {
return head;
}
List prev = head;
List curr = head->next;
while (curr != NULL) {
if (prev->exp == curr->exp) {
prev->coef += curr->coef;
prev->next = curr->next;
free(curr);
curr = prev->next;
}
else {
prev = curr;
curr = curr->next;
}
}
return head;
}