Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Have this code for project, Part 2 and Part 3 need to be added on: #include #include #include #include #include #include #include #include #include using
Have this code for project, Part 2 and Part 3 need to be added on:
#includeAll of Part 1 cases plus additional cases prog1 -q file file prog1 -q file prog1 file prog1 -s file prog1 -s file_with_extra_spaces prog1 -c file prog1 -c file_with_many_types prog1 -s -c file_with_extra spaces Copy of file with only realwords and with extra spaces TOO MANY FILES No output Exact copy of file Exact copy of file Copy of file, with extra spaces removed Exact copy of file Copy of file with only realwords removed All of Part 1 and Part 2 cases plus: prog1 -q -p file prog1 -q -p -I file prog1 -q < file prog1 -q -p -l < file prog1 -s -p file_with_extra_spaces Prints statistics only Prints statistics and word lengths only No output Prints statistics and word lengths only Copy of file, with extra spaces removed, plus statisticS prog1 -l-s -p file_with_extra spaces prog1 -c -l -s file_with_many_types prog1 -s -1 -c -p#include #include #include #include #include #include #include #include using namespace std; bool isWord(string word) { for(size_t i = 0; i < word.length(); i++) if(isspace(word[i])) return false; return true; } bool isGoodWord(string word) { for(size_t i = 0; i < word.length(); i++) if(!isalnum(word[i])) return false; return true; } bool isRealWord(string word) { for(size_t i = 0; i < word.length(); i++) if(!isalpha(word[i])) return false; return true; } bool isCapWord(string word) { if(isupper(word[0])) return true; return false; } bool isAcronym(string word) { for(size_t i = 0; i < word.length(); i++) if(!isupper(word[i])) return false; return true; } string printFile(string filename) { ifstream ifile(filename); string line; string out = ""; while(getline(ifile, line)) out += line + ' '; ifile.close(); return out; } bool adjspc(char l, char r){return isspace(l)&&isspace(r);} string removeSpaces(string s) { s.erase(std::unique(s.begin(), s.end(), adjspc), s.end()); return s; } string squishFile(string filename) { ifstream ifile(filename); string line; string out = ""; while(getline(ifile, line)){ line = removeSpaces(line); line.erase(line.find_last_not_of(" \t \f\v") + 1); out += line; if(ifile.peek() != ' ') out += ' '; } ifile.close(); return out; } string myReplaceAll(string str, const string& from, const string& to) { size_t start_pos = 0; while((start_pos = str.find(from, start_pos)) != string::npos) { int end_pos = start_pos + from.length(); while(isspace(str[end_pos])) end_pos++; str.replace(start_pos, end_pos - start_pos, to); start_pos += to.length(); // Handles case where 'to' is a substring of 'from' } return str; } string censorFile(string filename, bool withSquish){ ifstream ifile(filename); string contents( (istreambuf_iterator (ifile)), (istreambuf_iterator ())); ifile.close(); if (withSquish) contents = squishFile(filename); vector words; string word = ""; string out = ""; for(string::iterator it = contents.begin(); it != contents.end(); ++it){ if (!isspace(*it)){ word += *it; } else { if (word.length()!=0) words.push_back(word); word = ""; } } for(vector ::iterator it = words.begin(); it != words.end(); ++it){ if (!isRealWord(*it)){ contents = myReplaceAll(contents, *it, ""); } } return contents; // cout << "[ "; // for (vector ::iterator it = words.begin(); it != words.end(); ++it) cout << "("<<*it<<")" << " "; // cout << "]"; } int main(int argc, char *argv[]) { bool isQuietMode = false, isSquishMode = false, isCensorMode = false, isPrintMode = false, isLengthMode = false; ifstream ifile; char filename[30]; /*bool isExtraSpaceFileBoolean;*/ for(int i=1; i< argc; i++) { if(argv[i][0] == '-') { if(strlen(argv[i]) == 2) { if(argv[i][1] == 'q') { isQuietMode = true; } else if(argv[i][1] == 's') { isSquishMode = true; } else if(argv[i][1] == 'c') { isCensorMode = true; } else if(argv[i][1] == 'p') { isPrintMode = true; } else if(argv[i][1] == 'l') { isLengthMode = true; } else { cout << argv[i] << " INVALID FLAG" << endl; exit(1); } } else { cout << argv[i] << " INVALID FLAG" << endl; exit(1); } } else { ifile.open(argv[i]); if(ifile.fail()) { cout << argv[i] << " CANNOT OPEN" << endl; exit(1); } else { strcpy(filename, argv[i]); } if(i != (argc-1)) { cout << "TOO MANY FILES" << endl; exit(1); } } } //isExtraSpaceFileBoolean = isExtraSpaceFile(filename); if(( isSquishMode || isCensorMode ) && isQuietMode){ cout << "CONFLICTING FLAGS" << endl; exit(1); } else if(isQuietMode && !isPrintMode && !isLengthMode) return 0; else if(!(isQuietMode || isSquishMode || isCensorMode || isPrintMode || isLengthMode)) { cout << printFile(filename); exit(0); } else if(/*isExtraSpaceFileBoolean &&*/ isSquishMode && isCensorMode) { cout << censorFile(filename, true); exit(0); } else if(/*isExtraSpaceFileBoolean &&*/ isSquishMode) { cout << squishFile(filename); exit(0); } else if(isCensorMode) { cout << censorFile(filename, false); exit(0); } return 0; }Need in C++
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started