The file contains several records of structures in which there is a field with a surname ( char sname[50] ). The data from the file is transferred to the array, sorting is performed by name in alphabetical order. What condition should be put in brackets ("Condition") for the array to be sorted? or is it necessary to call a function in that place?
#include "stdafx.h" #include<iostream> #include<cstring> using namespace std; struct contact { char nomer[50]; char adress[50]; char sname[50]; }; int base_output(contact *buf, int kol_el) { cout << "ΠΠΎΠ»Π½ΡΠΉ ΡΠΏΠΈΡΠΎΠΊ ΠΊΠΎΠ½ΡΠ°ΠΊΡΠΎΠ² Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
\n"; cout << "-----------------------------------------------------------\n"; for (int i = 0; i < kol_el; i++) { cout << (i + 1) << ".) " << buf[i].sname; cout << "\n" << buf[i].nomer; cout << "\n" << buf[i].adress; cout << "\n\n"; } cout << "-----------------------------------------------------------\n"; system("pause"); return 0; } void Merge(contact *buf, int first, int last) { int middle, start, final, j; contact *mas = new contact[100]; middle = (first + last) / 2; start = first; final = middle + 1; for (j = first; j <= last; j++) if ((start <= middle) && ((final > last) || ("ΡΡΠ»ΠΎΠ²ΠΈΠ΅"))) //Π·Π°ΠΌΠ΅Π½Π° ΡΡΠ»ΠΎΠ²ΠΈΡ { mas[j] = buf[start]; start++; } else { mas[j] = buf[final]; final++; } for (j = first; j <= last; j++) buf[j] = mas[j]; delete[]mas; } void MergeSort(contact *buf, int first, int last) { if (first<last) { MergeSort(buf, first, (first + last) / 2); MergeSort(buf, (first + last) / 2 + 1, last); Merge(buf, first, last); } } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "RUS"); FILE *f; f = fopen("telbook.dat", "r+b"); fseek(f, 0, SEEK_END); int size = ftell(f); int kol_el = size / sizeof(contact); contact *buf = new contact[kol_el]; fseek(f, 0, SEEK_SET); fread(buf, sizeof(contact), kol_el, f); fclose(f); int first = 0; int last = kol_el; unsigned int rezh = 0; do { system("cls"); const int NotUsed = system("color 03"); cout << "ΠΡΠ±Π΅ΡΠΈΡΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅:\n"; cout << "1.ΠΠΎΠΊΠ°Π·Π°ΡΡ Π±Π°Π·Ρ ΠΊΠΎΠ½ΡΠ°ΠΊΡΠΎΠ².\n"; cout << "2.Π‘ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° ΠΏΠΎ Π°Π»ΡΠ°Π²ΠΈΡΡ.(ΡΠ»ΠΈΡΠ½ΠΈΠ΅ΠΌ)\n"; cout << "3.ΠΡΠΉΡΠΈ ΠΈΠ· ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ.\n"; cin >> rezh; switch (rezh) { case 1: { base_output(buf,kol_el); break; } case 2: { MergeSort(buf, first, last); break; } } } while (rezh != 3); system("cls"); system("pause"); return 0; } #include "stdafx.h" #include<iostream> #include<cstring> #include<cstdlib> using namespace std; struct contact { char nomer[50]; char adress[50]; char sname[50]; }; int base_output(contact *buf, int kol_el) { cout << "ΠΠΎΠ»Π½ΡΠΉ ΡΠΏΠΈΡΠΎΠΊ ΠΊΠΎΠ½ΡΠ°ΠΊΡΠΎΠ² Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
\n"; cout << "-----------------------------------------------------------\n"; for (int i = 0; i < kol_el; i++) { cout << (i + 1) << ".) " << buf[i].sname; cout << "\n" << buf[i].nomer; cout << "\n" << buf[i].adress; cout << "\n\n"; } cout << "-----------------------------------------------------------\n"; system("pause"); return 0; } void Merge(contact *buf, int first, int last) { int middle, start, final, j; contact *mas = new contact[100]; middle = (first + last) / 2; start = first; final = middle + 1; for ( j = first; j < last; j++) //j<=last if ((start < middle) && ((final > last) || (strcmp(buf[start].sname,buf[final].sname)<0))) //start <= middle { mas[j] = buf[start]; start++; } else { mas[j] = buf[final]; final++; } for ( j = first; j < last; j++) //j<=last buf[j] = mas[j]; delete[]mas; } void MergeSort(contact *buf, int first, int last) { if (first<last) { MergeSort(buf, first, (first + last) / 2); MergeSort(buf, (first + last) / 2 + 1, last); Merge(buf, first, last); } } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "RUS"); FILE *f; f = fopen("telbook.dat", "r+b"); fseek(f, 0, SEEK_END); int size = ftell(f); int kol_el = size / sizeof(contact); contact *buf = new contact[kol_el]; fseek(f, 0, SEEK_SET); fread(buf, sizeof(contact), kol_el, f); fclose(f); int first = 0; int last = kol_el; unsigned int rezh = 0; do{ system("cls"); const int NotUsed = system("color 03"); cout << "ΠΡΠ±Π΅ΡΠΈΡΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅:\n"; cout << "1.ΠΠΎΠΊΠ°Π·Π°ΡΡ Π±Π°Π·Ρ ΠΊΠΎΠ½ΡΠ°ΠΊΡΠΎΠ².\n"; cout << "2.Π‘ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° ΠΏΠΎ Π°Π»ΡΠ°Π²ΠΈΡΡ.(ΡΠ»ΠΈΡΠ½ΠΈΠ΅ΠΌ)\n"; cout << "3.ΠΡΠΉΡΠΈ ΠΈΠ· ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ.\n"; cin >> rezh; switch (rezh) { case 1: { base_output(buf,kol_el); break; } case 2: { MergeSort(buf, first, last); break; } } } while (rezh != 3); system("cls"); system("pause"); return 0; }