|
15鱼币
#include <stdio.h>
#include <malloc.h>
struct poly
{
int coef;
int index;
struct poly *next; //多项式后继指针
};
struct poly *CreatList(void); //创建一元多项式
struct poly *Plus(struct poly *an, struct poly *bn); //多项式相加函数
void Print(struct poly *P);
int main(void)
{
struct poly *an = NULL;
struct poly *bn = NULL;
struct poly *cn = NULL;
an = CreatList();
bn = CreatList();
cn = Plus(an, bn);
Print(cn);
return 0;
}
//创建函数
struct poly *CreatList(void)
{
struct poly *head = (struct poly *)malloc(sizeof(struct poly));
if (NULL == head)
{
printf("空间不足!");
return 0;
}
struct poly *tail = head;
tail->next = NULL;
int number; // 一元多项式的项数
int coef;
int index;
printf("请输入一元多项式的项数: ");
scanf("%d", &number);
if (number == 0)
{
head->next = NULL;
}
for (int i = 0; i < number; ++i)
{
printf("第%d项的系数是: ", i + 1);
scanf("%d", &coef);
printf("第%d项的指数是: x^", i + 1);
scanf("%d", &index);
printf("\n");
struct poly *GENERAL_TERM = (struct poly *)malloc(sizeof(struct poly));
if (NULL == GENERAL_TERM)
{
printf("空间不足!");
return 0;
}
GENERAL_TERM->coef = coef;
GENERAL_TERM->index = index;
tail->next = GENERAL_TERM;
GENERAL_TERM->next = NULL;
tail = GENERAL_TERM;
poly *p = NULL;
poly *q = NULL;
poly *t = NULL;
if (head == NULL || head->next == NULL)
{
return 0;
}
for (p = head; p != NULL; p = p->next)
{
for (q = head; q->next != NULL; q = q->next)
{
if (q->index > q->next->index)
{
int m = q->index;
int n = q->coef;
q->index = q->next->index;
q->next->index = m;
q->coef = q->next->coef;
q->coef = n;
}
}
}
}
return (head);
}
//多项式相加函数
struct poly *Plus(struct poly *an, struct poly *bn)
{
struct poly *pa = an->next;
struct poly *pb = bn->next;
struct poly *cn = (struct poly *)malloc(sizeof(struct poly));
if (NULL == cn)
{
printf("空间不足!");
return 0;
}
struct poly *pc = cn;
while (pa && pb)
{
struct poly *pa_b = (struct poly *)malloc(sizeof(struct poly));
if (NULL == (pa_b))
{
printf("空间不足!");
return 0;
}
if (pa->index < pb->index)
{
pa_b->coef = pa->coef;
pa_b->index = pa->index;
pa = pa->next;
pc->next = pa_b;
pa_b->next = NULL;
pc = pa_b;
}
else if (pa->index == pb->index)
{
pa_b->coef = pa->coef + pb->coef;
pa_b->index = pa->index;
pa = pa->next;
pb = pb->next;
pc->next = pa_b;
pa_b->next = NULL;
pc = pa_b;
}
else
{
pa_b->coef = pb->coef;
pa_b->index = pb->index;
pb = pb->next;
pc->next = pa_b;
pa_b->next = NULL;
pc = pa_b;
}
}
if (pa == NULL)
{
while (pb)
{
struct poly *pa_b = (struct poly *)malloc(sizeof(struct poly));
if (NULL == pa_b)
{
printf("空间不足!");
return 0;
}
pa_b->coef = pb->coef;
pa_b->index = pb->index;
pb = pb->next;
pc->next = pa_b;
pa_b->next = NULL;
pc = pa_b;
}
}
else
{
while (pa)
{
struct poly *pa_b = (struct poly *)malloc(sizeof(struct poly));
if (NULL == pa_b)
{
printf("空间不足!");
return 0;
}
pa_b->coef = pa->coef;
pa_b->index = pa->index;
pa = pa->next;
pc->next = pa_b;
pa_b = NULL;
pc = pa_b;
}
}
return (cn);
}
//打印输出函数
void Print(struct poly *P)
{
int total_terms = 0;
struct poly *pointer = P->next;
while (pointer)
{
if (total_terms != 0)
{
printf("+");
}
printf("%dx^%d", pointer->coef, pointer->index);
pointer = pointer->next;
total_terms++;
}
printf("\n总项数为: %d\n", total_terms);
}
运行有时候正常,如图一
有时候陷入死循环,如图二
求大佬指点
本帖最后由 jackz007 于 2020-11-13 11:46 编辑 #include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int coef ;
int index ;
struct node * next ;
} poly ;
poly * FindNode(poly * h , int index)
{
for(; h && h -> index != index ; h = h -> next) ;
return h ;
}
poly * CreatePoly(void)
{
poly * h , * p , * q ;
int f , k , m ;
printf("请输入一元多项式的项数: ") ;
fflush(stdin) ;
scanf("%d" , & m) ;
for(h = NULL , p = NULL , f = 1 , k = 0 ; k < m ; k ++) {
if((p = (poly *) malloc(sizeof(poly))) != NULL) {
p -> next = NULL ;
printf("请输入第 %d 项的系数和指数 : " , k + 1) ;
fflush(stdin) ;
scanf("%d%d" , & p -> coef , & p -> index) ;
if(! k) h = p ;
else q -> next = p ;
q = p ;
} else {
fprintf(stderr , "空间不足!\n") ;
f -- ;
break ;
}
}
if(! f) h = NULL ;
return h ;
}
poly * PlusPoly(poly * ha)
{
poly * h , * p , * q , * x ;
int f , k , m , n , coef , index ;
for(f = 1 , n = 0 ; ha ; ha = ha -> next , n ++) {
if((p = (poly *) malloc(sizeof(poly))) != NULL) {
p -> coef = ha -> coef ;
p -> index = ha -> index ;
p -> next = NULL ;
if(! n) h = p ;
else q -> next = p ;
q = p ;
} else {
fprintf(stderr , "空间不足!\n") ;
f -- ;
break ;
}
}
if(f) {
printf("请输入一元多项式的项数: ") ;
fflush(stdin) ;
scanf("%d" , & m) ;
for(k = 0 ; k < m ; k ++) {
printf("请输入第 %d 项的系数和指数 : " , k + 1) ;
fflush(stdin) ;
scanf("%d%d" , & coef , & index) ;
if((x = FindNode(h , index))) {
x -> coef += coef ;
} else {
if((p = (poly *) malloc(sizeof(poly))) != NULL) {
p -> coef = coef ;
p -> index = index ;
p -> next = NULL ;
if(! n) h = p ;
else q -> next = p ;
q = p ;
n ++ ;
} else {
fprintf(stderr , "空间不足!\n") ;
f -- ;
break ;
}
}
}
}
if(! f) h = NULL ;
return h ;
}
void PrintPoly(poly * h)
{
int d , m ;
for(d = 0 , m = 0 ; h ; m ++ , h = h -> next) {
if(h -> coef) {
if(h -> coef < 0) printf(" - ") ;
else if(d) printf(" + ") ;
if(! h -> index) {
printf("%d" , abs(h -> coef)) ;
} else if(h -> index == 1) {
if(h -> coef == 1) printf("x") ;
else printf("%dx" , abs(h -> coef)) ;
} else {
printf("%dx^%d" , abs(h -> coef) , h -> index) ;
}
d ++ ;
}
}
printf("\n总项数为: %d\n", d) ;
}
void DestroyPoly(poly * h)
{
poly * p ;
for(p = h ; h ; p = h) {
h = h -> next ;
free(p) ;
}
}
main(void)
{
poly * ha , * hb ;
ha = CreatePoly() ;
printf("\n") ;
hb = PlusPoly(ha) ;
printf("\n") ;
PrintPoly(hb) ;
DestroyPoly(ha) ;
DestroyPoly(hb) ;
}
编译、运行实况: D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
请输入一元多项式的项数: 2
请输入第 1 项的系数和指数 : 3 3
请输入第 2 项的系数和指数 : 2 2
请输入一元多项式的项数: 4
请输入第 1 项的系数和指数 : 2 2
请输入第 2 项的系数和指数 : 4 4
请输入第 3 项的系数和指数 : 3 3
请输入第 4 项的系数和指数 : 1 1
4x^2 + 4x^4 + 6x^3 + x
总项数为: 4
D:\0002.Exercise\C>x
请输入一元多项式的项数: 2
请输入第 1 项的系数和指数 : 3 3
请输入第 2 项的系数和指数 : 1 1
请输入一元多项式的项数: 1
请输入第 1 项的系数和指数 : 1 1
3x^3 + 2x
总项数为: 2
D:\0002.Exercise\C>
|
-
图一
-
|