Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // StackImpl00.cpp
- // CF.StackImpl00
- //
- // Created on 10/19/20.
- //``@author: John Church https://www.facebook.com/JayAreSee79
- //
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stack>
- #include <ctime>
- #include <iomanip>
- #include <stdexcept>
- #include <system_error>
- #include <cerrno>
- using namespace std::literals::string_literals;
- namespace JC {
- int main(int argc, const char * argv[]);
- } /* namespace JC */
- /*
- * MARK: main()
- */
- int main(int argc, const char * argv[]) {
- std::cout << "CF.StackImpl00" << std::endl;
- int RC;
- RC = JC::main(argc, argv);
- return RC;
- }
- namespace JC {
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- size_t const SIZE = 10;
- struct studentRecord {
- std::string firstname;
- std::string lastname;
- int studentID;
- double GPA;
- studentRecord(std::string const & fn = "."s, std::string const & ln = "."s,
- int sid = -1, double gpa = 0.0) :
- firstname(fn), lastname(ln), studentID(sid), GPA(gpa) {
- return;
- }
- virtual
- ~studentRecord() = default;
- void serialize(std::ostream & out) {
- out << std::setw( 8) << firstname
- << std::setw(12) << lastname
- << std::setw( 7) << studentID
- << std::setw( 5) << std::fixed << std::setprecision(1) << GPA
- << std::endl;
- }
- } record[SIZE];
- // TODO: for debugging
- size_t __attribute__((__unused__)) record_l = sizeof(record);
- size_t __attribute__((__unused__)) record_e = record_l / sizeof(*record);
- // TODO: find a better way to get filenames into program
- #ifdef __WIN32__
- static
- std::string const
- SL = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\studentList.txt";
- static
- std::string const
- UL = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\updatedList.txt";
- #else
- static
- std::string const
- SL { "/Users/guest/tmp/jc_studentList.txt" };
- static
- std::string const
- UL { "/Users/guest/tmp/jc_updatedList.txt" };
- #endif
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void loadData(void) {
- // TODO: file name should be a constant so if it changes all instances of use get the changed name
- std::ifstream inFile(SL.c_str());
- if (!inFile.is_open()) {
- std::cout << "File can't be opened! " << std::endl;
- #ifdef __WIN32__
- system("PAUSE"); // TODO: won't work on anything but Windows, find portable solution
- #endif
- throw std::system_error(ENOENT, std::generic_category(), SL);
- }
- for (size_t i_ = 0; i_ < SIZE; i_++) {
- std::string fn;
- std::string ln;
- int sid;
- double gdp;
- inFile >> fn >> ln >> sid >> gdp;
- if (!inFile.eof()) {
- studentRecord sr(fn, ln, sid, gdp);
- record[i_] = sr;
- }
- else {
- break;
- }
- }
- inFile.close();
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void print(void) {
- std::cout << "############# Reading the input file " << SL << " #############" << std::endl;
- std::fstream file;
- // TODO: file name should be a constant so if it changes all instances of use get the changed name
- file.open(SL.c_str());
- if (file.is_open()) {
- // checking whether the file is open
- std::string tp;
- while (getline(file, tp)) {
- //read data from file object and put it into string.
- std::cout << tp << "\n"; //print the data of the string
- }
- file.close();
- }
- else {
- throw std::system_error(ENOENT, std::generic_category(), SL);
- }
- /* *
- std::cout << "############# The content of the Array: #############" << std::endl;
- for (int i = 0; i < SIZE; i++)
- {
- std::cout << "Student name: " << record[i].firstname << " ";
- std::cout << record[i].lastname << " ";
- std::cout << "ID#: " << record[i].studentID << " ";
- std::cout << "GPA: " << record[i].GPA << " " << std::endl;
- }* */
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void createStack(std::stack<std::string> & stk) {
- stk.emplace("test");
- while (!stk.empty()) {
- std::cout << '\t' << stk.top();
- stk.pop();
- }
- std::cout << '\n';
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void createStack(std::stack<studentRecord> & stk) {
- for (size_t i_ = 0; i_< SIZE && record[i_].studentID > -1; ++i_) {
- stk.emplace(record[i_]);
- }
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void writeData(void) {
- std::cout << "\n############# Creating New File: " << UL << "#############" << std::endl;
- // TODO: file name should be a constant so if it changes all instances of use get the changed name
- std::ofstream outFile(UL.c_str());
- for (size_t i_ = 0; i_ < SIZE; i_++) {
- if (!outFile.is_open()) {
- std::cout << "File can't be written! " << std::endl;
- #ifdef __WIN32__
- system("Pause"); // TODO: won't work on anything but Windows, find portable solution
- #endif
- throw std::system_error(EBADF, std::generic_category(), UL);
- }
- if (record[i_].studentID == -1) {
- break;
- }
- record[i_].serialize(outFile);
- }
- outFile.close();
- std::cout << "Success!!\nNew file has been created: " << std::endl;
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- void printNewFile() {
- std::fstream newfile;
- // TODO: file name should be a constant so if it changes all instances of use get the changed name
- newfile.open(UL.c_str());
- if (newfile.is_open()) {
- //checking whether the file is open
- std::string tp;
- while (getline(newfile, tp)) {
- //read data from file object and put it into string.
- std::cout << tp << "\n"; //print the data of the string
- }
- newfile.close();
- }
- else {
- throw std::system_error(EBADF, std::generic_category(), UL);
- }
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- // TODO: return a random number to the caller
- int randomNumber() {
- std::cout << "\n############# Now we will generate a random number #############" << std::endl;
- // TODO: use a better ranndom number generator
- srand((unsigned) time(NULL));
- int random_nr = (rand() % 20) + 1;
- std::cout << "Random Number generated with ctime: " << random_nr << std::endl;
- return random_nr;
- }
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////
- int main(int argc, const char * argv[]) {
- try {
- loadData();
- print();
- writeData();
- printNewFile();
- randomNumber();
- //studentRecord record[SIZE];
- //std::ifstream inFile;
- //std::string fName = "C:\\Users\\churc\\Desktop\\NSU\\2020_Fall_Data Structures\\HW3 Stacks\\updatedList.txt";
- //loadData(fName);
- // std::stack<std::string> stk;
- std::stack<studentRecord> stk;
- //record.push();
- std::cout << "\n############# The contents of the stack #############" << std::endl;
- createStack(stk);
- std::cout << "Stack size: " << stk.size() << std::endl;
- std::cout << "The contents of the stack: \n";
- while (!stk.empty()) {
- studentRecord sr = stk.top();
- stk.pop();
- sr.serialize(std::cout);
- }
- // TODO: What is this for?
- // char ch;
- // std::cin >> ch;
- }
- catch (std::exception & ex) {
- std::cerr << "GRONK! " << ex.what() << std::endl;
- }
- return 0;
- }
- } /* namespace JC */
Add Comment
Please, Sign In to add comment