- •Практичне заняття 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 Теоретична частина
- •Завдання
Практичне заняття 12-13 Теоретична частина
Базується на матеріалі лекцій 26-28: потоки і файли
Завдання
Ввести тексти програм, які розглядалися у лекціях 26-28. Прокомпілювати, проаналізувати результат роботи. До тексту звіту включити лістінги програм та результати їх роботи.
#include<iostream>
#include<string>
#include<stdlib>
using namespace std;
int isFeet(string);
///////////////
class Distance
{
private:
int feet;
float inches;
public:
Distance(): feet(0),inches(0.0)
{ }
Distance (int ft,float in):feet(ft),inches(in)
{ }
void showdist()
{cout <<feet << "\' "<< inches <<"\''";}
void getdist();
};
////////////////
void Distance::getdist()
{string instr;
while (true)
{
cout <<"\n\nVvedit futy: ";
cin.unsetf(ios::skipws);
cin>>instr;
if(isFeet(instr))
{
feet=atoi(instr.c_str()); //перевести
//значення в цілочисельне
break;
}
cout<<"Futy povynni buty cilymy <1000\n";
}//кінець циклу while для футів
while(true)
{
cout<<"Vvedit dujmy: ";
cin.unsetf(ios::skipws);
cin>>inches;
if(inches>=12.0||inches<0.0)
{cout<< "Dujmy povynni buty miz 0 i 11.99\n";
cin.clear(ios::failbit); //штучне встановлення прапору помилки
}
if (cin.good() )//чи все гаразд з cin
{cin.ignore(10,'\n'); //зїсти розділювач
break;
}
cin.clear(); //помилка, очистити статус помилки
cin.ignore(10,'\n');
cout <<"Nepravylno vvedeni dujmy\n";
}
}
//////////////////////////
int isFeet(string str)
{int slen=str.size(); //одержати довжину
if(slen==0||slen>5) //не було даних або забагато
return 0;
for(int j=0;j<slen;j++)
if((str[j]<'0'||str[j]>'9')&&str[j]!='-')
return 0;
double n=atof(str.c_str());
if(n<-999.0||n>999.0)
return 0;
return 1;
}
//////////////////
int main()
{ Distance d;
char ans;
do
{d.getdist();
cout<<"\nVidstan=";
d.showdist();
cout<<"\nWe raz (y/n)?";
cin>>ans;
cin.ignore(10,'\n');
} while (ans!='n');
return 0;
}
Програма 26.1
#include<fstream>
#include<iostream>
#include<string>
#include<conio>
using namespace std;
int main()
{
char ch='x';
int j=77;
double d=6.02;
string str1="Kafka ";
string str2="Proust ";
ofstream outfile("fdata.txt"); //створити обєкт
outfile << ch
<< ' '
<< j
<< ' '
<< d
<< ' '
<< str1
<< ' '
<< str2;
cout <<"File zapysanyj\n";
getch();
return 0;
}
Програма 26.2
#include<fstream>
#include<iostream>
#include<string>
#include<conio>
using namespace std;
int main()
{
char ch;
int j;
double d;
string str1;
string str2;
ifstream infile("fdata.txt"); //створити обєкт
infile>>ch>>j>>d>>str1>>str2;
cout<<ch<<endl
<< j<<endl
<<str1<<endl
<<str2<<endl;
getch();
return 0;
}
Програма 26.3
#include<fstream>
int main()
{
ofstream outfile("TEST.TXT");
outfile<<"11111111111\n";
outfile<<"22222222222\n";
outfile<<"33333333333\n";
outfile<<"44444444444\n";
return 0;
}
Програма 26.4
#include<fstream>
#include <conio>
using namespace std;
int main()
{const int MAX=80;
char buffer[MAX];
ifstream infile("test.txt");
while(!infile.eof())
{infile.getline(buffer,MAX);
cout<<buffer<<endl;
}
getch();
return 0;
}
Програма 26.4а
#include<fstream>
#include <conio>
using namespace std;
int main()
{const int MAX=80;
char buffer[MAX];
ifstream infile("test.txt");
while(infile.good())
{infile.getline(buffer,MAX);
cout<<buffer<<endl;
}
getch();
return 0;
}
Програма 26.4b
#include<fstream>
#include<iostream>
#include<string>
#include<conio>
using namespace std;
int main()
{
string str="11111 111111 "
"22222 222222 ";
ofstream outfile("test.txt");
for(int j=0;j<str.size();j++)
outfile.put(str[j]);
cout<<"File zapysanyj\n";
getch();
return 0;
}
Програма 26.5
#include<fstream>
#include<iostream>
#include<string>
#include<conio>
using namespace std;
int main()
{
char ch;
ifstream infile("test.txt");
while(infile) //читати до кінця чи помилки
{infile.get(ch);//зчитати символ
cout<<ch; //вивести його
}
cout << endl;
getch();
return 0;
}
Програма 26.5а
#include<fstream>
#include<iostream>
#include<string>
#include<conio>
using namespace std;
int main()
{
char ch;
ifstream infile(“test.txt”);//створити вхідний файл
cout<<infile.rdbuf();//передати його буфер в cout
cout << endl;
getch();
return 0;
}
Програма 26.5b
#include <fstream>
#include<iostream>
#include <conio>
using namespace std;
const int MAX=100;
int buff[MAX];
int main()
{ int j=0;
for(j=0;j<MAX;j++)
buff[j]=j;
//створити вихідний потік
ofstream os("edata.dat",ios::binary);
//записати в нього
os.write(reinterpret_cast<char*>(buff),MAX*sizeof(int));
os.close();//закрити його
for(j=0;j<MAX;j++)
buff[j]=0;
ifstream is("edata.dat",ios::binary); //створити вхідний потік
is.read(reinterpret_cast<char*>(buff),MAX*sizeof(int));
for(j=0;j<MAX;j++)
if(buff[j]!=j)
{cerr <<"Nekorektni dani\n";return 1;}
cout<<"Korektni dani\n";
getch();
return 0;
}
Програма 26.6
#include <fstream>
#include<iostream>
#include<conio>
using namespace std;
///////////
class person
{protected:
char name[80];
short age;
public:
void getData()
{ cout<<"Vvedit imia: ";cin>>name;
cout <<"Vvedit vik: ";cin>>age;
}
};
/////////////////
