Order Now  +1 678 648 4277 

Updating Batman’s List of Foes in C++ Homework Help

After the program you wrote for Sheldon to keep track of his enemies, Batman wants the same thing, but with the ability to have more than 1 line of text for the details. If a line starts with space or tab, it is considered to be part of the previous line. Here is what our C++ homework helpers came up with.

Updating Lists in C++

#include #include #include #include #include #include #include #include #include #include #include using namespace std; bool valid_key(string key) { return isalpha(key[0]); } string trimFromBackslashR(string &str) { string s= str; if (s.empty()) return ""; int i = 0; while (s[i] == '\r') i++; int j = s.size() - 1; while (s[j] == '\r') j--; s = s.substr(i, j - i + 1); return s; } void printMap(map &m, int max, vector &keys_order,bool &first) { max++; if(m.size()!=0 && !first){ cout << endl; } first = false; for (int i = 0; i < static_cast(keys_order.size()); i++) { cout << keys_order[i]; for (int j = 0; j < max - static_cast(keys_order[i].size()); j++) cout << " "; cout << m[keys_order[i]] << endl; } } int addKeyValue(map &m, string& k, string& v, int &key_max_size, vector &keys_order,set keys) { if (!valid_key(k)) { // not alphabetic key return -1; } if (m.find(k) != m.end()) { // duplicate keys return -2; } if (keys.find(k) == keys.end()) { // not found keys return -3; } key_max_size = max(key_max_size, static_cast(k.size())); m[k] = trimFromBackslashR(v); keys_order.push_back(k); return 0; } void printError(int res, bool fromCin, char*progName, char*filename,vector&keyValueVector,int lineNumber) { switch (res) { case -1: cerr << progName << " has encountered an error " + ((fromCin) ? "" : "in file " + string(filename) + " ") + ": key is not alphabetic {" << keyValueVector[0] << "}" << endl; break; case -2: cerr << progName << " has encountered an error " + ((fromCin) ? "" : "in file " + string(filename) + " ") + ": duplicate key found in line " << lineNumber << endl; break; case -3: cerr << progName << " has encountered an error " + ((fromCin) ? "" : "in file " + string(filename) + " ") + ": key is not found in the key list in line " << lineNumber << endl; break; } } void trim(string &s) { if (s.empty()) return; int i = 0; while (s[i] == ' ' || s[i] == '\r' || s[i] == '\t') i++; int j = s.size() - 1; while (s[j] == ' ' || s[j] == '\r' || s[j] == '\t') j--; s = s.substr(i, j - i + 1); } void split(string& s, vector&v) { if (s.empty()) return; string trimmedString = s; trim(trimmedString); if(trimmedString.empty()){ return; } int index = s.find(" "); int index2 = index; if (index == -1) { v.push_back(s); return; } else { while (s[index2 + 1] == ' ') index2++; if(!s.substr(0, index).empty()) v.push_back(s.substr(0, index)); if (!s.substr(index2+1).empty()) v.push_back(s.substr(index2 + 1)); } } int initKeys(ifstream &in,set&keys) { string x; while (in >> x) { trim(x); if (!valid_key(x)) { return -1; } keys.insert(x); } return 0; } void trimLeft(string &s) { if (s.empty()) return; int i = 0; while (s[i] == ' ' || s[i] == '\r' || s[i] == '\t') i++; s = s.substr(i); } inline bool exists_test0(const std::string& name) { ifstream f(name.c_str()); return f.good(); } int main(int argc,char*argv[]) { bool first =true; if(argc < 2){ cerr << argv[0] << " has encountered an error: usage ./hw1 idsFile [input files]"<fstreams; string line; setkeys; vectorkeyValueVector; mapkeyValue; int maxKeyLength = -1; vectorkeyOrder; int res; int lineNumber = 0; bool read = false; bool fromCin = true; /*initialize the streams*/ if (argc != 2) { fromCin = false; for (int i = 2; i < argc; i++) { ifstream *tempf = new ifstream(); (*tempf).open(argv[i]); if (!(*tempf)) { cerr << argv[0] << " has encountered an error "+((fromCin)?"":"in file "+string(argv[i])+" ")+": file \""<(fstreams.size())); i++) { while(read || getline((fromCin) ? cin:*fstreams[i], line)) { if (read) read = false; lineNumber++; if (line[0] == ' ' || line[0] == '\t') { // continues line trimLeft(line); if (line.empty()) { continue; } if (keyValue.find(keyValueVector[0]) != keyValue.end()){ keyValue[keyValueVector[0]].append(" " + trimFromBackslashR(line)); } else { res = addKeyValue(keyValue, keyValueVector[0], line, maxKeyLength, keyOrder, keys); printError(res,fromCin,argv[0],argv[2+i],keyValueVector,lineNumber); if (res != 0) { return -1; } } } else { keyValueVector.clear(); split(line, keyValueVector); if (keyValueVector.size() == 0) { // new enemy printMap(keyValue,maxKeyLength,keyOrder,first); keyValue.clear(); maxKeyLength = -1; keyOrder.clear(); continue; } else if (keyValueVector.size() != 2) { // invalid input // may be a continious line read = true; if (!getline((fromCin) ? cin : *fstreams[i], line) || (line[0] != ' ' && line[0] != '\t')) { cerr << argv[0] << " has encountered an error " + ((fromCin) ? "" : "in file " + string(argv[2 + i]) + " ") + ": key or value is missing in line " << lineNumber << endl; return -1; } else{ continue; } } if ((res = addKeyValue(keyValue, keyValueVector[0], keyValueVector[1], maxKeyLength, keyOrder,keys)) != 0) { printError(res,fromCin,argv[0],argv[2+i],keyValueVector,lineNumber); return -1; } } } // end of file and an enemy printMap(keyValue, maxKeyLength, keyOrder,first); keyValue.clear(); maxKeyLength = -1; keyOrder.clear(); lineNumber = 0; if (fromCin) break; } return 0; }