alansam

StackImpl00

Oct 19th, 2020 (edited)
715
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  StackImpl00.cpp
  3. //  CF.StackImpl00
  4. //
  5. //  Created on 10/19/20.
  6. //``@author: John Church https://www.facebook.com/JayAreSee79
  7. //
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <stack>
  13. #include <ctime>
  14. #include <iomanip>
  15. #include <stdexcept>
  16. #include <system_error>
  17. #include <cerrno>
  18.  
  19. using namespace std::literals::string_literals;
  20.  
  21. namespace JC {
  22. int main(int argc, const char * argv[]);
  23. } /* namespace JC */
  24.  
  25. /*
  26.  *  MARK: main()
  27.  */
  28. int main(int argc, const char * argv[]) {
  29.   std::cout << "CF.StackImpl00" << std::endl;
  30.   int RC;
  31.  
  32.   RC = JC::main(argc, argv);
  33.  
  34.   return RC;
  35. }
  36.  
  37. namespace JC {
  38. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. size_t const SIZE = 10;
  40.  
  41. struct studentRecord {
  42.   std::string firstname;
  43.   std::string lastname;
  44.   int studentID;
  45.   double GPA;
  46.  
  47.   studentRecord(std::string const & fn = "."s, std::string const & ln = "."s,
  48.                 int sid = -1, double gpa = 0.0) :
  49.   firstname(fn), lastname(ln), studentID(sid), GPA(gpa) {
  50.     return;
  51.   }
  52.  
  53.   virtual
  54.   ~studentRecord() = default;
  55.  
  56.   void serialize(std::ostream & out) {
  57.     out << std::setw( 8) << firstname
  58.         << std::setw(12) << lastname
  59.         << std::setw( 7) << studentID
  60.         << std::setw( 5) << std::fixed << std::setprecision(1) << GPA
  61.         << std::endl;
  62.   }
  63. } record[SIZE];
  64. //  TODO: for debugging
  65. size_t __attribute__((__unused__)) record_l = sizeof(record);
  66. size_t __attribute__((__unused__)) record_e = record_l / sizeof(*record);
  67.  
  68. //  TODO: find a better way to get filenames into program
  69. #ifdef __WIN32__
  70. static
  71. std::string const
  72. SL = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\studentList.txt";
  73. static
  74. std::string const
  75. UL = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\updatedList.txt";
  76. #else
  77. static
  78. std::string const
  79. SL { "/Users/guest/tmp/jc_studentList.txt" };
  80. static
  81. std::string const
  82. UL { "/Users/guest/tmp/jc_updatedList.txt" };
  83. #endif
  84.  
  85. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. void loadData(void) {
  87.  
  88.   //  TODO: file name should be a constant so if it changes all instances of use get the changed name
  89.   std::ifstream inFile(SL.c_str());
  90.   if (!inFile.is_open()) {
  91.     std::cout << "File can't be opened! " << std::endl;
  92. #ifdef __WIN32__
  93.     system("PAUSE");  //  TODO: won't work on anything but Windows, find portable solution
  94. #endif
  95.     throw std::system_error(ENOENT, std::generic_category(), SL);
  96.   }
  97.  
  98.   for (size_t i_ = 0; i_ < SIZE; i_++) {
  99.     std::string fn;
  100.     std::string ln;
  101.     int sid;
  102.     double gdp;
  103.     inFile >> fn >> ln >> sid >> gdp;
  104.     if (!inFile.eof()) {
  105.       studentRecord sr(fn, ln, sid, gdp);
  106.       record[i_] = sr;
  107.     }
  108.     else {
  109.       break;
  110.     }
  111.   }
  112.   inFile.close();
  113. }
  114.  
  115. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. void print(void) {
  117.   std::cout << "############# Reading the input file " << SL << " #############" << std::endl;
  118.   std::fstream file;
  119.   //  TODO: file name should be a constant so if it changes all instances of use get the changed name
  120.   file.open(SL.c_str());
  121.   if (file.is_open()) {
  122.     // checking whether the file is open
  123.     std::string tp;
  124.     while (getline(file, tp)) {
  125.       //read data from file object and put it into string.
  126.       std::cout << tp << "\n"; //print the data of the string
  127.     }
  128.     file.close();
  129.   }
  130.   else {
  131.     throw std::system_error(ENOENT, std::generic_category(), SL);
  132.   }
  133.   /* *
  134.   std::cout << "############# The content of the Array: #############" << std::endl;
  135.   for (int i = 0; i < SIZE; i++)
  136.   {
  137.     std::cout << "Student name: " << record[i].firstname << " ";
  138.     std::cout << record[i].lastname << " ";
  139.     std::cout << "ID#: " << record[i].studentID << " ";
  140.     std::cout << "GPA: " << record[i].GPA << " " << std::endl;
  141.   }* */
  142. }
  143.  
  144. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  145. void createStack(std::stack<std::string> & stk) {
  146.   stk.emplace("test");
  147.   while (!stk.empty()) {
  148.     std::cout << '\t' << stk.top();
  149.     stk.pop();
  150.   }
  151.   std::cout << '\n';
  152. }
  153.  
  154. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. void createStack(std::stack<studentRecord> & stk) {
  156.   for (size_t i_ = 0; i_< SIZE && record[i_].studentID > -1; ++i_) {
  157.     stk.emplace(record[i_]);
  158.   }
  159. }
  160.  
  161. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. void writeData(void) {
  163.   std::cout << "\n############# Creating New File: " << UL << "#############" << std::endl;
  164.   //  TODO: file name should be a constant so if it changes all instances of use get the changed name
  165.   std::ofstream outFile(UL.c_str());
  166.  
  167.   for (size_t i_ = 0; i_ < SIZE; i_++) {
  168.     if (!outFile.is_open()) {
  169.       std::cout << "File can't be written! " << std::endl;
  170. #ifdef __WIN32__
  171.       system("Pause");  //  TODO: won't work on anything but Windows, find portable solution
  172. #endif
  173.       throw std::system_error(EBADF, std::generic_category(), UL);
  174.     }
  175.  
  176.     if (record[i_].studentID == -1) {
  177.       break;
  178.     }
  179.  
  180.     record[i_].serialize(outFile);
  181.   }
  182.   outFile.close();
  183.   std::cout << "Success!!\nNew file has been created: " << std::endl;
  184. }
  185.  
  186. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  187. void printNewFile() {
  188.   std::fstream newfile;
  189.   //  TODO: file name should be a constant so if it changes all instances of use get the changed name
  190.   newfile.open(UL.c_str());
  191.   if (newfile.is_open()) {
  192.     //checking whether the file is open
  193.     std::string tp;
  194.     while (getline(newfile, tp)) {
  195.       //read data from file object and put it into string.
  196.       std::cout << tp << "\n"; //print the data of the string
  197.     }
  198.     newfile.close();
  199.   }
  200.   else {
  201.     throw std::system_error(EBADF, std::generic_category(), UL);
  202.   }
  203. }
  204.  
  205. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  206. //  TODO: return a random number to the caller
  207. int randomNumber() {
  208.   std::cout << "\n############# Now we will generate a random number #############" << std::endl;
  209.   //  TODO: use a better ranndom number generator
  210.   srand((unsigned) time(NULL));
  211.   int random_nr = (rand() % 20) + 1;
  212.   std::cout << "Random Number generated with ctime: " << random_nr << std::endl;
  213.  
  214.   return random_nr;
  215. }
  216.  
  217. // ////////////////////////////////////////////////////////////////////////////////////////////////////////
  218. int main(int argc, const char * argv[]) {
  219.   try {
  220.     loadData();
  221.     print();
  222.     writeData();
  223.     printNewFile();
  224.     randomNumber();
  225.  
  226.     //studentRecord record[SIZE];
  227.     //std::ifstream inFile;
  228.     //std::string fName = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\updatedList.txt";
  229.     //loadData(fName);
  230.  
  231.  
  232. //    std::stack<std::string> stk;
  233.     std::stack<studentRecord> stk;
  234.     //record.push();
  235.  
  236.     std::cout << "\n############# The contents of the stack #############" << std::endl;
  237.  
  238.     createStack(stk);
  239.  
  240.     std::cout << "Stack size: " << stk.size() << std::endl;
  241.     std::cout << "The contents of the stack: \n";
  242.     while (!stk.empty()) {
  243.       studentRecord sr = stk.top();
  244.       stk.pop();
  245.       sr.serialize(std::cout);
  246.     }
  247.  
  248.     //  TODO: What is this for?
  249. //    char ch;
  250. //    std::cin >> ch;
  251.   }
  252.   catch (std::exception & ex) {
  253.     std::cerr << "GRONK! " << ex.what() << std::endl;
  254.   }
  255.  
  256.   return 0;
  257. }
  258. } /* namespace JC */
  259.  
RAW Paste Data