Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
практичні і лабораторні заняття.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
382.98 Кб
Скачать

Int main()

{char ch;

Distance dist;

fstream file;

file.open("DIST.DAT",ios::binary|ios::app|ios::out|ios::in);

do

{cout<<"\nVidstan ";

dist.getdist();

file.write((char*)&dist,sizeof(dist));

cout<<"Continue(y/n)";

cin>>ch;

}while (ch=='y');

file.seekg(0);

file.read((char*)&dist,sizeof(dist));

int count=0;

while(!file.eof())

{ cout<<"\nVidstan "<<++count<<":";

dist.showdist();

file.read((char*)&dist,sizeof(dist));

};

cout<<endl;

getch();

return 0;

}

Завдання 2

Закінчити розробку програми 26.13, дописавши пропущені фрагменти.

Приклад виконання завдання 2

#include<fstream>

#include<iostream>

#include<typeinfo> //для typeid()

#include<conio>

using namespace std;

#include<process.h> //для exit()

const int LEN=32; //максимальна довжина прізвища

const int MAXEN=100;//максимальне число працівників

enum employee_type {tmanager,tscientist,tlaborer};

/////////////

class employee

{private:

char name[LEN];

unsigned long number;

static int n;

static employee* arrap[]; //масив вказівників на клас робітника

public:

virtual void getdata()

{//cin.ignore(10,'/n');

cout<<"Vvedit pip: ";cin>>name;

cout<<"Vvedit nomer: ";cin>>number;

}

virtual void putdata()

{cout<<"\n PIP: "<<name;

cout<<"\n Nomer: "<<number;

}

virtual employee_type get_type();//одержати тип

static void add(); //додати працівника

static void display();//вивести дані про всіх

static void read();//читання з файлу

static void write(); //запис у файл

};

//статичні змінні

int employee::n;

employee* employee::arrap[MAXEN];

////////////////

class manager:public employee

{ private:

char title[LEN];

double dues;

public:

void getdata()

{employee::getdata();

cout<<"Vvedit tytul: ";cin>>title;

cout<<"Vvedit podatok: ";cin>>dues;

}

void putdata()

{employee::putdata();

cout<<"\n Tytul: "<<title;

cout<<"\n Podatok: "<<dues;

}

};

/////////////

class scientist:public employee

{private:

int pubs;

public:

void getdata()

{employee::getdata();

cout<<"Vvedit chyslo publikacij: ";cin>>pubs;

}

void putdata()

{employee::putdata();

cout<<"\n Chyslo pubikacij: "<<pubs;

}

};

//////////

class laborer:public employee

{

};

///////////

//додати працівника до списку

void employee::add()

{char ch;

cout<<"m dla manager"

"\ns dla wchenogo"

"\nl dla robitnyka"

"\nWash vybir: ";

cin>>ch;

switch(ch)

{case 'm':arrap[n]=new manager;break;

case 's':arrap[n]=new scientist;break;

case 'l':arrap[n]=new laborer; break;

default:cout<<"\n Nevidomyj typ\n";return;

}

arrap[n++]->getdata();

}

void employee::display()

{int j;

for (j=0;j<n;j++)

{cout<<(j+1);

switch(arrap[j]->get_type())

{case tmanager: cout<<" Typ: Manager";break;

case tscientist: cout<<"Typ: Vchenyj";break;

case tlaborer:cout<<"Typ: Robitnyk";break;

default:cout<<"Nevidomyj typ";

}

arrap[j]->putdata();

cout<<endl;

}

}

/////////

//повернення типу обєкту

employee_type employee::get_type()

{

if (typeid(*this)==typeid(manager))

return tmanager;

else if(typeid(*this)==typeid(scientist))

return tscientist;

else if(typeid(*this)==typeid(laborer))

return tlaborer;

else

{cerr<<"\n Nepravylnyj typ robitnyka ";exit(1);}

return tmanager;

}

//==============

//записати всі обєкти з памяті в файл

void employee::write()

{int size,j;

cout<<"Ide zapys "<<n<<" robitnykiv \n";

ofstream ouf;

employee_type etype;

ouf.open("EMPLOY.DAT",ios::trunc|ios::binary);

if(!ouf)

{cout<<"\nNemozlyvo vidkryty file\n";getch();return;}

for(j=0;j<n;j++)

{etype=arrap[j]->get_type();

ouf.write( (char*)&etype,sizeof(etype));

switch(etype)

{case tmanager:size=sizeof(manager);break;

case tscientist:size=sizeof(scientist);break;

case tlaborer: size=sizeof(laborer);break;

}

ouf.write((char*)arrap[j],size);

if(!ouf)

{cout<<"\nZapys u file nemozlyvyj\n";return;}

}

}

//----------

//Читання всіх даних з файлу в память

void employee::read()

{int size;

employee_type etype;

ifstream inf;

inf.open("EMPLOY.DAT",ios::binary);

if(!inf)

{cout <<"\nNemozlyvo vidkryty file\n";getch();return;}

n=0; //в памяті працівників нема

while(true)

{inf.read((char*)&etype,sizeof(etype));

if(inf.eof()) //вихід з циклу по eof

break;

if(!inf)

{cout <<"\nNemozlyvo chytaty type\n";getch();return;}

switch(etype)

{case tmanager:

arrap[n]=new manager;

size=sizeof(manager);

break;

case tscientist:

arrap[n]=new scientist;

size=sizeof(scientist);

break;

case tlaborer:

arrap[n]=new laborer;

size=sizeof(laborer);

break;

defaults:cout<<"\nNevidomyj typ u faili\n";getch();return;

}

inf.read((char*)arrap[n],size);

if(!inf)

{cout<<"\nChytanna z failu nemozlyve\n";getch();return;}

n++;

}//end while

cout<<"Ide chytanna "<<n<<" pracivnykiv\n";

}

/////////////

void main()

{char ch;

while(true)

{

cout<<"a - Dodavanna vidomostej"

"\nd - Vyvesty vidomosti pro vsih"

"\nw - Zapysaty vsi dani v fail"

"\nr - Prochytaty vsi dani z failu"

"\nx - Vyhid"

"\n Vash vybir:";

cin>>ch;

switch(ch)

{case 'a':

employee::add();break;

case 'd':

employee::display();break;

case 'w':

employee::write();break;

case 'r':

employee::read();break;

case 'x':

exit(0);

default:cout<<"\nNevidoma komanda";

}//end switch

} ; //end while

}