#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
struct person
{
char name[20];
int age;
};
void sozdanie(FILE *file);
void print(FILE *file);
void max(FILE *file);
void min(FILE *file);
int main()
{
setlocale(LC_ALL, "Russian");
FILE *f;
char s[20]="d:/files/1.txt";
int n;
do
{
cout<<"Выберите: 1-создание, 2-добавление, 3-вывод, 4-min, 5-max, 0-выход -> ";
cin>>n;
switch (n)
{
case 1:
{
f=fopen(s,"wb");
sozdanie(f);
fclose(f);
break;
}
case 2:
{
f=fopen(s,"ab");
sozdanie(f);
fclose(f);
break;
}
case 3:
{
f=fopen(s,"rb");
print(f);
fclose(f);
break;
}
case 4:
{
f=fopen(s,"rb");
min(f);
fclose(f);
break;
}
case 5:
{
f=fopen(s,"rb");
max(f);
fclose(f);
break;
}
}
} while (n);
system("PAUSE");
}
void sozdanie(FILE *file)
{
person dat;
int n;
cout<<"Добавить нового? 1-YES, 0-NO "; cin>>n;
while (n)
{
cout<<"Введите фамилию ";cin>>dat.name;
cout<<"Введите возраст ";cin>>dat.age;
fwrite(&dat,sizeof(person),1,file);
cout<<"Добавить нового? 1-YES, 0-NO "; cin>>n;
}
}
void print(FILE *file)
{
person dat;
while (fread(&dat,sizeof(person),1,file))
{
cout<<dat.name<< " "<<dat.age<<endl;
cout<<"---------------------------------------"<<endl;
}
}
void min(FILE *file)
{
person dat;
fread(&dat,sizeof(person),1,file);
int min;
char name_min[20];
min=dat.age;
strcpy(name_min,dat.name);
while (fread(&dat,sizeof(person),1,file))
{
if (dat.age<min)
{
min=dat.age;
strcpy(name_min,dat.name);
}
}
cout<<"Минимальный возраст: "<<name_min<< " "<<min<<endl;
cout<<"---------------------------------------"<<endl;
}
void max(FILE *file)
{
person dat;
fread(&dat,sizeof(person),1,file);
int max;
char name_max[20];
max=dat.age;
strcpy(name_max,dat.name);
while (fread(&dat,sizeof(person),1,file))
{
if (dat.age>max)
{
max=dat.age;
strcpy(name_max,dat.name);
}
}
cout<<"Максимальный возраст: "<<name_max<< " "<<max<<endl;
cout<<"---------------------------------------"<<endl;
}
|