-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREC10.cpp
249 lines (195 loc) · 6.21 KB
/
REC10.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
// #include <iostream>
// #include <fstream>
// using namespace std;
// int main(){
// ofstream file_out;
// file_out.open("output.txt");
// string message = "Hello, World!";
// file_out << message << endl;
// file_out.close(); //added a close statement
// return 0;
// }
// #include <iostream>
// #include <fstream>
// using namespace std;
// int main() {
// ofstream file_out;
// file_out.open("output.txt");
// string test_string = "C++ file handling is fun!";
// file_out << "Writing this sentence to the file" << endl; //removed parenthesis and added insertion op
// file_out << test_string << endl; //removed parenthesis and added insertion op
// file_out.close();
// return 0;
// }
// #include <iostream>
// #include <fstream>
// using namespace std;
// int main() {
// ofstream file_out; //need to initialize the file first
// file_out.open("log.txt"); //then open the file after
// string log_message = "This is a log entry.";
// file_out << log_message << endl;
// file_out.close();
// return 0;
// }
#include <iostream>
#include <fstream>
using namespace std;
int split(string input_string, char separator, string arr[], const int ARR_SIZE){
int arrayPosition = 0;
string newString;
bool presentSeparator = false;
if(input_string == ""){
return 0;
}
for(unsigned int i = 0; i < input_string.length() && arrayPosition < ARR_SIZE; i++){
if(input_string[i] != separator){
newString += input_string[i];
}
else if(input_string[i] == separator){
arr[arrayPosition] = newString;
newString = "";
arrayPosition += 1;
presentSeparator = true;
}
}
if(newString.length() > 0 && arrayPosition < ARR_SIZE){
arr[arrayPosition] = newString;
}
if(presentSeparator == false){
arr[0] = newString;
return 1;
}
else if(presentSeparator == true && arrayPosition >= ARR_SIZE){
return -1;
}
else if(presentSeparator == true){
return arrayPosition + 1;
}
return 0;
}
bool validateDouble(string input){
//if the string is empty return false
if (input.length() == 0){
return false;
}
//the string must have at least one numerical digit, but it can also start with a minus sign
//it can have up to one decimal
int numDecimals = 0;
bool seenDigit = false;
//if the string is longer than 1, the first character can be a digit or a minus sign
switch(input[0]){
case '-':
break;
case '.':
numDecimals++;
break;
default:
if (isdigit(input[0])){
seenDigit = true;
}
else{
return false;
}
}
//check if all other digits are valid for index 1 through the end
for (int i = 1; i < (int)input.length(); i++){
if (input[i] == '.'){ //if the character isn't a number return false
numDecimals++;
}
else if (isdigit(input[i])){
seenDigit = true;
}
else{
return false;
}
}
if (numDecimals > 1){
return false;
return seenDigit;
}
return 0;
}
int main(){
//declare variables
ifstream in_file("coordinates2.txt");
in_file.open("coordinates2.txt");
string line;
const int size = 3;
string coords[size];
double doubCoords[size];
double totalX, totalY, totalZ, amountX = 0.0, amountY = 0.0, amountZ = 0.0, avgX = 0.0, avgY = 0.0, avgZ = 0.0;
int invalidEntry = 0, validEntry = 0;
//declare and open files
ofstream out_file1("error_log.txt");
out_file1.open("error_log.txt");
ofstream out_file2("summary.txt");
out_file2.open("summary.txt");
ofstream out_file3("correct_cords.txt");
out_file3.open("correct_cords.txt");
//check the validate double function
while(getline(in_file, line)){
validateDouble(line);
if(validateDouble(line) == 0){
out_file1 << "Invalid entry:" << line << endl;
invalidEntry++;
}
else{
out_file3 << line << endl;
validEntry++;
}
}
//close the file since it will be an input file now
out_file3.close();
ifstream in_file2("correct_cords.txt");
in_file2.open("correctcords.txt");
if(invalidEntry == 0){
out_file2 << "No valid coordinated processed!" << endl;
out_file1.close();
out_file2.close();
out_file3.close();
return 0;
}
//if there is an invalid double then return there is an invalid value
// if(validateDouble == 0){
// cout << "Invalid value detected!" << endl;
// return 0;
// }
//else check each line, split them at the delimeter and add each coordinate depending on position
else{
while(getline(in_file2, line)){
split(line, ',', coords, size);
for(int i = 0; i < size; i++){
if(i == 0){
doubCoords[i] = stod(coords[i]);
totalX += doubCoords[i];
amountX += 1.0;
}
if(i == 1){
doubCoords[i] = stod(coords[i]);
totalY += doubCoords[i];
amountY += 1.0;
}
if(i == 2){
doubCoords[i] = stod(coords[i]);
totalZ += doubCoords[i];
amountZ += 1.0;
}
}
}
}
cout << "Hello" << endl;
avgX = totalX/amountX;
avgY = totalY/amountY;
avgZ = totalZ/amountZ;
cout << "The center of mass is at: " << avgX << ", " << avgY << ", " << avgZ << "!" << endl;
out_file2 << "Center of Mass:" << endl;
out_file2 << "X: " << avgX << endl;
out_file2 << "Y: " << avgY << endl;
out_file2 << "Z: " << avgZ << endl;
out_file2 << endl;
out_file2 << "Number of valid entries: " << validEntry << endl;
out_file1.close();
out_file2.close();
return 0;
}