Академический Документы
Профессиональный Документы
Культура Документы
#include <iostream>
int main()
setlocale(LC_ALL, "rus");
cout << "Вы хотите выполнить другую операцию (1 - да, 0 - нет)? ";
variable == 1 ? cout << "Выберите операцию!\n..........\n\n" : cout << "До свидания! Не забудьте
взять чек!\n\n";
return 0;
введенное значение. Если условие variable == 1 возвращает true (то есть если
второй код.
если 0:
#include <iostream>
#include <list>
struct Pair
char value;
size_t priority;
value(v),
priority(p)
{}
};
class PriorityQueue
list<Pair> queue;
public:
queue.insert(it, elem);
return;
queue.push_back(elem);
char dequeue()
queue.erase(queue.begin());
return result;
size_t size()
return queue.size();
char top()
return queue.front().value;
}
bool isEmpty()
return queue.size() == 0;
};
3. Практичне завдання
Відкрити бінарний файл, заповнений випадковими
цілими числами в діапазоні [-10...20]. Кількість чисел
у файлі заздалегідь невідома. Знайти максимальний і
мінімальний елемент у файлі. Всі числа, що
знаходяться між ними вивести на екран в зворотному
порядку. Масиви не використовувати.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
void read_files(fstream&, fstream&);
void fillFile(const int, fstream&, fstream&);
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";
int col = rand()%31 -10;
fstream stream1(f_name1, ios::in | ios::out | ios::trunc | ios::binary);
fstream stream2(f_name2, ios::in | ios::out | ios::trunc | ios::binary);
if (!stream1)
{
cout << "Файл не был открыт";
return 0;
}
fillFile(col, stream1);
fillFile(col, stream1, stream2);
read_files(stream1, stream2);
}
void fillFile(const int col, fstream& stream1, fstream& stream2) {
int min = INT_MAX, max = INT_MIN, countMax{}, countMin{}, val{};
stream1.seekg(0);
for (int i = 0; stream1.peek() != -1; i++)
{
stream1.read((char*)&val, sizeof(val));
if (val < min)
min = val;
if (val > max)
max = val;
}
stream1.seekg(0);
for (int i = 0; stream1.peek() != -1; i++)
{
stream1.read((char*)&val, sizeof(val));
countMin++;
if (val == min)
break;
}
stream1.seekg(0);
for (int i = 0; stream1.peek() != -1; i++)
{
stream1.read((char*)&val, sizeof(val));
countMax++;
if (val == max)
break;
}
for (int i = 0; i != countMax - countMin - 1 && countMax > countMin; i++)
{
stream1.seekg((countMax - i - 2) * sizeof(countMax));
stream1.read((char*)&val, sizeof(val));
stream2.write((char*)&val, sizeof(val));
}
for (int i = 0; i != countMin - countMax - 1 && countMax < countMin; i++)
{
stream1.seekg((countMin - i - 2) * sizeof(countMin));
stream1.read((char*)&val, sizeof(val));
stream2.write((char*)&val, sizeof(val));
}
}
void fillFile(const int col, fstream& stream)
{
for (int i = 0; i < col; i++)
{
int val = rand() % 19+3;
stream.write((char*)&val, sizeof(val));
}
}
void read_files(fstream& stream1, fstream& stream2)
{
cout << "Первый файл:" << endl;
stream1.clear();
stream1.seekg(0);
while (stream1.peek() != -1)
{
int number 1;
stream1.read((char*)&number 1, sizeof(int));
cout << number 1 << " ";
}
cout << endl;
cout << "Второй файл:" << endl;
stream2.clear();
stream2.seekg(0);
while (stream2.peek() != -1)
{
int number;
stream2.read((char*)&number, sizeof(int));
cout << number << " ";
}
}