#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
typedef long KieuDL;
typedef struct NUT
{
struct NUT(long dl)
{
this->duLieu=dl;
this->tiep=NULL;
this->truoc=NULL;
}
KieuDL duLieu;
struct NUT* tiep;
struct NUT* truoc;
}NUT;
class DSLKK
{
private:
int sign;
NUT *dau;
NUT *cuoi;
public:
DSLKK();
~DSLKK();
void themNutOCuoi(KieuDL);
void themNutPOCuoi(NUT *);
void themNutODau(KieuDL);
void themNutPODau(NUT *);
void themNutPTruocQ(NUT *,NUT *);
void themNutPSauQ(NUT *,NUT *);
KieuDL demNut(NUT *);
NUT *timNut(KieuDL);
NUT *nutMin();
void layNutP(NUT *);
void xoaNutP (NUT *);
void duyetDSLKKthuan();
void duyetDSLKKnghich();
void huyDSLKK();
bool DSLKKRong();
void quickSort();
void chuyen(char st[]);
void xuat();
friend DSLKK* tongDuong(DSLKK *,DSLKK *);
friend DSLKK* hieuDuong(DSLKK *,DSLKK *);
friend DSLKK* tich_tam(DSLKK*,NUT *);
friend DSLKK* tong(DSLKK *,DSLKK *,int );
friend DSLKK* hieu(DSLKK *,DSLKK *,int);
friend DSLKK* tich(DSLKK *,DSLKK *,int);
};
int so_sanh_tam(char st1[],char st2[]);
int so_sanh(char st1[],char st2[]);
////////////////////////////////////////////////////////////////////////////////////
DSLKK::DSLKK()
{ dau=cuoi=NULL;}
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::huyDSLKK()
{
NUT *p;
while (dau!=NULL)
{
p=dau;
dau=dau->tiep;
dau->truoc=NULL;
delete p;
}
cuoi=dau=NULL;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK::~DSLKK()
{ huyDSLKK();}
/////////////////////////////////////////////////////////////////////////////////////
bool DSLKK::DSLKKRong()
{ return (dau==NULL ? true:false); }
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::duyetDSLKKthuan()
{
NUT *p=dau;
while(p!=NULL)
{
p=p->tiep;
}
}
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::duyetDSLKKnghich()
{
NUT *p=cuoi;
while(p!=NULL)
{
p=p->truoc;
}
}
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::themNutOCuoi(KieuDL duLieu)
{
NUT *p=new NUT(duLieu);
if(DSLKKRong())
dau=cuoi=p;
else
{
cuoi->tiep=p;
p->truoc=cuoi;
cuoi=p;
}
}
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::themNutODau(KieuDL duLieu)
{
NUT *p=new NUT(duLieu);
if(DSLKKRong())
dau=cuoi=p;
else
{
p->tiep=dau;
dau->truoc=p;
dau=p;
}
}
/////////////////////////////////////////////////////////////////////////////////////
//chuyen tu mang kieu char vao DSLKK
void DSLKK::chuyen(char st[])
{
int i;
if(st[0]=='-')
{
this->sign=-1;
i=1;
}
else
{
this->sign=1;
i=0;
}
while(st[i]!='\0')
{
char x=st[i];
KieuDL tam=atoi(&x);
this->themNutOCuoi(tam);
i++;
}
}
/////////////////////////////////////////////////////////////////////////////////////
void DSLKK::xuat()
{
if(this->sign==-1)
NUT *p;
NUT *q=this->dau;
while(q!=NULL&&q->duLieu==0)
q=q->tiep;
if(q==NULL)
for(p=q;p!=NULL;p=p->tiep)
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * tongDuong(DSLKK *l1,DSLKK *l2)
{
NUT *p=l1->cuoi;
NUT *q=l2->cuoi;
DSLKK *l;
l=new DSLKK();
int s1,s2,sonho=0;
while(p!=NULL && q!=NULL)
{
s1=p->duLieu + q->duLieu+ sonho;
s2=s1%10;
if(p==l1->dau && q==l2->dau)
{
l->themNutODau(s1);
}
else
{
l->themNutODau(s2);
}
sonho=s1/10;
p=p->truoc;
q=q->truoc;
}
while(p!=NULL)
{
s1=p->duLieu+sonho;
s2=s1%10;
if(p==l1->dau)
{
l->themNutODau(s1);
}
else
{
l->themNutODau(s2);
}
sonho=s1/10;
p=p->truoc;
}
while(q!=NULL)
{
s1=q->duLieu+sonho;
s2=s1%10;
if(q==l2->dau)
{
l->themNutODau(s1);
}
else
{
l->themNutODau(s2);
}
sonho=s1/10;
q=q->truoc;
}
l->sign=1;
return l;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * hieuDuong(DSLKK *l1,DSLKK *l2)
{
DSLKK *l=new DSLKK();
int g,h=0;
NUT *p=l1->cuoi;
NUT *q=l2->cuoi;
while(p!=NULL && q!=NULL)
{
g=p->duLieu - h- q->duLieu;
if(g<0)
{
l->themNutODau(10+g);
h=1;
}
else
{
l->themNutODau(g);
h=0;
}
p=p->truoc;
q=q->truoc;
}
while(p!=NULL)
{
g=p->duLieu-h;
if(g<0)
{
l->themNutODau(10+g);
h=1;
}
else
{
l->themNutODau(g);
h=0;
}
p=p->truoc;
}
return l;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * tong(DSLKK *l1,DSLKK *l2,int SST)
{
DSLKK *l=new DSLKK();
if(l1->sign==-1 &&l2->sign==-1)
{
l=tongDuong(l1,l2);
l->sign=-1;
}
if(l1->sign==1 && l2->sign==1)
{
l=tongDuong(l1,l2);
l->sign=1;
}
if(l1->sign==1 && l2->sign==-1)
{
if(SST==1)
{
l=hieuDuong(l1,l2);
l->sign=1;
}
else
if(SST==-1)
{
l=hieuDuong(l2,l1);
l->sign=-1;
}
else
if(SST==0)
{
l->sign=1;
l=hieuDuong(l1,l2);
}
}
if(l1->sign==-1 && l2->sign==1)
{
if(SST==1)
{
l=hieuDuong(l1,l2);
l->sign=-1;
}
else
{
l=hieuDuong(l2,l1);
l->sign=1;
}
}
return l;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * hieu(DSLKK *l1,DSLKK *l2,int SST)
{
DSLKK *l=new DSLKK();
DSLKK *l3=new DSLKK();
NUT *p;
int sign;
sign=l2->sign;
for(p=l2->cuoi;p!=NULL;p=p->truoc)
l3->themNutODau(p->duLieu);
l3->sign=-sign;
l=tong(l1,l3,SST);
return l;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * tich_tam(DSLKK *l,NUT *A)
{
int h=0,t;
DSLKK *lst=new DSLKK();
NUT *p;
for(p=l->cuoi;p!=NULL;p=p->truoc)
{
t=p->duLieu* A->duLieu+ h;
lst->themNutODau(t%10);
h=t/10;
}
if(h!=0)
{
lst->themNutODau(h);
}
lst->sign=1;
return lst;
}
/////////////////////////////////////////////////////////////////////////////////////
DSLKK * tich(DSLKK *l1,DSLKK *l2,int SST)
{
DSLKK *lst=new DSLKK();
DSLKK *temp=new DSLKK();
NUT *run;
NUT *k;
for(k=l2->cuoi;k!=NULL;k=k->truoc)
{
run=l2->cuoi;
temp=tich_tam(l1,k);
while(run!=k)
{
temp->themNutOCuoi(0);
run=run->truoc;
}
lst=tongDuong(lst,temp);
}
if((l1->sign*l2->sign)==1)
lst->sign=1;
else
lst->sign=-1;
return lst;
}
/////////////////////////////////////////////////////////////////////////////////////
int so_sanh_tam(char st1[],char st2[])
{
int len1,len2,i=0,j=0;
if(st1[0]=='-')
i=1;
if(st2[0]=='-')
j=1;
len1=strlen(st1)-i;
len2=strlen(st2)-j;
if(len1>len2)
return 1;
else
if(len1<len2)
return -1;
else
{
for(int k=0;k<len1;k++)
if(atoi(&st1[k+i])>atoi(&st2[k+j]))
return 1;
else
if(atoi(&st1[k+i])<atoi(&st2[k+j]))
return -1;
return 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////
int so_sanh(char st1[],char st2[])
{
if(st1[0]!='-' && st2[0]=='-')
return 1;
if(st1[0]=='-' && st2[0]!='-')
return -1;
if(st1[0]=='-' && st2[0]=='-')
{
if(so_sanh_tam(st1,st2)==1)
return -1;
else
if(so_sanh_tam(st1,st2)==0)
return 0;
else
if(so_sanh_tam(st1,st2)==-1)
return 1;
}
if(st1[0]!='-' &&st2[0]!='-')
{
if(so_sanh_tam(st1,st2)==1)
return 1;
else if(so_sanh_tam(st1,st2)==0)
return 0;
else if(so_sanh_tam(st1,st2)==-1)
return -1;
}
return 0;
}
void main()
{
DSLKK *l1=new DSLKK();
DSLKK *l2=new DSLKK();
DSLKK *l=new DSLKK();
char st1[1001],st2[1001];
// Nhap:
cout<<"Nhap so nguyen thu nhat: "; cout<<"Nhap so nguyen thu hai: "; int ssc=so_sanh_tam(st1,st2);
int ss=so_sanh(st1,st2);
l1->chuyen(st1);
cout<<"Nhap so thu nhat: "; l1->xuat();
l2->chuyen(st2);
cout<<"Nhap so thu hai: "; l2->xuat();
int k=-1;
while(k<5)
{
cout<<"Chon tu 1 den 5"<<endl
; switch(k)
{
case 1:
{
cout<<" Tong hai so la:"; l=tong(l1,l2,ssc);
l->xuat();
}
break;
case 2:
{
cout<<" Hieu hai so la:"; l=hieu(l1,l2,ssc);
l->xuat();
}
break;
case 3:
{
cout<<" Tich hai so la:"; l=tich(l1,l2,ssc);
l->xuat();
}
break;
case 4:
{
if(ss==1)
cout<<" So thu nhat > so thu hai"; if(ss==-1)
cout<<" So thu nhat < so thu hai"; if(ss==0)
cout<<" So thu nhat = so thu hai"; }
break;
}
}
getch();
}