用C语言实现集合运算器

来源:互联网 发布:windows系统维护工具 编辑:程序博客网 时间:2024/06/06 02:59
#include <stdio.h>
#include <malloc.h>    
 
typedef struct poly
{
int n;
int m;
struct poly *next;
}POLY;


POLY *create();
void print(POLY *phead);
POLY *add(POLY *phead1,POLY *phead2);
POLY *reduce(POLY *phead1,POLY *phead2);
void derivation(POLY *phead);
POLY *multiply(POLY *phead1,POLY *phead2);
void addation(POLY *phead);


int main()
{
char ch,sh='Y';
POLY *phead1=NULL,*phead2=NULL,*phead;
printf("输入第一个多项式:");
phead1=create();
printf("输入第二个多项式:");
phead2=create();
printf("输出第一个多项式:");
print(phead1);
printf("输出第二个多项式:");
print(phead2);
printf("\n");
printf("加'+' 减'-' 乘'*' 求导'd'\n");
/* fflush(stdin);
printf("请输入运算方式:");
ch=getchar();*/
while(sh=='Y'||sh=='y')
{
fflush(stdin);
printf("请输入运算方式:");
ch=getchar();
switch(ch)
{
case '+':phead=add(phead1,phead2);print(phead);break;
case '-':phead=reduce(phead1,phead2);print(phead);break;
case '*':phead=multiply(phead1,phead2);addation(phead);print(phead);break;
case 'd':derivation(phead1);derivation(phead2);print(phead1);print(phead2);break;
default:printf("输入错误");break;
}
printf("是否继续y/n");
fflush(stdin);
scanf("%c",&sh);
if(sh=='y'||sh=='Y') continue;
else break;
}
printf("1");
return 0; 
}


POLY *create()
{
POLY *p,*q,*phead;
int c;
phead=(POLY *)malloc(sizeof(POLY));
phead->next=NULL;
p=phead;
scanf("%d",&c);
while(c!=0)
{
q=(POLY *)malloc(sizeof(POLY));
q->next=p->next;
p->next=q;
p=q;
q->n=c; 
scanf("%d",&q->m);
scanf("%d",&c);
}
p->next=NULL;
return phead;
}


POLY *add(POLY *phead1,POLY *phead2)
{
POLY *phead,*p,*q,*flag,*temp;
phead=(POLY *)malloc(sizeof(POLY));
phead->next=NULL;
p=phead;
flag=phead1->next;
temp=phead2->next;
while(flag!=NULL&&temp!=NULL)
{
if(flag->m<temp->m)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=flag->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
flag=flag->next;
continue;
}
if(flag->m>temp->m)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=temp->n;
q->m=temp->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
continue;
}
else
{
q=(POLY *)malloc(sizeof(POLY));
q->n=temp->n+flag->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
flag=flag->next;
continue;
}
}
while(flag!=NULL)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=flag->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
flag=flag->next;
}
while(temp!=NULL)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=temp->n;
q->m=temp->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
}
p->next=NULL;
return phead;



POLY *reduce(POLY *phead1,POLY *phead2)
{
POLY *phead,*p,*q,*flag,*temp;
phead=(POLY *)malloc(sizeof(POLY));
phead->next=NULL;
p=phead;
flag=phead1->next;
temp=phead2->next;
while(flag!=NULL&&temp!=NULL)
{
if(flag->m<temp->m)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=flag->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
flag=flag->next;
continue;
}
if(flag->m>temp->m)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=-temp->n;
q->m=temp->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
continue;
}
else
{
q=(POLY *)malloc(sizeof(POLY));
q->n=flag->n-temp->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
flag=flag->next;
continue;
}
}
while(flag!=NULL)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=flag->n;
q->m=flag->m;
q->next=p->next;
p->next=q;
p=q;
flag=flag->next;
}
while(temp!=NULL)
{
q=(POLY *)malloc(sizeof(POLY));
q->n=-(temp->n);
q->m=temp->m;
q->next=p->next;
p->next=q;
p=q;
temp=temp->next;
}
p->next=NULL;
return phead;



POLY *multiply(POLY *phead1,POLY *phead2)
{
POLY *phead,*p,*q,*flag,*temp;
phead=(POLY *)malloc(sizeof(POLY));
phead->next=NULL;
p=phead;
flag=phead1->next;
while(flag!=NULL)
{
temp=phead2->next;
while(temp!=NULL)
{
q=(POLY *)malloc(sizeof(POLY));
q->next=p->next;
p->next=q;
p=q;
q->n=flag->n*temp->n;
q->m=flag->m+temp->m;
temp=temp->next;
}
flag=flag->next;
}
p->next=NULL;
return phead;
}


void derivation(POLY *phead)
{
POLY *temp;
int x;
temp=phead->next;
while(temp!=NULL)
{
if(temp->m==0)
{
temp->m=0;
temp->n=0;
}
else
{
x=temp->m;
temp->m=x-1;
temp->n=temp->n*x;
}
temp=temp->next; 
}
}


void addation(POLY *phead)
{
POLY *temp,*flag,*r;
flag=phead->next;
while(flag!=NULL)
{
temp=flag;
while(temp->next)
{
if(flag->m==temp->next->m)
{
r=temp->next;
flag->n=flag->n+temp->next->n;
temp->next=r->next;
free(r);
}
else temp=temp->next;
}
flag=flag->next;
}
}


void print(POLY *phead)
{
POLY *temp;
temp=phead->next;
int flag=1;
while(temp!=NULL)
{
if(temp->n==0) 
{
temp=temp->next;
continue;
}
if(temp->n>0&&flag!=1)
printf("+");
if(temp->n!=1&&temp->n!=-1)
{
printf("%d",temp->n);
if(temp->m==1) printf("X");
else if(temp->m) printf("X^%d",temp->m);
}
else
{
if(temp->n==1)
{
if(temp->m==0) printf("1");
else if(temp->m==1) printf("X");
else printf("X^%d",temp->m);
}
if(temp->n==-1)
{
if(temp->m==0) printf("-1");
else if(temp->m==1) printf("-X");
else printf("-X^%d",temp->m);
}
}
temp=temp->next;
flag=0;
}
printf("\n");
}

0 0
原创粉丝点击