-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_file.cpp
More file actions
75 lines (68 loc) · 1.98 KB
/
parse_file.cpp
File metadata and controls
75 lines (68 loc) · 1.98 KB
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
#include "student.h"
#include "parse_file.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void setStudentValues(Student&, string, int*, bool);
void resize(Student*&, int&, int);
void fit_array(Student*&, int&);
void parse(Student*& students, int &length, ifstream& input_file){
string surname;
int* grades = new int[5];
bool has_contract;
int i = 0;
while(!input_file.eof()){
if (i + 1 > length){
resize(students, length, length+10);
}
getline(input_file,surname,',');
string grade;
for (int j = 0; j < 5; j++){
getline(input_file, grade, ',');
grades[j] = stoi(grade);
}
string str_has_contract = "";
getline(input_file, str_has_contract, '\n');
if(str_has_contract == "FALSE")
has_contract = false;
else
has_contract = true;
setStudentValues(students[i], surname, grades, has_contract);
i++;
};
fit_array(students, length);
}
void resize(Student*& students, int& length, int new_length){
Student* new_arr = new Student[new_length];
if(new_length > length){
for (int j = 0; j < length; j++){
new_arr[j] = students[j];
}
}
else{
for (int j = 0; j < new_length; j++){
new_arr[j] = students[j];
}
}
delete[] students;
students = new_arr;
length = new_length;
}
void fit_array(Student*& students, int& length){
int counter = 0;
while (students[counter].surname != ""){
counter++;
}
resize(students, length, counter);
}
void setStudentValues(Student& student, string surname, int* grades, bool has_contract){
student.surname = surname;
student.grades = grades;
double avarage = 0;
for (int j = 0; j < 5; j++){
avarage += grades[j];
}
student.avarage = avarage / 5;
student.has_contract = has_contract;
}