Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/Assignment4.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

734 changes: 734 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions CMakeLists.txt
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})
22 changes: 22 additions & 0 deletions analysis_tool.h
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);
2 changes: 2 additions & 0 deletions count_output.txt
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
217 changes: 217 additions & 0 deletions main.cpp
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){

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.

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;

Choose a reason for hiding this comment

The 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;
}
4 changes: 4 additions & 0 deletions search_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Search For: "evil"
Found at:
Line Pos
5 3