-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem5.cpp
263 lines (209 loc) · 6.14 KB
/
problem5.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include<iostream>
#include <string>
#include <strings.h>
#include <fstream>
using namespace::std;
class Movie{
private:
string title;
string releaseYear;
public:
Movie();
Movie(string _title, string _releaseYear);
string getTitle();
void setTitle(string _title);
string getReleaseYear();
void setReleaseYear(string _setReleaseYear);
};
// #include "Movie.h"
Movie::Movie(){
title = "";
releaseYear = "";
}
Movie::Movie(string _title, string _releaseYear){
title = _title;
releaseYear = _releaseYear;
}
string Movie::getTitle(){
return title;
}
void Movie::setTitle(string _title){
title = _title;
}
string Movie::getReleaseYear(){
return releaseYear;
}
void Movie::setReleaseYear(string _releaseYear){
releaseYear = _releaseYear;
}
class User{
private:
const static int size = 50;
string username;
int ratings[size];
int numRatings;
// static int size;
public:
User();
User(string _username, int _ratings[], int _numRatings);
string getUsername();
void setUsername(string _username);
int getRatingAt(int i);
int setRatingAt(int index, int value);
int getNumRatings();
void setNumRatings(int _numRatings);
int getSize();
};
User::User(){
username = "";
numRatings = 0;
for (int i = 0; i < 50; i++){
ratings[i] = 0;
}
}
User::User(string _username, int _ratings[], int _numRatings){
username = _username;
numRatings = _numRatings;
for (int i = 0; i < 50; i++){
ratings[i] = 0;
}
for (int i = 0; i < _numRatings; i++){
ratings[i] = _ratings[i];
}
}
string User::getUsername(){
return username;
}
void User::setUsername(string _username){
username = _username;
}
int User::getNumRatings(){
return numRatings;
}
void User::setNumRatings(int _numRatings){
numRatings = _numRatings;
}
int User::getRatingAt(int i){
//should be if not loop
if (i >= size){
return -1;
}
else if (i < size){
return ratings[i];
}
}
int User::setRatingAt(int i, int value){
if (i <= size && value >= 0 && value <= 5){
ratings[i] = value;
}
else{
return false;
}
if (ratings[i] == value){
return true;
}
else{
return false;
}
}
string toLower(string input){
string newInput = "";
for (int i = 0; i <= input.length(); i++){
if (input[i] <= 90 && input[i] >= 65){
newInput[i] = input[i] + 32;
newInput += newInput[i];
}
else{
newInput += input[i];
}
// newInput += newInput[i];
}
return newInput;
}
int split(string inputStr, char separator, string returnArray[], int arraySize) {
int elementIndex = 0;
string currentString = "";
// loop over each character of the string
for(int i = 0; i <= inputStr.size(); i++) {
// start by just placing the currentCharacter into
// a variable with an appropriate name. It just makes
// it easier. if we do this, then we don't have to
// constantly use inputStr[i] each time we want to use
// the character, easier to read
char currentCharacter = inputStr[i];
// if we have reached the separator, then we need
// to add the currentString (that we have been building)
// to the array, and increment elementIndex
// ELSE continue building the string
if(currentCharacter == separator) {
// add the currentString to the index, and then
// we are going to start over, so set it to empty
// again, also, increase the elementIndex
// because the NEXT string will be added to the next
// index in the array.
returnArray[elementIndex] = currentString;
currentString = "";
elementIndex ++;
// handle the case where their are MORE elements in the string
// then we have room for in the array
if (elementIndex >= arraySize) {
return -1;
}
}
else {
currentString += currentCharacter;
}
}
// if we have made it this far, the last currentString
// needs to be added at the elementIndex.
returnArray[elementIndex] = currentString;
// can you guess why we add 1 to the elementIndex?
// the answer is that we are using the index (which is 0 based) to indicate
// the number of elements that we have added.
// so, elementIndex might be 0,1,2,3... but the NUMBER of those items
// is always 1 plus that.
return elementIndex + 1;
}
int readRatings(string fileName, User users[], int numUsersStored, int usersArrSize, int maxRatings) {
string line = "";
int numUsersAdded = 0;
ifstream fin;
fin.open(fileName);
if (numUsersStored == usersArrSize){
return -2;
}
if (fin.fail()){
return -1;
}
while(getline(fin,line)) {
// what to do if the line is blank?
if(line == "") {
continue;
}
string userStringArray[maxRatings + 1];
// 1. split line into array
int numObjects = split(line,',', userStringArray, maxRatings);
// 2. create new tempUser object, set username to the new user
User tempUser;
tempUser.setUsername(userStringArray[0]);
// 3. add ratings to tempUser object
for (int i=1; i <numObjects; i++ ){
tempUser.setRatingAt(i-1, stoi(userStringArray[i]));
}
// 4. add tempUser to user Array
numUsersStored = numUsersStored + 1;
users[numUsersStored] = tempUser;
cout << line << endl;
}
return numUsersStored;
}
int main() {
User users[10];
int numUsers = 0;
int usersArrSize = 10;
int numUsersStored = 5;
numUsersStored = readRatings("ratings.txt", users, numUsersStored , usersArrSize, 5);
for(int i = 0; i < numUsersStored; i++) {
cout << users[i].getUsername() << endl;
}
}