Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Міністерство освіти і науки України

Харківський національний університет радіоелектроніки

Кафедра системотехніки

Дисципліна: «Алгоритмізація та програмування»

ЛАБОРАТОРНА РОБОТА № 3

«ВИВЧЕННЯ МОЖЛИВОСТЕЙ МОВИ С++ ПРИ ОРГАНІЗАЦІЇ


ПОТОКОВОГО ФАЙЛОВОГО ВВЕДЕННЯ-ВИВЕДЕННЯ»

Виконав: Прийняла: к.т.н., доц. каф. СТ


ст. гр. ІТУ-21-1 Білова Тетяна Георгіївна
Снітко Анна Олександрівна
з оцінкою «____________»
«____»_____________

2021р.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
void menu(fstream&, fstream&, fstream&, int, int);
void read_files(fstream&, fstream&);
float MidOfFile(fstream& stream1);
void read_files(fstream&, fstream&, fstream&, int, int);
void fillFile(const int, fstream&);
const int NotUsed = system("color 70");
int main()
{
setlocale(LC_ALL, "rus");
srand(time(0));
const char* f_name1 = "myfile1";
const char* f_name2 = "myfile2";
const char* f_name3 = "myfile3";
int col1 = rand() % 15 + 20;
int col2 = rand() % 15 + 20;
fstream stream1(f_name1, ios::in | ios::out | ios::trunc |
ios::binary);
fstream stream2(f_name2, ios::in | ios::out | ios::trunc |
ios::binary);
fstream stream3(f_name3, ios::in | ios::out | ios::trunc |
ios::binary);
if (!stream1)
{
cout << "The 1st file hasn't been opened";
return 0;
}
else if (!stream2)
{
cout << "The 2nd file hasn't been opened";
return 0;
}
else if (!stream3)
{
cout << "The 3rd file hasn't been opened";
return 0;
}
fillFile(col1, stream1);
fillFile(col2, stream2);
menu(stream1, stream2, stream3, col1, col2);
stream1.close();
stream2.close();
stream3.close();
}
void fillFile(const int col, fstream& stream)
{
for (int i = 0; i < col; i++)
{
float val = (rand() % 200 - 50) / 10.0;
stream.write((char*)&val, sizeof(val));
cout << val << " ";
}
cout << endl;
}
void menu(fstream& stream1, fstream& stream2, fstream& stream3, int col1, int col2)
{
while (1)
{
cout << "1 - Show two initial files" << endl;
cout << "2 - show final file(#3)" << endl;
cout << "3 - Close the program" << endl;
int choice{};
cin >> choice;
switch (choice)
{
case 1:
read_files(stream1, stream2);
break;
case 2:
read_files(stream1, stream2, stream3, col1, col2);
break;
case 3:
return;
}
cout << endl;
system("pause");
system("cls");
}
}
float MidOfFile(fstream& stream1)
{
float valRead1 = 0, sum = 0, count = 0;
stream1.seekg(0);
while (stream1.peek() != -1)
{
stream1.read((char*)&valRead1, sizeof(valRead1));
sum += valRead1;
count++;
}
return sum / count;
}
void read_files(fstream& stream1, fstream& stream2, fstream& stream3, int col1, int col2)
{
float valRead1{}, valRead2{};
float tmp = 0, mid = MidOfFile(stream1);;
stream3.clear();
stream2.clear();
stream1.clear();
stream3.seekg(0);
for (int i = 0; col1 != i && col2 != i;)
{
float tmp = 0, count = 0;
for (int j = 0; col1 != i && col2 != i && j < 4; j++)
{
stream1.seekg((col1 - i - 1) * sizeof(valRead1), ios::beg);
stream2.seekg((col2 - i - 1) * sizeof(valRead2), ios::beg);
stream1.read((char*)&valRead1, sizeof(valRead1));
stream2.read((char*)&valRead2, sizeof(valRead2));
tmp = tmp + valRead1 + valRead2;
i++;
count += 2;
}
tmp = tmp / count;
if (tmp >= mid)
stream3.write((char*)&tmp, sizeof(tmp));
}

if (col1 > col2)


{
for (int i = 0; i <= col1 - col2; i++) {
stream1.seekg((col1 - col2 - i) * sizeof(float));
stream1.read((char*)&valRead1, sizeof(valRead1));
if (valRead1 >= MidOfFile(stream1))
stream3.write((char*)&valRead1, sizeof(valRead1));
}
}
if (col1 < col2)
{
for (int i = 0; i <= col1 - col2; i++) {
stream2.seekg((col2 - col1 - i) * sizeof(float));
stream2.read((char*)&valRead1, sizeof(valRead1));
if (valRead1 >= MidOfFile(stream1))
stream3.write((char*)&valRead1, sizeof(valRead1));
}
}
cout << "Midlle count of first file : " << mid << endl;
stream3.seekg(0);
cout << "Third file:\n";
while (stream3.peek() != -1) {
stream3.read((char*)&valRead2, sizeof(valRead2));
cout << valRead2 << " ";
}

}
void read_files(fstream& stream1, fstream& stream2)
{
cout << "Первый файл:" << endl;
stream1.clear();
stream1.seekg(0);
while (stream1.peek() != -1)
{
float valRead1;
stream1.read((char*)&valRead1, sizeof(float));
cout << valRead1 << " ";
}
cout << endl;
cout << "Второй файл:" << endl;
stream2.clear();
stream2.seekg(0);
while (stream2.peek() != -1)
{
float valRead2;
stream2.read((char*)&valRead2, sizeof(float));
cout << valRead2 << " ";
}
}

Висновок: Вивчили можливості мови С++ при роботі з файлами. Отримали


практичні навички використання файлів при вирішенні практичних задач

You might also like