/
main.cpp
135 lines (121 loc) · 3.25 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstring>
using namespace std;
struct Point {
double x, y;
};
const size_t MAX = 60;
struct Student {
char name[MAX];
int year;
double grade;
};
void printStudent(Student const& s) {
cout << s.name << ' ' << s.year << ' ' << s.grade << endl;
}
void readStudent(Student& s) {
cout << "Име: ";
cin.getline(s.name, MAX);
do {
cout << "Курс: ";
cin >> s.year;
} while(s.year < 1 || s.year > 4);
cout << "Оценка: ";
cin >> s.grade;
}
/*
Student readStudent() {
// тук ще има копиране на записа
}
*/
int readStudents(Student students[]) {
int nStudents;
cout << "Моля, въведете брой студенти: ";
cin >> nStudents;
for(int i = 0; i < nStudents; i++) {
// изчистваме останалия нов ред, за да можем да четем правилно имена
char temp[MAX];
cin.getline(temp, MAX);
// cin.get()
cout << "Моля, въведете студент " << i + 1 << ':' << endl;
readStudent(students[i]);
}
return nStudents;
}
void testSizes() {
int a[10];
cout << sizeof(int) << ' ' << sizeof(a) << endl;
cout << sizeof(double) << ' ' << sizeof(Point) << endl;
cout << sizeof(Student) << endl;
}
void printHeader() {
cout << endl;
cout << "| ";
cout.setf(ios::left);
cout.width(MAX);
cout << "Name";
cout << " | Year | Grade |";
cout << endl;
cout << '+';
cout.unsetf(ios::left);
cout.setf(ios::right);
cout.fill('-');
cout.width(78);
cout << '+' << endl;
// възстановяване на настройките по подразбиране
cout.unsetf(ios::right);
cout.fill(' ');
}
void printStudentLine(Student const& student) {
cout.fill(' ');
cout << "| ";
cout.setf(ios::left);
cout.width(MAX);
cout << student.name;
cout << " | ";
cout.width(4);
cout.unsetf(ios::left);
cout.setf(ios::right);
cout << student.year;
cout << " | ";
cout.width(5);
cout.setf(ios::fixed);
cout.precision(2);
cout << student.grade;
cout << " |" << endl;
// възстановяване на настройките по подразбиране
cout.unsetf(ios::right);
cout.unsetf(ios::fixed);
cout.precision(6);
// !!! student.grade += 1;
}
void sortStudents(Student students[], size_t nStudents) {
for(int i = 0; i < nStudents - 1; i++) {
int mini = i;
for(int j = i + 1; j < nStudents; j++)
if (strcmp(students[j].name, students[mini].name) < 0)
mini = j;
Student tmp = students[mini];
students[mini] = students[i];
students[i] = tmp;
}
}
void printStudents(Student students[], size_t nStudents) {
printHeader();
for(int i = 0; i < nStudents; i++)
printStudentLine(students[i]);
}
void testStudents() {
const size_t MAXS = 30;
Student students[MAXS];
int nStudents = readStudents(students);
printStudents(students, nStudents);
cout << endl << endl;
sortStudents(students, nStudents);
printStudents(students, nStudents);
}
int main() {
// testSizes();
testStudents();
return 0;
}