-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cpp
121 lines (93 loc) · 3.23 KB
/
generator.cpp
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
113
114
115
116
117
118
119
120
121
#include "student.h"
#include "helper.h"
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>
#include <random>
#include <algorithm>
#include <iomanip>
#include <chrono>
std::vector<int> generateRandomMarks(int number, std::mt19937 & mt)
{
std::vector<int> marks(number);
std::uniform_int_distribution<int> dist {1, 10};
std::generate(marks.begin(), marks.end(), [&](){ return dist(mt); });
return marks;
}
int sumOfMarks (std::vector<int> marks)
{
int sum_of_marks{};
for (auto &i : marks)
sum_of_marks += i;
return sum_of_marks;
}
float calculateAvg(std::vector<int> marks, int exam)
{
return 0.4 * (sumOfMarks(marks) / marks.size()) + 0.6 * exam;
}
float calculateMedian(std::vector<int> marks, int exam)
{
return 0.4 * median(marks) + 0.6 * exam;
}
std::vector<Student> generateStudents(int amount)
{
auto start = std::chrono::steady_clock::now();
std::vector<Student> students;
std::random_device rd;
std::mt19937 mt(rd());
for(int i = 0; i < amount; i++) {
Student student;
student.name = "Vardas" + std::to_string(i);
student.surname = "Pavarde" + std::to_string(i);
student.marks = generateRandomMarks(13, mt);
student.exam = rand() % 10 + 1;
student.final_mark_avg = calculateAvg(student.marks, student.exam);
student.final_mark_med = calculateMedian(student.marks, student.exam);
if(student.final_mark_avg < 5)
{
student.file = true;
}
else
{
student.file = false;
}
students.push_back(student);
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "Studentu generavimas uztruko: " << elapsed_seconds.count() << "\n";
return students;
}
std::ostream& operator<< (std::ostream& strm, const Student& s)
{
std::stringstream ss;
std::copy(s.marks.begin(), s.marks.end(), std::ostream_iterator<int>(ss, " "));
std::string marks_text = ss.str();
return strm << std::setw(15) << s.surname << std::setw(15) << s.name << " " << std::setw(15) << marks_text << s.final_mark_avg << std::setw(15) << s.final_mark_med << std::setw(15) << s.category << std::endl;
}
void writeStudents(int amount)
{
auto start = std::chrono::steady_clock::now();
std::vector<Student> students = generateStudents(amount);
std::string file_name_1 = "studentai_kietekai" + std::to_string(students.size()) + ".txt";
std::ofstream students_file_1(file_name_1);
std::string file_name_2 = "studentai_vargsiukai" + std::to_string(students.size()) + ".txt";
std::ofstream students_file_2(file_name_2);
std::string file_name = "studentai" + std::to_string(students.size()) + ".txt";
std::ofstream students_file(file_name);
for(const Student& s : students)
{
students_file << s;
/* if(s.file) {
students_file_2 << s;
}
else
{
students_file_1 << s;
} */
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "Studentu irasymas i 2 failus uztruko: " << elapsed_seconds.count() << "\n";
}