01/10/2018, 14:19
Chỉ giúp em có lỗi sai sót không ạ
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 << "
Khong 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 << "
Nhap ten thanh pho :"; cin.ignore(); getline(cin, tp.ten);
cout << "
Nhap dien tich :"; cin >> tp.dientich;
cout << "
Nhap dan so :"; cin >> tp.danso;
}
void output1city(TP tp)
{
cout << "
Ten thanh pho :" << tp.ten << "
Dien tich : " << tp.dientich << "
Dan so: " << tp.danso;
}
void inputncity(L &l, int n)
{
for (int i = 1; i <= n; i++)
{
TP tp;
cout << "
Nhap 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 << "
Info 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 << "
Khong 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 << "
1-Nhap thong tin n thanh pho ";
cout << "
2-Xuat thong tin n thanh pho ";
cout << "
3-Xoa thanh pho ";
cout << "
4-Thong tin thanh pho co dien tich max ";
cout << "
5-Xap sep ";
cout << "
6-Luu file ";
cout << "
0-Thoat ";
cout << "
Nhap lua chon :"; cin >> luachon;
if (luachon == 1)
{
int n;
cout << "
Nhap 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 << "
Nhap 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;
}
Bài liên quan
Mình test đúng mà :?
Chỉ thấy mỗi bugs nhập thành phố vào, xóa đi hết rồi sắp xếp sẽ bị crash.
Hay là lại ko đúng ý thầy nhể :?
Dạ thật sự em cũng không hiểu nữa à khi code chạy thì đúng,nhưng thầy lại chấm sai và không giải thích gì thêm ạ.Mà em chỉ cần biết đúng là ok rồi,tại em chỉ cần như vậy thôi ạ,còn lỗi đó để em sửa lại.em cảm ơn anh ạ