-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatting.cpp
More file actions
203 lines (156 loc) · 5.12 KB
/
Copy pathFormatting.cpp
File metadata and controls
203 lines (156 loc) · 5.12 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
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
// Title: Lab 1 - Formatting.cpp
//
// Purpose: **Format from json to csv format. also, finding just the age of individual in the GetAge function.
//
// Class: CSC 2430 Winter 2022
// Author: **Kainoa Aqui
#include <cassert>
#include "Formatting.h"
#include <iostream>
using std :: cout;
using std :: string;
using std:: cout;
using std:: cin;
using std :: endl;
// Returns CSV header as a string
//
// Parameters:
// None
// Returns:
// CSV header
string CSVHeader() {
string header = "firstName, lastName, Age, Height, Nationality"; //json format
return header;
}
// Converts one JSON formatted line to CSV, returning the CSV
// representation as a string
//
// Parameters:
// json - JSON formatted line
// Returns:
// CSV formatted line
string FormatAsCSV(string json) {
string firstName;
string lastName;
string age;
string height;
string nationality;
string comma = ",";
string format;
string fName = "FirstName";
int length = json.length();
size_t foundFirst = json.find(fName);
if (foundFirst!=std::string::npos) {
for (int i = foundFirst + 12; i < length; i++){
if (!isalpha(json.at(i)) && json.at(i) != '"') {
continue;
}
else if (isalpha(json.at(i))) {
firstName += json.at(i);
}
else {
break;
}}}
if (!foundFirst) {
firstName = "";
}
//*above code: this finds the first name and stores it into a string. returns blank if not found.
string lName = "LastName";
size_t foundLast = json.find(lName);
if (foundLast!=std::string::npos) {
for (int i = foundLast + 11; i < length; i++){
if (!isalpha(json.at(i)) && json.at(i) != '"') {
continue;
}
else if (isalpha(json.at(i))) {
lastName += json.at(i);
}
else {
break;
}}}
if (!foundLast) {
lastName = "";
}
// *above code: finding person's last name if they have one and putting it into a string. blank if it doesn't exist.
string ageNum = "Age";
size_t foundAge = json.find(ageNum);
if (foundAge!=std::string::npos) {
for (int i = foundAge; i < length; i++){
if (!isdigit(json.at(i)) && json.at(i) != ',') {
continue;
}
else if (isdigit(json.at(i))) {
age += json.at(i);
}
else {
break;
}}}
if (!foundAge) {
age = "";
}
// *above code: finds persons age if they have on and puts it into a string and blank if not
string heightNum = "Height";
size_t foundHeight = json.find(heightNum);
if (foundHeight!=std::string::npos) {
for (int i = foundHeight + 8; i < length; i++){
if (!isdigit(json.at(i)) && json.at(i) != ',') {
continue;
}
else if (isdigit(json.at(i))) {
height += json.at(i);
}
else {
break;
}}}
if (!foundHeight) {
height = "";
}
// *above code: finds height and puts it into a string. string is blank if height doesn't exist.
string nationalityName = "Nationality";
size_t foundNationality = json.find(nationalityName);
if (foundNationality!=std::string::npos) {
for (int i = foundNationality + 14; i < length; i++){
if (!isalpha(json.at(i)) && json.at(i) != '"') {
continue;
}
else if (isalpha(json.at(i))) {
nationality += json.at(i);
}
else {
break;
}}}
if (!foundNationality) {
nationality = "";
}
// *above code: finds nationality and puts it into a string. string is blank if nationality doesn't exist.
format = firstName + comma + lastName + comma + age + comma + height + comma + nationality;
return format;
}
// Return the age value stored in a JSON
// formatted line. The return value is a string
// If age doesn't appear, returns empty string
//
// Parameters:
// json - JSON formatted line
// Returns:
// age as string, or empty if age doesn't appear
string GetAge(string json) {
string age = "Age";
string personAge;
int length = json.length();
size_t found = json.find(age);
if (found!=std::string::npos) {
for (int i = found + 5; i < length; i++){
if (!isdigit(json.at(i)) && json.at(i) != ',') {
continue;
}
else if (isdigit(json.at(i))) {
personAge += json.at(i);
}
else {
break;
}}}
return personAge;
}
// above code starts at the index where Age is found and adds 5 to it where the age begins. the code loops, storing
// --> the integer values and breaks once it reaches a comma