- •Практичне заняття 8 Теоретична частина
- •Завдання
- •Int main()
- •Void push(int var)
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 9 Теоретична частина
- •Зміст завдання
- •Лабораторне заняття 10 Теоретична частина
- •Практичне заняття 9 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Практичне заняття 10-11 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 11-12 Теоретична частина
- •Завдання 1
- •Завдання 2
- •Завдання 3
- •Int main()
- •Практичне заняття 12-13 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 13 Теоретична частина
- •Завдання 1
- •Int main()
- •Завдання 2
- •Практичне заняття 14 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Лабораторне заняття 14 Теоретична частина
- •Завдання
Int main()
{ ifstream file;
file.open("a:test.dat");
if(!file)
cout<<"\nNemozlyvo vidkryty file";
else
cout<<"\nFile vidkrytyj bez pomylok ";
cout<<"\nfile= "<<file;
cout<<"\nKod pomylky="<<file.rdstate();
cout<<"\ngood()="<<file.good();
cout<<"\neof()="<<file.eof();
cout<<"\nfail()="<<file.fail();
cout<<"\nbad()="<<file.bad()<<endl;
file.close();
getch();
return 0;
}
Програма 26.11
#include<fstream>
#include<iostream>
#include<conio>
using namespace std;
class person
{protected:
char name[40];
int age;
public:
void getData()
{cout<<”\n Vvedit pip “;cin>>name;
cout<<”Vvedit vik “;cin>>age;
}
void showData()
{cout<<”\n Name “<<name;
cout<<”\nVik “<<age;
}
void diskin(int);
void diskout();
static int diskcount();
};
//////////////////
void person::diskin(int pn) //читання даних про pn осіб
{ifstream infile; //створити файл
infile.open(“persfile.dat”,ios::binary);//відкрити файл
infile.seekg(pn*sizeof(person));
infile.read((char*)this,sizeof(*this));
}
void person::diskout() //запис в кінець файлу
{ofstream outfile;
outfile.open(“persfile.dat”,ios::app|ios::binary);
outfile.write((char*)this,sizeof(*this)); //записати в файл
}
int person::diskcount()
{ifstream infile;
infile.open(“persfile.dat”,ios::binary);
infile.seekg(0,ios::end);
return (int)infile.tellg()/sizeof(person);
}
/////////////
Int main()
{ person p;
char ch;
int n,j;
do
{cout<<”Vvedit dani pro osobu “;
p.getData();
p.diskout();
cout<<”Continue(y/n)”;
cin>>ch;
} while (ch==’y’);
n=person::diskcount();
cout<<”V faili “<<n<<” osib\n”;
for(j=0;j<n;j++)
{cout<<”\nOsoba “<<j;
p.diskin(j);
p.showData();
};
cout<<endl;
getch();
return 0;
}
Програма 26.12
#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()
{//реалізувати метод для цього класу
}
void putdata()
{//реалізувати метод для цього класу
}
};
//////////
class laborer:public employee
{
};
///////////
//додати працівника до списку
void employee::add()
{char ch;
//реалізувати меню вводу різних типів працівників
arrap[n++]->getdata();
}
void employee::display()
{//реалізувати меню відображення різних типів працівників
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;
//реалізувати реакцію на ввід
} ; //end while
}
Програма 26.13
#include<iostream>
#include<conio>
using namespace std;
class Distance
{private:
int feet;
float inches;
public:
Distance():feet(0),inches(0.0)
{ }
Distance(int ft,float in):feet(ft),inches(in)
{ }
friend istream& operator>>(istream& s,Distance& d);
friend ostream& operator<<(ostream& s,Distance& d);
};
//////
istream& operator>>(istream& s,Distance& d)
{ cout<<”\nVvedit futy: “;s>>d.feet;
cout<<”\nVvedit dujmy: “;s>>d.inches;
return s;
};
////////////
ostream& operator<<(ostream& s,Distance& d)
{s<<d.feet<<”\’-“<<d.inches<<’\”’;
return s;
}
/////////////
