Главная » 2017 » Май » 8 » База данных С++
11:26
База данных С++

#include<iostream>
#include<stdio.h>
using namespace std;
void sozdanie(FILE *x);
void vivod(FILE *x);
void maxmin(FILE *x);
void sum_vozr(FILE *x);
char s[10]="3.txt";
struct chel
{
    char surname[20];
    char name[10];
    char pol;
    int age;
};
int main()
{
    setlocale(LC_ALL, "Russian"); 
    FILE *f;   
    int vibor=1;
    while (vibor)
    {
        cout<<endl;
        cout <<"Введите режим работы: 1 - создание базы; 2 - вывод базы"<<endl;
        cout <<"Введите режим работы: 3 - MaxMin; 4 - Сумма всех возрастов"<<endl;
        cout <<"Введите режим работы: 0 - Выход"<<endl;
        cin >>vibor;
        switch (vibor)
        {
            case 1:sozdanie(f); break;
            case 2:vivod(f); break;
            case 3:maxmin(f); break;
            case 4:sum_vozr(f); break;
        }
    }       
    return 0;
}
void sozdanie(FILE *x)
{
    cout <<"Создание базы данных"<<endl;
    x=fopen(s,"wb");
    int n;
    cout <<"Введите количество записей ="<<endl;
    cin >>n;
    chel dat;
    for (int i=1;i<=n;i++)
    {
        cout<<"Запись "<<i<<endl;
        cout<<"Введите фамилию "<<endl;
        cin>>dat.surname;
        cout<<"Введите имя "<<endl;
        cin>>dat.name;
        cout<<"Введите пол m/w, мужской - m, женский - w "<<endl;
        cin>>dat.pol;
        cout<<"Введите возраст "<<endl;
        cin>>dat.age;
        fwrite(&dat,sizeof(chel),1,x);
    }
    cout <<endl;

    fclose(x);
}
void vivod(FILE *x)
{
    cout <<"База данных"<<endl;
    x=fopen(s,"rb");
    chel dat;
    fread(&dat,sizeof(chel),1,x);
    do
    {
        cout<<dat.surname<<" ";
        cout<<dat.name<<" ";
        cout<<dat.pol<<" ";
        cout<<dat.age<<" ";
        cout <<endl;
    }
    while (fread(&dat,sizeof(chel),1,x));
    fclose(x);
}
void maxmin(FILE *x)
{
    cout <<"Наибольший и наименьший возраст"<<endl;
    x=fopen(s,"rb");
    chel dat, datmax, datmin;
    fread(&dat,sizeof(chel),1,x);
    int max=dat.age;
    datmax=dat;
    datmin=dat;
    int min=dat.age;
    do
    {
        if (dat.age>max) 
        {
            max=dat.age;
            datmax=dat;
        }
        if (dat.age<min) 
        {
            min=dat.age;
            datmin=dat;
        }
    }
    while (fread(&dat,sizeof(chel),1,x));
    
    cout <<endl<<"Максимальный возраст "<<max<<endl;
    cout<<datmax.surname<<" ";
    cout<<datmax.name<<" ";
    cout<<datmax.pol<<" ";
    cout<<datmax.age<<" ";
    cout <<endl;cout <<endl;
    cout <<"Минимальный возраст "<<min<<endl;
    cout<<datmin.surname<<" ";
    cout<<datmin.name<<" ";
    cout<<datmin.pol<<" ";
    cout<<datmin.age<<" ";
    cout <<endl;
    fclose(x);
}
void sum_vozr(FILE *x)
{
    cout <<"Суммарный возраст = ";
    x=fopen(s,"rb");
    chel dat;
    int vozr=0;
    fread(&dat,sizeof(chel),1,x);
    do
    {
        vozr=vozr+dat.age;
    }
    while (fread(&dat,sizeof(chel),1,x));
    fclose(x);
    cout <<vozr<<endl;
}

Просмотров: 400 | Добавил: denjes | Рейтинг: 0.0/0
Всего комментариев: 0
avatar