-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]sam shurie assignment4 #6
base: master
Are you sure you want to change the base?
Changes from all commits
67d5bcc
cf8c71f
196e332
cb2d404
5a483ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.3) | ||
project(Assignment4_Analysis) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp analysis_tool.h) | ||
add_executable(Assignment4_Analysis ${SOURCE_FILES}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
using std::cout; | ||
using std::cin; | ||
using std::string; | ||
using std::endl; | ||
using std::iostream; | ||
using std::ifstream; | ||
using std::ofstream; | ||
|
||
string validateArg1(string arg); | ||
string validateArg2(string arg); | ||
string validateArg3(string arg2, string arg3); | ||
void tokenize(string line); | ||
int getFileNumLines(string path); | ||
|
||
int ctoi(char character); | ||
void search(string file_path, string arg3); | ||
void sort(ifstream file, string arg3); | ||
void count(string arg1); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Most Common Character(s): o | ||
Least Common Characters(s): g x 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
#include <string.h> | ||
#include "analysis_tool.h" | ||
|
||
int main(int argc, char* argv[]) { | ||
|
||
string file_path = ""; // Arg 1 | ||
string method = ""; // method provided (search, sort, count) | ||
string query = ""; // additional arg provided depending on what arg2 method is called (asc, desc, string) | ||
if(argc > 1) { | ||
file_path = static_cast<string>(argv[1]); | ||
if (argc > 2) { | ||
method = static_cast<string>(argv[2]); | ||
if (argc > 3) { | ||
query = static_cast<string>(argv[3]); | ||
} | ||
} | ||
} | ||
|
||
//check arguments for errors | ||
file_path = validateArg1(file_path); | ||
method = validateArg2(method); | ||
query = validateArg3(method, query); | ||
|
||
//get the number of lines in the file | ||
const int num_lines = getFileNumLines(file_path); | ||
|
||
// open file and set cursor to beginning of file | ||
ifstream file(file_path); | ||
file.seekg(0L, file.beg); | ||
|
||
// run tool depending on user selection | ||
if(method == "search"){ | ||
search(file_path, query); | ||
//}else if(method == "sort"){ | ||
// sort(file, query); | ||
}else if(method == "count"){ | ||
count(file_path); | ||
} | ||
return 0; | ||
} | ||
|
||
/* | ||
* Argument 1 one should hold a valid filepath upon return | ||
*/ | ||
string validateArg1(string arg){ | ||
ifstream file; | ||
file.open(arg); | ||
while(!file){ | ||
file.close(); | ||
cout << "File provided could not be located. Enter a file path: "; | ||
cin >> arg; | ||
file.open(arg); | ||
} | ||
file.close(); | ||
return arg; | ||
} | ||
|
||
/* | ||
* Argument 2 should provide a valid tool choice upon return | ||
*/ | ||
string validateArg2(string arg){ | ||
while(arg != "search" && arg != "sort" && arg != "count"){ | ||
cout << "You did not provide a valid method. Enter \"search\", \"sort\", or \"count\": "; | ||
cin >> arg; | ||
} | ||
return arg; | ||
} | ||
|
||
/* | ||
* Argument 3 should provide a valid searchable item or order type upon return if the user chose either sort or | ||
* search for Argument 2 | ||
*/ | ||
string validateArg3(string method, string query){ | ||
if(method == "search" || method == "sort") { | ||
if (method == "search") { | ||
while (query == "") { | ||
cout << "Query needed for Search Tool. Enter a string value to search for: "; | ||
cin >> query; | ||
} | ||
} else { //This else handles query if "sort" is provided | ||
while (query != "asc" && query != "desc") { | ||
cout << "Order type needed for Sort Tool. Enter either \"asc\" for ascending or \"desc\" for descending: "; | ||
cin >> query; | ||
} | ||
} | ||
} | ||
return query; | ||
} | ||
|
||
void search(string file_path, string arg){ | ||
ifstream file(file_path); | ||
ofstream output("search_output.txt"); | ||
output << "Search For: \"" << arg << "\"" << endl; | ||
output << "Found at: " << endl; | ||
output << "Line\tPos" << endl; | ||
|
||
string line; | ||
int found = -1; | ||
int line_count = 0; | ||
while(getline(file, line)){ | ||
line_count++; | ||
found = line.find(arg, 0); | ||
if(found > -1){ | ||
output << line_count++ << "\t" << found << endl; | ||
} | ||
} | ||
file.close(); | ||
output.close(); | ||
} | ||
|
||
|
||
//void sort(ifstream file, string arg){} | ||
|
||
|
||
void count(string file_path) { | ||
const int SIZE = 36; | ||
const char characters[SIZE] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', | ||
'r', 's', | ||
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | ||
int occurances[SIZE]; // parallel to digits | ||
// Initialize all elements of occurances to the value of zero | ||
for (int i = 0; i < SIZE; i++) { | ||
occurances[i] = 0; | ||
} | ||
|
||
ifstream file(file_path); | ||
// while file still has lines, get next line | ||
string line; | ||
while (getline(file, line)) { | ||
// for every character in the current line, determine whether the character is equal to | ||
// any character in the characters array. If found in the array, add one to the parallel index | ||
// in occurances array. | ||
for (int current = 0; current < line.size(); current++) { | ||
int index = 0; | ||
bool found = false; | ||
char c; | ||
while (!found && index < SIZE) { | ||
// store current character and convert to lowercase | ||
c = line.at(current); | ||
tolower(c); | ||
if (c == characters[index]) { | ||
found = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do like that you stop once you find the value. Mine kept looping until it got to the end of the array. |
||
occurances[index]++; //Add one to the number of occurances for found digit | ||
} | ||
index++; | ||
} | ||
} | ||
} | ||
file.close(); | ||
|
||
// find the largest occurance | ||
int largest = 0; | ||
for (int i = 0; i < SIZE; i++) { | ||
if(occurances[i] > largest){ | ||
largest = occurances[i]; | ||
} | ||
} | ||
|
||
//find the smallest occurance, going backwards from the largest value | ||
int smallest = largest; | ||
for(int i = 0; i < SIZE; i++){ | ||
if((occurances[i] != 0) && (occurances[i] < smallest)){ | ||
smallest = occurances[i]; | ||
} | ||
} | ||
|
||
//Open output file and output largest and smallest occurances | ||
ofstream output("count_output.txt"); | ||
output << "Most Common Character(s): "; | ||
|
||
for(int i = 0; i < SIZE; i++){ | ||
if(occurances[i] == largest){ | ||
output << characters[i] << " "; | ||
} | ||
} | ||
output << endl << "Least Common Characters(s): "; | ||
for(int i = 0; i < SIZE; i++){ | ||
if(occurances[i] == smallest){ | ||
output << characters[i] << " "; | ||
} | ||
} | ||
output.close(); | ||
} | ||
|
||
void tokenize(string line){ | ||
|
||
const int LENGTH = line.size(); | ||
|
||
char *tokenized = new char[LENGTH]; | ||
char *tokens = strtok((char *)line.c_str(), " "); | ||
|
||
int count = 0; | ||
while(tokens){ | ||
tokenized[count++] = *tokens; | ||
tokens = strtok(NULL," "); | ||
} | ||
|
||
//Testing | ||
for(int i = 0; i < LENGTH; i++){ | ||
cout << tokenized[i]; | ||
} | ||
} | ||
|
||
int ctoi(char character){ | ||
return character - '0'; | ||
} | ||
|
||
int getFileNumLines(string path){ | ||
ifstream file(path); | ||
string line; | ||
int line_count = 0; | ||
while(getline(file, line)){ | ||
++line_count; | ||
} | ||
file.close(); | ||
return line_count; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Search For: "evil" | ||
Found at: | ||
Line Pos | ||
5 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how you validate each argument. Making sure that the file exists is a good idea.