How to sort the alphabet list
-
How do I sort out three alphabet files? How to perform alphabet alphabet class using the student's name before I record it. That's the file that's supposed to be inscribed on the list already classified. http://ideone.com/Xtg4g5 ♪
Baseline:
student[i].Family
- Name of studentstudent[i].Name
- Name of student.student[i].Sex
- Paul.student[i].Date[0]
- numberstudent[i].Date[1]
- month.student[i].Date[2]
- Birth yearstudent[i].City
- The city of birth.case 2:
char f4[20]; FILE *z1; cout << "\nВведите имя файла для чтения: "; //fscanf(v, "%d", &m); cin >> f4; z1 = fopen(f4, "r"); for (int i = 0; i < N; i++) fscanf(z1, "%s%s%s%d%d%d%s", &student[i].Family, &student[i].Name, &student[i].Sex, &student[i].Date[0], &student[i].Date[1], &student[i].Date[2], &student[i].City); cout << "\nЧтение из файла " << f4 << ":\n"; for (int i = 0; i < N; i++) cout << student[i].Family << " " << student[i].Name << " " << student[i].Sex << " " << student[i].Date[0] << "." << student[i].Date[1] << "." << student[i].Date[2] << " " << student[i].City << "\n"; cout << "File name? "; char girls[50]; cin.getline(girls, 50); f = fopen("girls.txt", "a+"); for (int i = 0; i < N; i++) if (strcmp(student[i].Sex, "Female") == 0) { student[i].Show(); } //сортировка по алфавиту это то что я сам сейчас попробовал но это не работает походу while (a == 1) { a = 0; for (int i = 0; i<N - 1; i++){ if (strcmp(student[i].Family, student[i + 1].Family)>0) { strcpy(tmp, student[i].Family); strcpy(student[i].Family, student[i + 1].Family); strcpy(student[i + 1].Family, tmp); /*strcpy(tmp, fr[i].dolgnost); strcpy(fr[i].dolgnost, fr[i + 1].dolgnost); strcpy(fr[i + 1].dolgnost, tmp); tmp1 = fr[i].stag; fr[i].stag = fr[i + 1].stag; fr[i + 1].stag = tmp1;*/ a = 1; } } }// вот до этого момента новое. fclose(f); //С помощью цикла показываем всех девочек. system("pause"); break;
-
Comments already mentioned that a collection and a sort function could be used. Create a collection. Fill the list of values.
list<Student> lines;
Redesign the operator more (or less) for your structure.
bool Student::operator < (const Student& rhs) const { return ... }
Then use the function sort().
lines.sort();
You can also compare the letter codes. http://book.itep.ru/10/ascii.htm ♪
This is an example of a simple software using symbol codes:
char Name1 [5]={'B','C','D','Z','Y'}; char Name2 [5]={'A','c','E','T','k'}; for(int i =0 ; i<5;i++){ cout<<Name1[i]-Name2[i]<<" "; }
Out:
1 -32 -1 6 -18
In your case, it is necessary to use a normal method of sorting so that at the beginning of the list there is a name with the smallest code of the first symbol.