Repository navigation
Expand file tree
/
Copy pathdb.cpp
More file actions
175 lines (128 loc) · 4.44 KB
/
Copy pathdb.cpp
File metadata and controls
175 lines (128 loc) · 4.44 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* MEC Assignment
* Database CPP File
*/
#include "db.hpp"
// ************************************************** Structures ************************************************** //
// Converts the category struct to a string that can be saved to file
std::string GradeCategory::toFile(bool end_line) {
std::string line = "";
line += name;
line += "-";
line += std::to_string(weight);
line += ":";
for(int i=0; i<grades.size(); i++) line += (" " + std::to_string(grades[i]));
if(end_line) line += "\n";
return line;
}
// Gets the grade for the category
double GradeCategory::getGrade() {
double grade = 0;
// Add each weight to the grade
for(int i=0; i<grades.size(); i++) grade += grades[i];
// Return the category grade by dividing by the weight
return grade/weight;
}
// Gets the sum of all the grades for the category
double GradeCategory::getGradeSum() {
double sum = 0;
// Add each grade to the sum
for(int i=0; i<grades.size(); i++) sum += grades[i];
// Return the category grade sum
return sum;
}
// ************************************************** Classes ************************************************** //
// Class constructor
DataBase::DataBase() {}
// Class deconstructor
DataBase::~DataBase() {}
// Checks to see if file has the correct extension
bool DataBase::hasSuffix(std::string &str, std::string &ending) {
// If the ending checked is larger return false
// The ending cannot be longer than the actual string being checked
if(str.length() < ending.length()) return false;
// Subtract the length of the ending from the string to get the starting position
// to compare the rest of the string to the ending
// If they are equal then the function equals 0, invert it to make it true
return !str.compare(str.length()-ending.length(), ending.length(), ending);
}
// Sets the file name for the database
void DataBase::setFileName(std::string &f_name) {
// Set the file name
_f_name = f_name;
// Read the file
_read();
return;
}
// Write the file
void DataBase::write() {
// Open the file to create/write
std::ofstream file(_f_name);
// Iterate through the file to get the data
for(int i=0; i<categories.size(); i++) {
if(i==categories.size()-1) file << categories[i].toFile(false);
else file << categories[i].toFile();
}
// Close the file
file.close();
return;
}
// Write the file
void DataBase::_create() {
std::cout << "[INFO] Creating database file called " << (_f_name) << "\n";
// Open the file to create/write
std::ofstream file(_f_name);
// Create the default category along with the weights
GradeCategory labs = {"Labs", 50};
GradeCategory assignments = {"Assignments", 500};
GradeCategory term_project = {"Term Project", 350};
GradeCategory rewiew_project = {"Review Project", 30};
GradeCategory final = {"Final", 100};
// Create the database file
file << labs.toFile() << assignments.toFile() << term_project.toFile() << rewiew_project.toFile() << final.toFile(false);
// Close the file
file.close();
return;
}
// Read the file
void DataBase::_read() {
// Open the file to read
std::ifstream file(_f_name);
if(!file) _create();
// Iterate over every line in the file
for(std::string line; getline(file, line);) {
// Empty category to read from file to
GradeCategory cat;
//Find key spots to break up the line by delimeters
int d1 = line.find("-"), d2 = line.find(":");
// Get the category name and weight
cat.name = line.substr(0, d1);
cat.weight = std::stoi(line.substr(d1+1, d2-d1-1));
// Read char by char till whitespace then conver to double
std::string num = "";
// Get all the grades seperated by whitespace after name and weight
for(unsigned int i=line.find(":")+2; i<line.size(); i++) {
// Get the single char at pos i
char c = line[i];
// Check if the number has ended
if(c==' ') {
// Change num to an int then add it to the img vector
cat.grades.push_back(std::stod(num));
// Reset num for the next number to be read
num = "";
continue;
}
// Add the char to the end of num
num.push_back(c);
// Change num to an int then add it to the img vector before next iteration of the loop
if(i+1==line.size()) {
cat.grades.push_back(std::stod(num));
}
}
// Add the grade category to the category vector
categories.push_back(cat);
}
// Close the file
file.close();
return;
}