AaronThomsen

main.cpp

Feb 10th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. #include "Thing.h"
  6. #include "Sphere.h"
  7. #include "Cube.h"
  8. #include "Cylinder.h"
  9. #include "ColoredCylinder.h"
  10. #include "ColoredSphere.h"
  11. #include "ColoredRectangularPrism.h"
  12. #include "ColoredCube.h"
  13.  
  14. using namespace std;
  15.  
  16. void populateThingPtr(Thing* &ptr, ifstream& inFile, char nameBuff[], char colorBuff[], double dbArr[]);
  17.  
  18. int main() {
  19.     const int numRecords = 50;
  20.     Thing* shapePtr = nullptr;
  21.     char nameBuff[30], colorBuff[9];
  22.     double dbArr[4];
  23.     //Pointers below will be used for downcasting
  24.     GeometricSolid* gS = nullptr;
  25.     ColoredThing* cT = nullptr;
  26.  
  27.     colorBuff[8] = 0; //color is always 8 bytes, so set index 9 to null permanently
  28.  
  29.     ifstream inFile("ass3data.bin", ios::binary | ios::in);
  30.  
  31.     if (!inFile) {
  32.         cerr << "Unable to open input file" << endl;
  33.         exit(1);
  34.     }
  35.  
  36.     //Write headers
  37.     cout << fixed << setprecision(4);
  38.     cout << setw(30) << left << "Type of object" << setw(9) << right << "Volume" << "  ";
  39.     cout << left << setw(15) << "Color" << right << setw(15) << "Density" << endl;
  40.  
  41.     for (int i = 0; i < numRecords; i++) {
  42.         inFile.read(nameBuff, 4 * sizeof(char));//4 for null
  43.         populateThingPtr(shapePtr, inFile, nameBuff, colorBuff, dbArr);
  44.  
  45.         gS = dynamic_cast<GeometricSolid*>(shapePtr);
  46.         if (gS) { //if downcast from Thing* to GeometricSolid* was successful
  47.             cout << *gS;
  48.         }
  49.  
  50.         cT = dynamic_cast<ColoredThing*>(shapePtr);
  51.  
  52.         if (cT) {// if downcast from Thing* to ColoredThing* was successful
  53.             cout << *cT;
  54.         }
  55.  
  56.         cout << endl;
  57.  
  58.         delete shapePtr;
  59.     }
  60.  
  61. }
  62.  
  63. void populateThingPtr(Thing* &ptr, ifstream& inFile, char nameBuff[], char colorBuff[], double dbArr[]) {
  64.  
  65.     if (nameBuff == static_cast<string>("spn")) {//need to cast to string in order for == to work
  66.         inFile.read(reinterpret_cast<char*>(dbArr), 1 * sizeof(double));
  67.         ptr = new Sphere(dbArr[0]);
  68.     }
  69.     else if (nameBuff == static_cast<string>("cyn")) {
  70.         inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
  71.         ptr = new Cylinder(dbArr[0], dbArr[1]);
  72.     }
  73.     else if (nameBuff == static_cast<string>("ren")) {
  74.         inFile.read(reinterpret_cast<char*>(dbArr), 3 * sizeof(double));
  75.         ptr = new RectangularPrism(dbArr[0], dbArr[1], dbArr[2]);
  76.     }
  77.     else if (nameBuff == static_cast<string>("cun")) {
  78.         inFile.read(reinterpret_cast<char*>(dbArr), 1 * sizeof(double));
  79.         ptr = new Cube(dbArr[0]);
  80.     }
  81.     else if (nameBuff == static_cast<string>("cyc")) {
  82.         inFile.read(reinterpret_cast<char*>(dbArr), 3 * sizeof(double));
  83.         inFile.read(colorBuff, 8 * sizeof(char));
  84.         ptr = new ColoredCylinder(dbArr[0], dbArr[1], dbArr[2], colorBuff);
  85.     }
  86.     else if (nameBuff == static_cast<string>("spc")) {
  87.         inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
  88.         inFile.read(colorBuff, 8 * sizeof(char));
  89.         ptr = new ColoredSphere(dbArr[0], dbArr[1], colorBuff);
  90.     }
  91.     else if (nameBuff == static_cast<string>("rec")) {
  92.         inFile.read(reinterpret_cast<char*>(dbArr), 4 * sizeof(double));
  93.         inFile.read(colorBuff, 8 * sizeof(char));
  94.         ptr = new ColoredRectangularPrism(dbArr[0], dbArr[1], dbArr[2], dbArr[3], colorBuff);
  95.     }
  96.     else if (nameBuff == static_cast<string>("cuc")) {
  97.         inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
  98.         inFile.read(colorBuff, 8 * sizeof(char));
  99.         ptr = new ColoredCube(dbArr[0], dbArr[1], colorBuff);
  100.     }
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment