Advertisement
hitlerdidnothingwron

Untitled

Nov 11th, 2016
2,729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. /**
  2. * pics.cpp
  3. *
  4. * EECS 183, Fall 2016
  5. * Project 4: CoolPics
  6. *
  7. * John Dorsey, Patrick Ahimovic
  8. * jsdorsey, paddya
  9. *
  10. * General function that runs the CoolPics user interface
  11. */
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. using namespace std;
  17.  
  18. #include "Line.h"
  19. #include "Triangle.h"
  20. #include "Circle.h"
  21. #include "Rectangle.h"
  22. #include "Graphics.h"
  23.  
  24. /**
  25. * Requires: Nothing.
  26. * Modifies: cout.
  27. * Effects: Prints an opening message.
  28. */
  29. void printOpener();
  30.  
  31. /**
  32. * Requires: Nothing.
  33. * Modifies: cout.
  34. * Effects: Prints a closing message.
  35. */
  36. void printCloser();
  37.  
  38. /**
  39. * Requires: Nothing.
  40. * Modifies: cout.
  41. * Effects: Prints a menu.
  42. */
  43. void printMenu();
  44.  
  45. /**
  46. * Requires: ins is in good state.
  47. * Modifies: cin, ins.
  48. * Effects: Closes ins if it is open. Keeps reading filename from the user
  49. * (and appends .txt) until the file is successfully opened.
  50. * Returns the name of the file that was opened.
  51. */
  52. string openFile(ifstream& ins);
  53.  
  54. /**
  55. * Requires: Nothing.
  56. * Modifies: Nothing.
  57. * Effects: Returns str with all of its alphabetical characters lowercased.
  58. */
  59. string tolower(string str);
  60.  
  61. /**
  62. * Requires: Nothing.
  63. * Modifies: cin, drawer.
  64. * Effects:
  65. * Opens a file
  66. * Start with a blank canvas (drawer)
  67. * Start reading from file. For each line....
  68. * Read the 1st character to determine shape
  69. * Read the shape: L reads a line, C reads a circle, T read a triangle
  70. * R reads a rectangle.
  71. * For any other character, clears drawer and prints
  72. * "Error in input file: " << [character already read]
  73. * << [all chars remaining on the line] << endl;
  74. * Draw shape on canvas
  75. * Close file
  76. * Print "[Loaded filename]"
  77. */
  78. void loadFile(Graphics& drawer);
  79.  
  80. /**
  81. * Requires: Nothing.
  82. * Modifies: cout, drawer.
  83. * Effects:
  84. * Read filename from user
  85. * concatenate filename with .bmp
  86. * Write image to file.
  87. * Print "[Wrote filename]"
  88. */
  89. void writeFile(const Graphics& drawer);
  90.  
  91. int main()
  92. {
  93. Graphics drawer;
  94. string command;
  95. printOpener();
  96. printMenu();
  97.  
  98. // read first command from user
  99. cin >> command;
  100. cout << endl;
  101. command = tolower(command);
  102.  
  103. // read user's input until he or she quits
  104. while (command != "quit")
  105. {
  106. if (command == "load")
  107. {
  108. loadFile(drawer);
  109. }
  110. else if (command == "write")
  111. {
  112. writeFile(drawer);
  113. }
  114. else
  115. {
  116. cout << "Invalid command" << endl << endl;
  117. }
  118.  
  119. printMenu();
  120.  
  121. // read next command
  122. cin >> command;
  123. cout << endl;
  124. command = tolower(command);
  125. }
  126.  
  127. printCloser();
  128. }
  129.  
  130. void writeFile(const Graphics& drawer)
  131. {
  132. // This makes use of Graphics::writeFile()
  133.  
  134. // file name (sans ".bmp")
  135. string fileName;
  136. cin >> fileName;
  137. fileName += ".bmp";
  138. drawer.writeFile(fileName);
  139. cout << "[Wrote " + fileName + "]" << endl;
  140. }
  141.  
  142. void loadFile(Graphics& drawer)
  143. {
  144. ifstream ins;
  145. string fileName;
  146. char shapeType;
  147. string errorLine;
  148.  
  149. fileName = openFile(ins);
  150. while (!ins.eof()) {
  151. ins >> shapeType;
  152. Circle newCircle;
  153. Line newLine;
  154. Rectangle newRect;
  155. Triangle newTri;
  156.  
  157. switch (shapeType) {
  158.  
  159.  
  160. case 'C':
  161. //read and draw a circle
  162. ins >> newCircle;
  163. newCircle.draw(drawer);
  164. break;
  165.  
  166. case 'L':
  167. // read and draw a line
  168. ins >> newLine;
  169. newLine.draw(drawer);
  170. break;
  171.  
  172. case 'R':
  173. // read and draw a rectangle
  174. ins >> newRect;
  175. newRect.draw(drawer);
  176. break;
  177.  
  178. case 'T':
  179. // read and draw a triangle
  180. ins >> newTri;
  181. newTri.draw(drawer);
  182. break;
  183.  
  184. default:
  185. getline(ins, errorLine);
  186. drawer.clear();
  187. cout << "Error in input file: " << shapeType << errorLine << endl;
  188. }
  189.  
  190. // close ins, print "wrote" statement
  191. ins.close();
  192. }
  193. cout << "[Loaded " << fileName << "]" << endl;
  194. }
  195.  
  196. string tolower(string str)
  197. {
  198.  
  199. // cycles through string, turns all applicable letters to lowercase
  200. for (int i = 0; i < str.length(); i++) {
  201. if (str[i] <= 'Z' && str[i] >= 'A') {
  202. str[i] -= ('Z' - 'z');
  203. }
  204. }
  205.  
  206. return str;
  207. }
  208.  
  209.  
  210. // Don't change the implementations below!
  211.  
  212. void printMenu()
  213. {
  214. cout << "Command: Description:" << endl
  215. << "-------- ------------" << endl
  216. << "load filename Loads data from a txt file" << endl
  217. << "write filename Creates a bmp image from data" << endl
  218. << "quit Quits the program" << endl << endl;
  219. }
  220.  
  221.  
  222. void printOpener()
  223. {
  224. cout << "=================================================" << endl
  225. << " Welcome to CoolPics" << endl
  226. << "=================================================" << endl << endl;
  227. }
  228.  
  229. void printCloser()
  230. {
  231. cout << "=================================================" << endl
  232. << " Thanks for using CoolPics!" << endl
  233. << "=================================================" << endl;
  234. }
  235.  
  236. string openFile(ifstream& ins)
  237. {
  238. string fileName;
  239.  
  240. // close stream if open
  241. if (ins.is_open())
  242. {
  243. ins.clear();
  244. ins.close();
  245. }
  246.  
  247. // get filename
  248. cin >> fileName;
  249. fileName = fileName + ".txt";
  250. ins.open(fileName);
  251.  
  252. // keep retrying if failed to open
  253. while (ins.fail())
  254. {
  255. cout << "Error in opening " << fileName
  256. << ". Enter another file name: ";
  257. ins.clear();
  258. cin >> fileName;
  259. fileName = fileName + ".txt";
  260. ins.open(fileName);
  261. }
  262.  
  263. return fileName;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement