В файле содержатся несколько записей структур, в которых есть поле с фамилией(char sname[50]
).Данные из файла переносятся в массив, происходит сортировка слиянием по фамилиям в алфавитном порядке. Какое условие нужно поставить в скобки( "Условие" ), чтобы произошла сортировка массива? или в том месте нужно вызывать функцию?
#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;
}