01/10/2018, 14:15
Sắp xếp sinh viên theo thứ tự abc
chào mọi người, mọi người có thể cho em hỏi? em thấy đoạn code em làm đúng rồi mà tại sao kết ủa xuất ra lại sai ạ?
void splitfirstname(char fullname[], char firstname[])
{
for (size_t i = strlen(fullname) - 1; i >= 0; i--)
{
if (fullname[i] == ' ')
{
strcpy_s(firstname,30,fullname + i+1);
break;
}
}
}
void arragename(char path[], student a[], int n) {
student temp;
ofstream fout;
fout.open(path);
char firstname1[30], firstname2[30];
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n; j++) {
splitfirstname(a[i].name, firstname1);
splitfirstname(a[j].name, firstname2);
int check1 = _strcmpi(firstname1, firstname2);
if (check1 > 0)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else if (check1 == 0)
{
int check2 = _strcmpi(a[i].name, a[j].name);
if (check2 > 0)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
fout << a[i].studentid << endl;
fout << a[i].name << endl;
fout << a[i].idcard << endl;
fout << a[i].dob.day << a[i].dob.month << a[i].dob.year << endl;
fout << a[i].gender << endl;
fout << a[i].avgscores << endl;
}
fout.close();
}
Bài liên quan
Bạn tìm hiểu quick sort ở đây: https://www.geeksforgeeks.org/quick-sort/
dạ em làm ra rồi cám ơn ạ