cho em hỏi bài code của em như vậy là có sai sót gì không ạ! kết quả chương trình chạy đúng theo yêu cầu của đề bài ,nhưng em code trên giấy thì thầy nhận xét là chỉ đúng phần khai báo danh sách còn lại sai hết
Đề bài và code của em đây ạ! Mong anh chị giúp đỡ ạ,em xin cảm ơn ạ!
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<stdio.h>
#include<float.h>
#include<stdlib.h>
using namespace std;
struct thanhpho
{
string ten;
float dientich;
int danso;
};
typedef thanhpho TP;
struct node
{
TP data;
node *pnext;
};
typedef node N;
struct list
{
N *phead;
N *ptail;
};
typedef list L;
void creatlist(L &l)
{
l.phead = l.ptail = NULL;
}
N *creatnode(TP tp)
{
N *p = new N;
if (p == NULL)
{
cout << "\nKhong du bo nho de cap phat!";
return NULL;
}
p->data = tp;
p->pnext = NULL;
return p;
}
void addtail(L &l, N *p)
{
if (l.phead == NULL)
{
l.phead = l.ptail = p;
}
else
{
l.ptail->pnext = p;
l.ptail = p;
}
}
void input1city(TP &tp)
{
cout << "\nNhap ten thanh pho :"; cin.ignore(); getline(cin, tp.ten);
cout << "\nNhap dien tich :"; cin >> tp.dientich;
cout << "\nNhap dan so :"; cin >> tp.danso;
}
void output1city(TP tp)
{
cout << "\nTen thanh pho :" << tp.ten << "\nDien tich : " << tp.dientich << "\nDan so: " << tp.danso;
}
void inputncity(L &l, int n)
{
for (int i = 1; i <= n; i++)
{
TP tp;
cout << "\nNhap info thanh pho thu " << i << ":";
input1city(tp);
N *p = creatnode(tp);
addtail(l, p);
}
}
void outputncity(L l)
{
int dem = 0;
for (N *k = l.phead; k != NULL; k = k->pnext)
{
dem++;
cout << "\nInfo cua thanh pho thu " << dem << ":";
output1city(k->data);
}
}
void xoa(L &l, string tim)
{
if (l.phead == NULL)
{
return;
}
else
{
int dem = 0;
for (N *k = l.phead; k != NULL; k = k->pnext)
{
if (stricmp(k->data.ten.c_str(), tim.c_str()) == 0)
{
dem++;
}
}
if (dem == 0)
{
cout << "\nKhong tim thay !";
return;
}
else
{
if (stricmp(l.phead->data.ten.c_str(), tim.c_str()) == 0)
{
N *g = l.phead;
l.phead = l.phead->pnext;
delete g;
return;
}
else
{
if (stricmp(l.ptail->data.ten.c_str(), tim.c_str()) == 0)
{
for (N *k = l.phead; k != NULL; k = k->pnext)
{
if (k->pnext == l.ptail)
{
delete l.ptail;
k->pnext = NULL;
l.ptail = k;
return;
}
}
}
else
{
N *h = new N;
for (N *k = l.phead; k != NULL; k = k->pnext)
{
if (stricmp(k->data.ten.c_str(), tim.c_str()) == 0)
{
h->pnext = k->pnext;
delete k;
return;
}
h = k;
}
}
}
}
}
}
float timmax(L l)
{
float max = FLT_MIN;
for (N *k = l.phead; k != NULL; k = k->pnext)
{
if (k->data.dientich > max)
{
max = k->data.dientich;
}
}
return max;
}
void inmax(L l)
{
float max = timmax(l);
for (N *k = l.phead; k != NULL; k = k->pnext)
{
if (k->data.dientich==max)
{
output1city(k->data);
}
}
}
void sx(L &l)
{
for (N *k = l.phead; k != l.ptail; k = k->pnext)
{
for (N *q = k->pnext; q != NULL; q = q->pnext)
{
if (k->data.danso > q->data.danso)
{
swap(k->data, q->data);
}
}
}
}
void luu1file(ofstream &fo, TP tp)
{
fo << tp.ten << "," << tp.dientich << "," << tp.danso << endl;
}
void luunhieu(ofstream &fo, L l)
{
for (N *k = l.phead; k != NULL; k = k->pnext)
{
luu1file(fo, k->data);
}
}
void menu()
{
L l;
creatlist(l);
int luachon;
do
{
system("cls");
cout << "\n1-Nhap thong tin n thanh pho ";
cout << "\n2-Xuat thong tin n thanh pho ";
cout << "\n3-Xoa thanh pho ";
cout << "\n4-Thong tin thanh pho co dien tich max ";
cout << "\n5-Xap sep ";
cout << "\n6-Luu file ";
cout << "\n0-Thoat ";
cout << "\nNhap lua chon :"; cin >> luachon;
if (luachon == 1)
{
int n;
cout << "\nNhap vao so luong thanh pho :"; cin >> n;
inputncity(l, n);
}
else
{
if (luachon == 2)
{
outputncity(l);
system("pause");
}
else
{
if (luachon == 3)
{
string tim;
cout << "\nNhap ten ban muon xoa :"; cin.ignore(); getline(cin, tim);
xoa(l, tim);
system("pause");
}
else
{
if (luachon == 4)
{
inmax(l);
system("pause");
}
else
{
if (luachon == 5)
{
sx(l);
system("pause");
}
else
{
if (luachon == 6)
{
ofstream fo;
fo.open("DS.dat", ios::out||ios::);
luunhieu(fo, l);
fo.close();
system("pause");
}
else
{
break;
}
}
}
}
}
}
} while (luachon != 0);
}
int main()
{
menu();
system("pause");
return 0;
}

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?