/
struct.cpp
110 lines (95 loc) · 2.57 KB
/
struct.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
#include <iostream>
using namespace std;
const int MAX_NAME = 45;
struct Student {
char name[MAX_NAME];
int fn;
double grade;
};
void readStudent(Student& student) {
cout << "Име: ";
cin.getline(student.name, MAX_NAME);
cout << "Ф№: ";
cin >> student.fn;
cout << "Оценка: ";
cin >> student.grade;
cin.get();
}
int readStudents(Student s[]) {
cout << "Въведете брой студенти: ";
int n;
cin >> n;
cin.get();
for(int i = 0; i < n; i++)
readStudent(s[i]);
return n;
}
void printStudentLine(Student s) {
cout << s.fn << '\t' << s.grade << '\t' << s.name << endl;
}
void printStudents(Student s[], int n) {
cout << "\n\nФ№\tОценка\tИме\n";
cout << "-----------------------------------------------\n";
for(int i = 0; i < n; i++)
printStudentLine(s[i]);
}
void printStudentsByIndex(Student* studentIndex[], int n) {
cout << "\n\nФ№\tОценка\tИме\n";
cout << "-----------------------------------------------\n";
for(int i = 0; i < n; i++)
printStudentLine(*studentIndex[i]);
}
double averageGrade(Student students[], int n) {
double sumGrade = 0;
for(int i = 0; i < n; i++)
sumGrade += students[i].grade;
return sumGrade / n;
}
void sortStudentsByFn(Student students[], int n) {
for(int i = 0; i < n - 1; i++) {
int mini = i;
for(int j = i + 1; j < n; j++)
if (students[j].fn < students[mini].fn)
mini = j;
/*
Student tmp = students[i];
students[i] = students[mini];
students[mini] = tmp;
*/
swap(students[i], students[mini]);
}
}
void initStudentIndex(Student students[], Student* studentIndex[], int n) {
for(int i = 0; i < n; i++)
studentIndex[i] = &students[i];
}
void sortStudentIndexByFn(Student* studentIndex[], int n) {
for(int i = 0; i < n - 1; i++) {
int mini = i;
for(int j = i + 1; j < n; j++)
if (studentIndex[j]->fn < studentIndex[mini]->fn)
mini = j;
/*
Student* tmp = studentIndex[i];
studentIndex[i] = studentIndex[mini];
studentIndex[mini] = tmp;
*/
swap(studentIndex[i], studentIndex[mini]);
}
}
const int MAX_STUDENTS = 100;
void testStudents() {
Student students[MAX_STUDENTS];
int n = readStudents(students);
// sortStudentsByFn(students, n);
Student* studentIndex[MAX_STUDENTS];
initStudentIndex(students, studentIndex, n);
sortStudentIndexByFn(studentIndex, n);
//printStudents(students, n);
printStudentsByIndex(studentIndex, n);
cout << "Среден успех: " << averageGrade(students, n) << endl;
}
int main() {
testStudents();
return 0;
}