|
| 1 | +#include <iostream> |
| 2 | +#include <ctime> |
| 3 | +#include <fstream> |
| 4 | +#include <vector> |
| 5 | +#include <sstream> |
| 6 | +#include <regex> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +// Declaration of the class |
| 11 | +class Point2D; |
| 12 | +class Point3D; |
| 13 | +class RandomIntegerGenerator; |
| 14 | +class StringToPoint3D; |
| 15 | +class FileIO; |
| 16 | + |
| 17 | +// Implementation of the class |
| 18 | +class Point2D { |
| 19 | +private: |
| 20 | + int _x; |
| 21 | + int _y; |
| 22 | +public: |
| 23 | + Point2D(int x, int y) { |
| 24 | + this->_x = x; |
| 25 | + this->_y = y; |
| 26 | + } |
| 27 | + |
| 28 | + int getX() { return this->_x; } |
| 29 | + int getY() { return this->_y; } |
| 30 | + |
| 31 | + virtual void print() { cout << "(" << getX() << "," << getY() << ")" << endl; } |
| 32 | +}; |
| 33 | + |
| 34 | +class Point3D : public Point2D { |
| 35 | +private: |
| 36 | + int _z; |
| 37 | +public: |
| 38 | + int getZ() { return _z; } |
| 39 | + |
| 40 | + void setZ(int z) { _z = z; } |
| 41 | + |
| 42 | + Point3D(int x, int y, int z) : Point2D(x, y) { this->_z = z; } |
| 43 | + |
| 44 | + void print() { cout << "(" << getX() << ", " << getY() << ", " << getZ() << ")" << endl; } |
| 45 | +}; |
| 46 | + |
| 47 | +class RandomIntegerGenerator { |
| 48 | +public: |
| 49 | + RandomIntegerGenerator() { srand(time(NULL)); } |
| 50 | + int next() { return rand(); } |
| 51 | + int next(int ceiling) { return rand() % ceiling; } |
| 52 | + int next(int left, int right) { return rand() % (right - left + 1) + left; } |
| 53 | +}; |
| 54 | + |
| 55 | +class StringToPoint3D { |
| 56 | +public: |
| 57 | + bool isValid(string input) { |
| 58 | + // the valid input is a string of 3 digits eg "1, 2, 3" |
| 59 | + const string pattern = "\\d+, \\d+, \\d+"; |
| 60 | + regex rgx(pattern); |
| 61 | + bool valid = regex_match(input, rgx); |
| 62 | + return valid; |
| 63 | + } |
| 64 | + |
| 65 | +public: |
| 66 | + Point3D* convert(string input) { |
| 67 | + Point3D* p = NULL; |
| 68 | + if (isValid(input)) { |
| 69 | + //! The first way is using tokenizer |
| 70 | + /* |
| 71 | + vector<string> tokens; |
| 72 | + istringstream iss(input); |
| 73 | + string token; |
| 74 | + while (getline(iss, token, ',')) { |
| 75 | + tokens.push_back(token); |
| 76 | + } |
| 77 | + int x = stoi(tokens[0]); |
| 78 | + int y = stoi(tokens[1]); |
| 79 | + int z = stoi(tokens[2]); |
| 80 | + p = new Point3D(x, y, z); |
| 81 | + */ |
| 82 | + |
| 83 | + //! The second way is using stringstream |
| 84 | + string x, y, z; |
| 85 | + stringstream ss(input); |
| 86 | + getline(ss, x, ','); |
| 87 | + getline(ss, y, ' '); |
| 88 | + getline(ss, y, ','); |
| 89 | + getline(ss, z, ' '); |
| 90 | + getline(ss, z); |
| 91 | + p = new Point3D(stoi(x), stoi(y), stoi(z)); |
| 92 | + } |
| 93 | + return p; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +class FileIO { |
| 98 | +public: |
| 99 | + void saveFile(string filename, vector<Point3D> data) { |
| 100 | + ofstream outfile(filename); |
| 101 | + for (int i = 0; i < data.size(); i++) { |
| 102 | + outfile << "(" << data[i].getX() << ", " << |
| 103 | + data[i].getY() << ", " << data[i].getZ() << ")" << endl; |
| 104 | + } |
| 105 | + cout << "All points are saved successfully." << endl; |
| 106 | + outfile.close(); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +int main() { |
| 111 | + // Call class StringToPoint3D to convert string to Point3D |
| 112 | + // Call vector<Point3D> data to store all Point3D objects |
| 113 | + // Call class FileIO to save data to a file |
| 114 | + StringToPoint3D stp; |
| 115 | + vector<Point3D> points; |
| 116 | + FileIO fio; |
| 117 | + |
| 118 | + // Call class RandomIntegerGenerator to generate random integer |
| 119 | + RandomIntegerGenerator rig; |
| 120 | + int n = rig.next(3, 7); |
| 121 | + |
| 122 | + // Request user to input n Point3D objects |
| 123 | + cout << "Please input " << n << " points: "; |
| 124 | + cout << " (The valid inputs are 3 digits separated by comma.)" << endl; |
| 125 | + string input; |
| 126 | + |
| 127 | + // Convert string to Point3D object and store them in vector<Point3D> data |
| 128 | + for (int i = 0; i < n; i++) { |
| 129 | + getline(cin, input); |
| 130 | + if (stp.isValid(input)) { |
| 131 | + Point3D* p = NULL; |
| 132 | + p = stp.convert(input); |
| 133 | + points.push_back(*p); |
| 134 | + } |
| 135 | + else { |
| 136 | + cout << "Invalid input. Please input again!" << endl; |
| 137 | + i--; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + cout << endl; |
| 142 | + // Print all Point3D objects in vector<Point3D> data |
| 143 | + for (int i = 0; i < points.size(); i++) { |
| 144 | + points[i].print(); |
| 145 | + } |
| 146 | + |
| 147 | + cout << endl; |
| 148 | + // Call saveFile to save all Point3D objects in vector<Point3D> data |
| 149 | + fio.saveFile("point3d.txt", points); |
| 150 | + |
| 151 | + cout << endl << "Press any key to exit..." << endl; |
| 152 | + cin.get(); |
| 153 | +} |
0 commit comments