#include<stdio.h> #include<stdlib.h>
typedef struct node { int coef;
int exp;
struct node *next;
}Node;
void inarray(int x,int *end,int n);
int string_change_num(char *start,int *end);
Node *string_change_link(int *end,int n);
Node *order(Node *head);
Node *unite(Node *head);
void print (Node *head);
void main() { Node *head,*head1;
char str[255];
int i,end[255],n,x;
printf("输入字符,以'='号结束:\n\n");
printf("例如要输入 3x^3+4x^3,就要输入3x3+4x3\n\n");//注意 '+'或者'-'只是为了标记用,作用是识别指数,没有实际符号意义 for(i=0;i<255;i++) { scanf("%c",&str[i]);
if(str[i]=='=')
break; }
str[i]='\0';
n=string_change_num(str,end);
head=string_change_link(end,n);
head1=order(head);
printf("按照指数从小到大排序的多项式:\n\n");
print(head1);
printf("\n");
printf("输入要查找的系数:\n\n");
scanf("%d",&x); inarray(x,end,n);
printf("合并后的多项式是:\n");
head1=unite(head);
head=order(head1);
print(head);
printf("\n"); }
int string_change_num(char *start,int *end) { int i=0,j=0,k,sum=0,c=0,flag=1;
while(start[i]!='\0') { if(start[i]=='x'||start[i]=='+'||start[i]=='-') { if(start[i]=='x'&&start[j-1]=='-')
flag=0; if(start[i]=='x'&&start[j-1]=='+')
flag=1;
for(k=j;k<i;k++) sum=sum*10+start[k]-'0';
if(flag==0) { sum=-sum;
flag=1; } end[c++]=sum; sum=0;
j=i+1; } i++; } while(start[j]>'0'&&start[j]<'9') { sum=sum*10+start[j]-'0';
j++; }
end[c++]=sum;
return c;
} Node *string_change_link(int *end,int n) { Node *head,*p,*s;
int i=0;
head=(Node *)malloc(sizeof(Node));
head->next=NULL;
p=head;
while(i<n) { s=(Node *)malloc(sizeof(Node));
if(i%2==0)
s->coef=end[i];
i++;
if(i%2!=0)
s->exp=end[i];
p->next=s;
p=s;
i++; }
p->next=NULL;
return head; }
Node *order(Node *head) { Node *p,*q,*t,*s,*h; h=(Node *)malloc(sizeof(Node)); h->next=head; p=h; while(p->next->next!=NULL) { for(s=p,q=p->next;q->next!=NULL;q=q->next) if(q->next->exp<s->next->exp) s=q; if(s!=q) { t=s->next; s->next=t->next; t->next=p->next; p->next=t; } p=p->next; } head=h->next; free(h); return head; }
Node *unite(Node *head) { Node *p,*q,*q1,*p1;
q1=head;
p1=head;
p=head->next; q=p->next;
while(p->next!=NULL) { for(q=p->next;q!=NULL;q=q->next) { if(p->exp==q->exp) { while(q1->next!=q)
q1=q1->next;
while(p1->next!=p)
p1=p1->next;
if(p->coef+q->coef==0) { if(q==p->next) { p1->next=q->next;
free(q);
free(p);
q=p1;
p=p1; } else{ p1->next=p->next;
free(p);
p=p1;
q1->next=q->next;
free(q);
q=q1; } p1=head;
q1=head; } else{ p->coef=p->coef+q->coef; q1->next=q->next;
free(q);
q=q1;
p1=head;
q1=head; } } } if(p->next!=NULL)
p=p->next; if(p->next==NULL)
break; } return head; }
void print(Node *head) { Node *p;
p=head->next;
if(p==NULL)
printf("0"); else { if(p->exp==0) printf("%d",p->coef);
else if(p->exp==1) printf("%dx",p->coef); else
if(p->coef==1)
printf("x^%d",p->exp);
else if(p->coef==-1)
printf("-x^%d",p->exp);
else printf("%dx^%d",p->coef,p->exp); p=p->next;
while(p!=NULL) { if(p->exp==0)
printf("%d",p->coef);
else if(p->exp==1) { if(p->coef>0) printf("+%dx",p->coef);
else printf("%d",p->coef); }
else if(p->coef>0&&p->coef!=1&&p->exp!=-1)
printf("+%dx^%d",p->coef,p->exp);
else if(p->coef<0&&p->coef!=1&&p->exp!=-1)
printf("%dx^%d",p->coef,p->exp);
else if(p->coef==1)
printf("+x^%d",p->exp); else if(p->coef==-1)
printf("-x^%d",p->exp);
p=p->next; }
} }
void inarray(int x,int *end,int n) { int i;
for(i=0;i<n;i+=2) { if(end[i]==x) printf("系数是:%d 指数是:%d\n\n",end[i],end[i+1]);
}
if(i>n)
printf("没有找到!!\n");
}