include pch include iostream include fstream using namespace std struc

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "pch.h"
#include <iostream>
#include <fstream>
using namespace std;
struct Cell {
int num;
int day;
int month;
};
struct tree
{
Cell info;
tree *L;
tree *R;
};
void add(tree* t, Cell* x)
{
tree* q;
if ( (x->day < (t->info).day && x->month == (t->info).month) || x->month < (t->info).month)
{
if (t->L == 0) {
q = new tree;
q->info = *x;
q->L = 0;
q->R = 0;
t->L = q;
}
else {
add(t->L, x);
}
}
if((x->day >= (t->info).day && x->month == (t->info).month) || x->month > (t->info).month){
if (t->R == 0)
{
q = new tree;
q->info = *x;
q->L = 0;
q->R = 0;
t->R = q;
}
else
{
add(t->R, x);
}
}
}
void treeprint(tree *x, ofstream &file) {
if (x != NULL) {
treeprint(x->L, file);
cout << (x->info).num << " ";
file.write((char*)&(x->info).num, sizeof((x->info).num));
treeprint(x->R, file);
}
file.close();
}
void Fill(int n, const char *Filename)
{
int num, day, month;
ofstream file(Filename, ios::binary);
for (int i = 0; i < n; i++)
{
cin >> num;
file.write((char*)&num, sizeof(num));
cin >> day;
file.write((char*)&day, sizeof(day));
cin >> month;
file.write((char*)&month, sizeof(month));
}
file.close();
}
int main()
{
int n, day, month;
cin >> n;
const char *InputName("input.bin"), *OutputName("output.bin");
Fill(n, InputName);
tree *beg = new tree;
Cell* x = new Cell;
cin >> day >> month;
ifstream f("input.bin");
if (f.is_open()) {
int flag = 1;
f.read((char*)x, sizeof(*x));
beg->info = *x;
beg->L = 0;
beg->R = 0;
while (flag)
{
f.read((char*)x, sizeof(*x));
if (f.eof())
flag = 0;
if ( ((abs(day - x->day) > 3 && month == x->month) || abs(month - x->month) > 0) && flag)
add(beg, x);
}
f.close();
ofstream file(OutputName, ios::binary);
if (file.is_open()) {
treeprint(beg, file);
}
else {
cout << "Файл output.bin не открыт";
}
}
else {
cout << "Файл input.bin не открыт";
}
system("pause");
return 0;
}