coolpics eecs /** * pics.cpp * * EECS 183, Fall 2016 * Project 4: CoolPics * * John Dorsey, Patrick Ahimovic * jsdorsey, paddya * * General function that runs the CoolPics user interface */ coolpics eecs How to get it? coolpics eecs #include #include #include using namespace std; #include "Line.h" #include "Triangle.h" #include "Circle.h" #include "Rectangle.h" coolpics eecs How to dowload it? coolpics eecs #include "Graphics.h" /** * Requires: Nothing. * Modifies: cout. * Effects: Prints an opening message. */ void printOpener(); /** coolpics eecs How to get it? coolpics eecs * Requires: Nothing. * Modifies: cout. * Effects: Prints a closing message. */ void printCloser(); /** * Requires: Nothing. * Modifies: cout. * Effects: Prints a menu. coolpics eecs PasteShr coolpics eecs */ void printMenu(); /** * Requires: ins is in good state. * Modifies: cin, ins. * Effects: Closes ins if it is open. Keeps reading filename from the user * (and appends .txt) until the file is successfully opened. * Returns the name of the file that was opened. */ coolpics eecs How to get it for free? coolpics eecs string openFile(ifstream& ins); /** * Requires: Nothing. * Modifies: Nothing. * Effects: Returns str with all of its alphabetical characters lowercased. */ string tolower(string str); /** coolpics eecs How to dowload it? coolpics eecs * Requires: Nothing. * Modifies: cin, drawer. * Effects: * Opens a file * Start with a blank canvas (drawer) * Start reading from file. For each line.... * Read the 1st character to determine shape * Read the shape: L reads a line, C reads a circle, T read a triangle * R reads a rectangle. * For any other character, clears drawer and prints coolpics eecs How to get it for free? coolpics eecs * "Error in input file: " << [character already read] * << [all chars remaining on the line] << endl; * Draw shape on canvas * Close file * Print "[Loaded filename]" */ void loadFile(Graphics& drawer); /** * Requires: Nothing. coolpics eecs How to get it? coolpics eecs * Modifies: cout, drawer. * Effects: * Read filename from user * concatenate filename with .bmp * Write image to file. * Print "[Wrote filename]" */ void writeFile(const Graphics& drawer); int main() coolpics eecs How to get it? coolpics eecs { Graphics drawer; string command; printOpener(); printMenu(); // read first command from user cin >> command; cout << endl; command = tolower(command); coolpics eecs PasteShr coolpics eecs // read user's input until he or she quits while (command != "quit") { if (command == "load") { loadFile(drawer); } else if (command == "write") { coolpics eecs How to get it? coolpics eecs writeFile(drawer); } else { cout << "Invalid command" << endl << endl; } printMenu(); // read next command coolpics eecs How to get it for free? coolpics eecs cin >> command; cout << endl; command = tolower(command); } printCloser(); } void writeFile(const Graphics& drawer) { coolpics eecs How to dowload it? coolpics eecs // This makes use of Graphics::writeFile() // file name (sans ".bmp") string fileName; cin >> fileName; fileName += ".bmp"; drawer.writeFile(fileName); cout << "[Wrote " + fileName + "]" << endl; } coolpics eecs How to use it? coolpics eecs void loadFile(Graphics& drawer) { ifstream ins; string fileName; char shapeType; string errorLine; fileName = openFile(ins); while (!ins.eof()) { ins >> shapeType; coolpics eecs PasteShr coolpics eecs Circle newCircle; Line newLine; Rectangle newRect; Triangle newTri; switch (shapeType) { case 'C': //read and draw a circle coolpics eecs How to get it? coolpics eecs ins >> newCircle; newCircle.draw(drawer); break; case 'L': // read and draw a line ins >> newLine; newLine.draw(drawer); break; coolpics eecs How to use it? coolpics eecs case 'R': // read and draw a rectangle ins >> newRect; newRect.draw(drawer); break; case 'T': // read and draw a triangle ins >> newTri; newTri.draw(drawer); coolpics eecs How to get it? coolpics eecs break; default: getline(ins, errorLine); drawer.clear(); cout << "Error in input file: " << shapeType << errorLine << endl; } // close ins, print "wrote" statement ins.close(); coolpics eecs How to dowload it? coolpics eecs } cout << "[Loaded " << fileName << "]" << endl; } string tolower(string str) { // cycles through string, turns all applicable letters to lowercase for (int i = 0; i < str.length(); i++) { if (str[i] <= 'Z' && str[i] >= 'A') { coolpics eecs How to get it? coolpics eecs str[i] -= ('Z' - 'z'); } } return str; } // Don't change the implementations below! coolpics eecs How to dowload it? coolpics eecs void printMenu() { cout << "Command: Description:" << endl << "-------- ------------" << endl << "load filename Loads data from a txt file" << endl << "write filename Creates a bmp image from data" << endl << "quit Quits the program" << endl << endl; } coolpics eecs How to get it? coolpics eecs void printOpener() { cout << "=================================================" << endl << " Welcome to CoolPics" << endl << "=================================================" << endl << endl; } void printCloser() { cout << "=================================================" << endl coolpics eecs PasteShr coolpics eecs << " Thanks for using CoolPics!" << endl << "=================================================" << endl; } string openFile(ifstream& ins) { string fileName; // close stream if open if (ins.is_open()) coolpics eecs How to dowload it? coolpics eecs { ins.clear(); ins.close(); } // get filename cin >> fileName; fileName = fileName + ".txt"; ins.open(fileName); coolpics eecs How to dowload it? coolpics eecs // keep retrying if failed to open while (ins.fail()) { cout << "Error in opening " << fileName << ". Enter another file name: "; ins.clear(); cin >> fileName; fileName = fileName + ".txt"; ins.open(fileName); } coolpics eecs How to get it? coolpics eecs return fileName; } coolpics eecs