Đây là đề bài ak:
còn dưới đây là đoạn code của e tự viết nhờ mọi người fix lỗi giúp e. E có chạy được nhưng khi nhập những yêu cầu thì lại không được…:
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdio.h>
using namespace std;
struct khachhang
{
std::string makh;
std::string tenkh;
int tuoi;
std::string tinh;
std::string dt;
};
typedef struct khachhang Khachhang;
struct node
{
Khachhang *data;
node *pNext;
};
typedef struct node Node;
struct singlelist
{
Node *pHead;
};
typedef singlelist SingleList;
void khoitaodslkd(SingleList *&list)
{
list=new SingleList;
list->pHead= NULL;
}
//tao node khach hang
Node* taoNode(Khachhang* kh)
{
Node *pNode=new Node;
if(pNode!=NULL)
{
pNode->data=kh;
pNode->pNext=NULL;
return pNode;
}
else
{
cout<<"---Cap phat bo nho that bai---";
}
}
void nhapKH(SingleList *&list,Khachhang *kh)
{
cout<<"\n---Nhap vao thong tin khach hang--- "<<endl;
fflush(stdin);
cout<<"\nNhap ma khach hang: ";
getline(cin,kh->makh);
fflush(stdin);
cout<<"\nNhap ten khach hang: ";
getline(cin,kh->tenkh);
cout<<"\nNhap tuoi khach hang: ";
cin>>kh->tuoi;
fflush(stdin);
cout<<"\nNhap tinh: ";
getline(cin,kh->tinh);
fflush(stdin);
cout<<"\nNhap so dien thoai: ";
getline(cin,kh->dt);
}
void xuatNode(Node *p)
{
cout<<setw(20)<<p->data->makh<<setw(20)<<p->data->tenkh<<setw(20)<<p->data->tuoi<<setw(20)<<p->data->tinh<<setw(20)<<p->data->dt<<endl;
}
void xuat(SingleList *list)
{
cout<<setw(20)<<"MA KHACH HANG"<<setw(20)<<"TEN"<<setw(20)<<"TUOI"<<setw(20)<<"TINH"<<setw(20)<<"SDT"<<endl;
/* Node *pTmp=list->pHead;
if(pTmp==NULL)
{
cout<<"Danh sach rong";
return;
}
else
{
Khachhang *kh=pTmp->data;
cout<<setw(20)<<kh->makh<<setw(20)<<kh->tenkh<<setw(20)<<kh->tuoi<<setw(20)<<kh->tinh<<setw(20)<<kh->dt<<endl;
pTmp=pTmp->pNext;
} */
for (Node *k=list->pHead;k!=NULL;k->pNext)
{
xuatNode(k);
}
}
//tim kiem khach hang tu ma khach hang
void timkh(SingleList *&list)
{
string ma;
cout<<"Nhap ma khach hang can tim: ";cin.ignore();getline(cin,ma);
Node *pSearch=list->pHead;
while(pSearch!=NULL)
{
if(pSearch->data->makh.compare(ma)==0)
xuatNode(pSearch);
else
pSearch=pSearch->pNext; //neu chua tim thay thi duyet phan tu ke tiep
}
/*
for(Node *pSearch=list->pHead;pSearch!=NULL;pSearch=pSearch->pNext)
{
if(pSearch->data->makh.compare(ma)==0)
xuatNode(pSearch);
}*/
}
//them mot khach hang vao dau danh sach
void InsertFirst(SingleList *&list,Khachhang *kh)
{
Node *pNode= taoNode(kh);//khoi tao node moi
if(list->pHead==NULL)
{
list->pHead==pNode; //neu mang rong thi pHead chinh la pNode
}
else
{
pNode->pNext=list->pHead;//tro next cua pNode =pHead hien tai
list->pHead=pNode;// Doi pHead hien tai=pNode(pNode tro thanh pHead moi )
}
}
//xoa node
void DelNode(SingleList *&list)
{
string ma;
cout<<"Nhap vao ma khach hang can xoa: ";getline(cin,ma);
Node *pDel =list->pHead;
if(pDel==NULL)
{
cout<<"Danh sach rong....";
}
else
{
Node *pPre=NULL;
while(pDel!=NULL)
{
Khachhang *kh=pDel->data;
if(kh->makh==ma)
break;
pPre=pDel;
pDel=pDel->pNext;
}
if(pDel==NULL)
{
cout<<"Khong tim thay ma khach hang: "<<ma;
}
else
{
if(pDel==list->pHead)
{
list->pHead=list->pHead->pNext;
pDel->pNext=NULL;
delete pDel;
pDel=NULL;
}
else
{
pPre->pNext=pDel->pNext;
pDel->pNext=NULL;
delete pDel;
pDel=NULL;
}
}
}
}
void timtinh(SingleList *&list)
{
string t;
cout<<"Nhap tinh khach hang can tim: ";cin.ignore();getline(cin,t);
Node *pSearch=list->pHead;
while(pSearch!=NULL)
{
if(pSearch->data->tinh.compare(t)==0)
cout<<setw(20)<<pSearch->data->makh<<setw(20)<<pSearch->data->tenkh<<setw(20)<<pSearch->data->tuoi<<setw(20)<<pSearch->data->tinh<<setw(20)<<pSearch->data->dt<<endl;
pSearch=pSearch->pNext;
}
}
//Dem so khach hang co so tuoi lon hon 50
void dem(SingleList *&list)
{
int dem=0;
Node *pSearch=list->pHead;
while(pSearch!=NULL)
{
if(pSearch->data->tuoi>50)
dem++;
}
cout<<"So khach hang co so tuoi lon hon 50 la: "<<dem;
}
//Sap xep ds tang dan theo tuoi, sd SelectionSort
void Sort(SingleList *&list)
{
Node *q,*min,*p;
p=list->pHead;
while(p!=NULL)
{
min=p;q=p->pNext;
while(q!=NULL)
{
if(q->data->tuoi < min->data->tuoi)
min=q;
q=q->pNext;
}
swap(p->data,min->data);
p=p->pNext;
}
}
void insertAndsort(SingleList *&list, Khachhang *x)
{
Node *p = new Node;
p->data = x;
if (list->pHead == NULL)
{
list->pHead = p;
list->pHead->pNext = NULL;
}
else
{
p->pNext=list->pHead;
list->pHead = p;
}
Sort(list);
}
//Huy toan bo danh sach
void Delist(SingleList *&list)
{
Node *pTemp= NULL;
while(list->pHead!=NULL)
{
pTemp=list->pHead;
list->pHead=list->pHead->pNext;
free(pTemp); //giai phong con tro
}
}
//luu tru danh sach khach hang vao file text
//ham ghi thong tin 1 khach hang vao file
void ghithongtinKH(ofstream &fileout,Khachhang *kh)
{
fileout<<kh->makh<<", ";
fileout<<kh->tenkh<<", ";
fileout<<kh->tuoi<<", ";
fileout<<kh->tinh<<", ";
fileout<<kh->dt;
}
void luu_file(SingleList *&list)
{
ofstream fileout;
fileout.open("DANHSACHKHACHHANG.TXT",ios::out);
for(Node *k=list->pHead;k!=NULL;k=k->pNext)//dung vong lap de luu tat ca thong tin khach hang
{
ghithongtinKH(fileout,k->data);
fileout<<endl;
}
fileout.close();
}
int main()
{
int n,k;
SingleList *list;
khoitaodslkd(list);
Khachhang *kh=new Khachhang;;
cout<<"Nhap so khach hang: ";cin>>k;
for(int i=1;i<=k;i++)
{
cout<<"nhap thong tin khach hang thu: "<<i;
nhapKH(list,kh);
Khachhang *x=new Khachhang;
InsertFirst(list,x);
}
cout<<"MENU LUA CHON YEU CAU:"<<endl;
cout<<"1.HIEN THI TOAN BO DANH SACH;"<<endl;
cout<<"2.TIM MOT KHACH HANG THEO MA KHACH HANG;"<<endl;
cout<<"3.THEM MOT KHACH HANG VAO DAU DANH SACH;"<<endl;
cout<<"4.XOA MOT KHACH HANG KHOI DANH SACH TU MA KHACH HANG;"<<endl;
cout<<"5.NHAP MOT TINH VA IN RA DANH SACH KHACH HANG O TINH DO;"<<endl;
cout<<"6.DEM SO KHACH HANG TREN 50 TUOI;"<<endl;
cout<<"7.SAP XEP DANH SACH TANG DAN THEO TUOI;"<<endl;
cout<<"8.THEM MOT KHACH HANG VA VAN GIU NGUYEN THU TU TANG DAN;"<<endl;
cout<<"9.HUY TOAN BO DANH SACH;"<<endl;
cout<<"10.LUU TRU DANH SACH KHACH HANG VAO FILE TEXT"<<endl;
cin>>n;
switch(n)
{
case 1:
{
system("cls");
cout<<"Danh sach khach hang: "<<endl;
xuat(list);
break;
}
case 2:
{
system("cls");
timkh(list);
break;
}
case 3:
{
system("cls");
Khachhang *x=new Khachhang;
cout<<"Nhap thong tin khach hang can them: "<<endl;
nhapKH(list,x);
InsertFirst(list,kh);
break;
}
case 4:
{
system("cls");
DelNode(list);
xuat(list);
break;
}
case 5:
{
system("cls");
timtinh(list);
break;
}
case 6:
{
system("cls");
dem(list);
break;
}
case 7:
{
system("cls");
Sort(list);
xuat(list);
break;
}
case 8:
{
system("cls");
Khachhang *x=new Khachhang;
cout<<"Nhap thong tin khach hang can them: "<<endl;
nhapKH(list,x);
insertAndsort(list,x);
break;
}
case 9:
{
system("cls");
Delist(list);
break;
}
case 10:
{
system("cls");
luu_file(list);
break;
}
default:
{
cout<<"Sai cu phap. Xin moi nhap lai!!!!";
}
}
}
Nhờ mọi người sửa lại đoạn code ds liên kết đơn với ạ. Em cảm ơn