Cong 2 da thuc use dslk
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//#include<alloc>
struct node
{
int heso;
int bac;
struct node *link;
}*start1,*start2;
int add(struct node **start,int Heso,int Bac)
{
struct node *temp;
if(*start==NULL)
{
temp=malloc(sizeof(struct node));
if(temp==NULL)
{
printf("
Loi cap phat bo nho!");
return 0;
}
temp->heso=Heso;
temp->bac=Bac;
temp->link=NULL;
*start=temp;
return 1;
}
else
{
temp=malloc(sizeof(struct node));
if(temp==NULL)
{
printf("
Loi cap phat bo nho!");
return 0;
}
temp->heso=Heso;
temp->bac=Bac;
temp->link=*start;
*start=temp;
return 1;
}
}
void congdathuc(struct node *star1,struct node *star2)
{
struct node *temp1,*temp2,*temp;
temp1=star1;
temp2=star2;
while((temp1!=NULL)&&(temp2!=NULL))
{
if(temp1->bac==temp2->bac)
{
printf("%d*x^%d + ",((temp1->heso)+(temp2->heso)),temp1->bac);
temp1=temp1->link;
temp2=temp2->link;
}
else{
if(temp1->bac<temp2->bac) {
printf("%d*x^%d + ",temp2->heso,temp2->bac);
temp2=temp2->link;
}
if(temp1->bac>temp2->bac){
printf("%d*x^%d + ",temp1->heso,temp1->bac);
temp1=temp1->link;
}
}
if((temp1==NULL)&&(temp2!=NULL))
{
temp=temp2;
while(temp!=NULL) {
printf("%d*x^%d + ",temp->heso,temp->bac);
temp=temp->link;
}
}
if((temp2==NULL)&&(temp1!=NULL))
{
temp=temp1;
while(temp!=NULL){
printf("%d*x^%d + ",temp->heso,temp->link);
temp=temp->link;
}
}
}
}
int main()
{
int n,m,i=0,Heso;
printf("nhap bac cua da thuc thu nhat: ");
scanf("%d",&n);
printf("nhap bac cua da thuc thu hai: ");
scanf("%d",&m);
printf("nhap da thuc thu nhat: ");
while(i!=n+1)
{
printf("
nhap he so cua x^%d ",i);
scanf("%d",&Heso);
add(&start1,Heso,i);
i++;
}
i=0;
printf("
nhap he so cua da thuc thu hai: ");
while(i!=m+1)
{
printf("
nhap he so cua x^%d ",i);
scanf("%d",&Heso);
add(&start2,Heso,i);
i++;
}
printf("
ket qua cong hai da thuc la:
");
congdathuc(start1,start2);
printf("0");
getch();
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top