Skip to content

Commit

Permalink
completely changed the ch15 code
Browse files Browse the repository at this point in the history
  • Loading branch information
kuashio committed Dec 5, 2022
1 parent 87f5ea7 commit f79cba0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 50 deletions.
40 changes: 24 additions & 16 deletions src/15/Challenge/ch15_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,38 @@
#include <iostream>
#include <fstream>
#include <string>
#include <stack>

// JSON File Validation, main()
// Summary: This application checks the balance of braces, brackets, and quotes in a JSON file.
int main(){
// is_valid_JSON()
// Summary: This function returns true if the file in the argument is a valid JSON file based on its balance of braces, brackets, and quotes.
// Arguments:
// filename: A string with the name of the file to open.
// Returns: An integer enconding: 1 for valid JSON files, 0 for invalid, and -1 on failure to open file.
int is_valid_JSON(std::string filename){
std::string line;
int braces=0, brackets=0, quotes=0; // Counters

std::fstream file ("jason.json", std::ios::in);
if(file.is_open()){
bool quotes = false;

std::fstream file (filename, std::ios::in);
if(file.is_open()){

// Write your code here

file.close();
}
else{
std::cout << "Something went wrong\n\n";
return 1;
}

// Final verdict.
if(brackets==0 && braces==0 && quotes==0)
std::cout << "The file is valid!\n\n";
else
std::cout << "The file is invalid!\n\n";
return -1;
}

// JSON File Validation, main()
int main(){
int x = is_valid_JSON("jason.json");

if(x == 1)
std::cout << "The JSON file is valid!\n\n";
else if(x == 0)
std::cout << "The JSON file is invalid!\n\n";
else
std::cout << "Could not open the file!\n\n";
std::cout << std::flush;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/15/Challenge/jason.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"$gcc"
],
"group": "build",
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\"",
"detail": "compiler: C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"presentation":{
"reveal": "always",
"focus": true
Expand Down
75 changes: 43 additions & 32 deletions src/15/Solution/ch15_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,66 @@
#include <iostream>
#include <fstream>
#include <string>
#include <stack>

// JSON File Validation, main()
// Summary: This application checks the balance of braces, brackets, and quotes in a JSON file.
int main(){
// is_valid_JSON()
// Summary: This function returns true if the file in the argument is a valid JSON file based on its balance of braces, brackets, and quotes.
// Arguments:
// filename: A string with the name of the file to open.
// Returns: An integer enconding: 1 for valid JSON files, 0 for invalid, and -1 on failure to open file.
int is_valid_JSON(std::string filename){
std::string line;
int braces=0, brackets=0, quotes=0; // Counters
std::fstream file ("jason.json", std::ios::in);
bool quotes = false;

std::fstream file (filename, std::ios::in);
if(file.is_open()){
while(std::getline(file, line)){
for (auto &ch : line){
if(quotes==0) // Content outside quotes.
std::stack<char> stack;
while(std::getline(file, line))
for(auto &ch : line)
if(!quotes) // Content outside quotes.
switch(ch){
case '{':
braces++;
break;
case '}':
braces--;
break;
case '[':
brackets++;
stack.push(ch);
break;
case '}':
case ']':
brackets--;
if(stack.empty()){
file.close();
return 0;
}
if(stack.top() == (ch - 2))
stack.pop();
else
return 0;
break;
case '"':
quotes++;
quotes = true;
break;
}
}
else // Ignore everything inside quotes.
if(ch=='"')
quotes--;

if(braces<0 || brackets<0) // Invalidate if brackets are ever negative.
braces = -1000000;
}
}
quotes = false;
file.close();
if(stack.empty() && !quotes)
return 1;
else
return 0;
}
else{
std::cout << "Something went wrong\n\n";
return 1;
}
else
return -1;
}

// JSON File Validation, main()
int main(){
int x = is_valid_JSON("jason.json");

// Final verdict.
if(brackets==0 && braces==0 && quotes==0)
std::cout << "The file is valid!\n\n";
if(x == 1)
std::cout << "The JSON file is valid!\n\n";
else if(x == 0)
std::cout << "The JSON file is invalid!\n\n";
else
std::cout << "The file is invalid!\n\n";
std::cout << "Could not open the file!\n\n";
std::cout << std::flush;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/15/Solution/jason.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"$gcc"
],
"group": "build",
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\"",
"detail": "compiler: C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"presentation":{
"reveal": "always",
"focus": true
Expand Down

0 comments on commit f79cba0

Please sign in to comment.