codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> #include <string.h> #include <conio.h> using namespace std; struct NODE { int data; NODE *pNext,*pPred; };typedef struct NODE NODE; typedef struct { NODE *pHead,*pTail; }LIST; void Init(LIST &L) { L.pHead=L.pTail=NULL; } NODE *CreateNode(int &x) { NODE *newnode=new NODE; if(newnode==NULL) //Khong du bo nho cap phat return NULL; newnode->data=x; newnode->pNext=NULL; newnode->pPred=NULL; return newnode; } void AddHead(LIST &L,NODE *p) { if(L.pHead==NULL) { L.pHead=L.pTail=p; } else { p->pNext=L.pHead; L.pHead->pPred=p; L.pHead=p; } } void AddTail(LIST &L,NODE *p) { if(L.pHead==NULL) { L.pHead=L.pTail=p; } else { L.pTail->pNext=p; p->pPred=L.pTail; L.pTail=p; } } void DisplayHead(LIST L) { if(L.pHead==NULL) return; for(NODE *p=L.pHead;p!=NULL;p=p->pNext) cout<<p->data; } void DeleteHead(LIST &L) { if(L.pHead==NULL) return; else if(L.pHead==L.pTail) { delete L.pHead; Init(L); } else { NODE *p=L.pHead; L.pHead=L.pHead->pNext; delete p; } } int Count(LIST L) { int count=0; if(L.pHead==NULL) return count; for(NODE *p=L.pHead;p!=NULL;p=p->pNext) count++; return count; } void Nhap2Chuoi(LIST &L,LIST &L2,char s[100],char s2[100]) { cout<<"\nNhap So Thu Nhat:"; fflush(stdin); gets(s); cout<<"\nNhap So Thu Hai:"; fflush(stdin); gets(s2); for(int i=0;i<strlen(s);i++) { int temp=s[i]-48; NODE *p=CreateNode(temp); AddTail(L,p); } for(int i=0;i<strlen(s2);i++) { int temp2=s2[i]-48; NODE *q=CreateNode(temp2); AddTail(L2,q); } } void Nhap1Chuoi(LIST &L,char s[100]) { cout<<"\nNhap So:"; fflush(stdin); gets(s); for(int i=0;i<strlen(s);i++) { int temp=s[i]-48; NODE *p=CreateNode(temp); AddTail(L,p); } } void CanBangChuSo(LIST &L,LIST &L2) { int k=0,x=0; int CountL=Count(L),CountL2=Count(L2); if(CountL > CountL2) { k=CountL-CountL2; for(int i=0;i<k;i++) { NODE *p=CreateNode(x); AddHead(L2,p); } } if(CountL2 > CountL) { k=CountL2-CountL; for(int i=0;i<k;i++) { NODE *q=CreateNode(x); AddHead(L,q); } } } void XoaSo0Du(LIST &L) { int dem=0; if(L.pHead->data!=0) return; for(NODE *p=L.pHead;p->pNext!=NULL;p=p->pNext) { if(p->data==0 && p->pNext->data==0) dem++; else break; } for(int i=0;i<dem;i++) DeleteHead(L); DeleteHead(L); } LIST TinhTong(LIST L,LIST L2) { LIST Tong; Init(Tong); int nho=0,x; NODE *z; CanBangChuSo(L,L2); for(NODE *p=L.pTail,*q=L2.pTail;;p=p->pPred,q=q->pPred) { int temp=(p->data)+(q->data)+nho; nho=temp/10; x=temp%10; z=CreateNode(x); AddHead(Tong,z); if(q==L2.pHead) break; } if(nho!=0) { z=CreateNode(nho); AddHead(Tong,z); } return Tong; } LIST operator+(LIST L,LIST L2) { LIST kq; Init(kq); kq=TinhTong(L,L2); return kq; } int SoSanh2So(LIST L,LIST L2) { CanBangChuSo(L,L2); for(NODE *p=L.pHead,*q=L2.pHead;p!=NULL;p=p->pNext,q=q->pNext) { if(p->data > q->data) // Ket luan So Thu 1 > So Thu 2 return 1; else if(p->data < q->data) //Ket luan So Thu 2 > So Thu 1 return 2; } return 0; // 2 so bang nhau } LIST TinhHieu(LIST L,LIST L2) { LIST Hieu; Init(Hieu); int nho=0,x; NODE *z; for(NODE *p=L.pTail,*q=L2.pTail;;p=p->pPred,q=q->pPred) { int temp=(p->data)-(q->data)-nho; if(temp<0) { temp+=10; nho=1;} else { nho=temp/10;} x=temp%10; z=CreateNode(x); AddHead(Hieu,z); if(q==L2.pHead) break; } XoaSo0Du(Hieu); return Hieu; } LIST operator-(LIST L,LIST L2) { LIST kq; Init(kq); kq=TinhHieu(L,L2); return kq; } void RemoveList(LIST &L) { if(L.pHead==NULL) return; while(L.pHead!=NULL) { NODE *p=L.pHead; L.pHead=L.pHead->pNext; delete p; } Init(L); } LIST TinhTich(LIST L,LIST L2) { int x,khong=0,k,k2=0; NODE *z; LIST Tich; Init(Tich); LIST Sum,Sum2; Init(Sum); Init(Sum2); z=CreateNode(khong); AddHead(Sum,z); AddHead(Sum2,z); Sum=Sum+Sum2; if(Count(L)>Count(L2)) k=Count(L); else k=Count(L2); for(NODE *q=L2.pTail;;q=q->pPred) { int nho=0; for(NODE*p=L.pTail;;p=p->pPred) { int temp=(p->data)*(q->data)+nho; nho=temp/10; x=temp%10; z=CreateNode(x); AddHead(Tich,z); if(p==L.pHead) break; } if(nho!=0) { z=CreateNode(nho); AddHead(Tich,z); } for(int i=1;i<=k;i++) { z=CreateNode(khong); AddHead(Tich,z); } for(int j=0;j<k2;j++) { z=CreateNode(khong); AddTail(Tich,z); } k--; k2++; Sum=Sum+Tich; RemoveList(Tich); if(q==L2.pHead) break; } XoaSo0Du(Sum); return Sum; } LIST operator*(LIST L,LIST L2) { LIST kq; Init(kq); kq=TinhTich(L,L2); return kq; } LIST Thuong(LIST L,LIST L2) { int x=1; NODE *z; LIST Temp,thuong; Init(Temp); Init(thuong); for(NODE *p=L.pHead;p!=NULL;p=p->pNext) { int x=p->data; int count=0; z=CreateNode(x); AddTail(Temp,z); while(SoSanh2So(Temp,L2)!=2) { Temp=Temp-L2; count++; } z=CreateNode(count); AddTail(thuong,z); } XoaSo0Du(thuong); return thuong; } LIST operator/(LIST L1,LIST L2) { return Thuong(L1,L2); } LIST LuyThua(LIST L,int somu) { LIST sum1,sum2,luythua; int khong=0,mot=1; Init(sum1); Init(sum2); NODE *z; z=CreateNode(khong); AddHead(sum1,z); z=CreateNode(mot); AddHead(sum2,z); luythua=sum1+sum2; for(int i=0;i<somu;i++) { luythua=luythua*L; } return luythua; } LIST GiaiThua(LIST L) { int mot=1; LIST gt,k; Init(gt); Init(k); NODE*z; z=CreateNode(mot); z=CreateNode(mot); AddHead(gt,z); AddHead(k,z); while(SoSanh2So(L,k)!=2) { gt=gt*L; CanBangChuSo(L,k); L=L-k; } return gt; } int main() { system("color 0a"); int somu,chon; LIST L,L2; char s[100],s2[100]; Init(L); Init(L2); LIST *T=new LIST; LIST *Tich=new LIST; LIST *H=new LIST; LIST *Thuong2=new LIST; LIST *luythuaL=new LIST; LIST *gt=new LIST; do{ system("cls"); cout<<"\t\t|______________COPYRIGHT TUAN NGUYEN______________|\n"; cout<<"\t1.Tinh Tong 2 Chuoi So Nguyen Duong \n\n"; cout<<"\t2.Tinh Hieu 2 Chuoi So Nguyen Duong\n\n"; cout<<"\t3.Tinh Tich 2 Chuoi So Nguyen Duong\n\n"; cout<<"\t4.Tinh Thuong 2 Chuoi So Nguyen Duong\n\n"; cout<<"\t5.Tinh Luy Thua Chuoi So Nguyen Duong\n\n"; cout<<"\t6.Tinh Giai Thua Chuoi So Nguyen Duong\n\n"; cout<<"\t7.Ket thuc\n"; cout<<"Moi Ban Chon:\n"; cin>>chon; switch(chon) { case 1:system("cls");Nhap2Chuoi(L,L2,s,s2);CanBangChuSo(L,L2);*T=L+L2; cout<<"\nTong = ";DisplayHead(*T); RemoveList(L);RemoveList(L2);getch();break; case 2:system("cls");Nhap2Chuoi(L,L2,s,s2);CanBangChuSo(L,L2); if(SoSanh2So(L,L2)==1 || SoSanh2So(L,L2)==0) {*H=L-L2;if(Count(*H)==0) cout<<"Hieu = 0";else cout<<"\nHieu = ";} else if(SoSanh2So(L,L2)==2) {*H=L2-L; if(Count(*H)==0) cout<<"Hieu = 0";else cout<<"\nHieu = - ";} DisplayHead(*H);RemoveList(L);RemoveList(L2);getch();break; case 3:system("cls");Nhap2Chuoi(L,L2,s,s2);CanBangChuSo(L,L2);*Tich=L*L2; cout<<"\nTich = "; if(Count(*Tich)==0) cout<<"0"; else DisplayHead(*Tich);RemoveList(L);RemoveList(L2);getch();break; case 4:system("cls");Nhap2Chuoi(L,L2,s,s2);CanBangChuSo(L,L2);*Thuong2=L/L2; cout<<"\nThuong = "; DisplayHead(*Thuong2);getch();RemoveList(L);RemoveList(L2);break; case 5:system("cls");Nhap1Chuoi(L,s);do{ cout<<"\nNhap Vao So Mu De Tinh Luy Thua:"; cin>>somu; if(somu<0) cout<<"Nhap So Mu >=0!\n"; }while(somu<0);*luythuaL=LuyThua(L,somu);cout<<"\n So Thu Nhat ^ "<<somu<<" = "; DisplayHead(*luythuaL);getch();RemoveList(L);break; case 6:system("cls");cout<<"Nhap n:\n";Nhap1Chuoi(L,s);*gt=GiaiThua(L); cout<<"n! = ";DisplayHead(*gt);getch();RemoveList(L);break; } }while(chon!=7); delete T; delete H; delete Tich; delete Thuong2; delete luythuaL; delete gt; return 0; }
Private
[
?
]
Run code
Submit