From 5bd264d958a27c89cf065edbc79c5efb33771ddc Mon Sep 17 00:00:00 2001 From: Guilherme Matos Date: Mon, 30 Oct 2023 17:47:28 +0000 Subject: [PATCH] Fixing some bugs --- CMakeLists.txt | 3 ++- main.cpp | 2 ++ src/Runtime.cpp | 30 +++++++++++++++++++----------- src/Runtime.hpp | 2 +- src/Student.cpp | 12 +++++++++--- src/Student.hpp | 9 ++++++++- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4281000..0707ed3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,4 +36,5 @@ add_executable(AED2324_PRJ1_G23 main.cpp src/Utils.cpp src/Utils.hpp src/Runtime.cpp src/Runtime.hpp src/ClassSchedule.cpp src/ClassSchedule.hpp - src/Student.cpp src/Student.hpp) + src/Student.cpp src/Student.hpp + src/Process.cpp src/Process.hpp) diff --git a/main.cpp b/main.cpp index d95278d..65a1c69 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,8 @@ int main(int argc, char** argv) { //ac.display(); Runtime rt(sc, cpu, ac); + + rt.run(); return 0; } diff --git a/src/Runtime.cpp b/src/Runtime.cpp index 1f025a5..92741dd 100644 --- a/src/Runtime.cpp +++ b/src/Runtime.cpp @@ -3,6 +3,7 @@ */ #include "Runtime.hpp" #include "ClassSchedule.hpp" +#include "Student.hpp" #include "Utils.hpp" #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -92,7 +94,7 @@ void Runtime::run() { } else { std::cout << "> "; } - std::cin >> in; + getline(std::cin, in); stream = std::istringstream(in); while (std::getline(stream, buf, ' ')) { line.push_back(buf); @@ -105,6 +107,7 @@ void Runtime::run() { if (!is_batching) { process_args(line); in.clear(); + line.clear(); while (procs.size() != 0) { Process p = procs.front(); procs.pop(); @@ -118,21 +121,21 @@ void Runtime::run() { void Runtime::process_args(std::vector args) { if (args[0] == "remove") { if (args.size() != 3) { - fprintf(stdout, "ERROR: USAGE: remove takes two arguments: remove " - " "); + std::cerr << "ERROR: USAGE: remove takes two arguments: remove " << " " << std::endl; + return; } else { Process t(TypeOfRequest::Remove); t.add_operand(args[1]); t.add_operand(args[2]); - t.add_operand(args[3]); procs.push(t); return; } } if (args[0] == "add") { if (args.size() != 4) { - fprintf(stderr, "ERROR: USAGE: add takes three arguments: add " - " "); + std::cerr << "ERROR: USAGE: add takes three arguments: add " << + " " << std::endl; + return; } else { Process t(TypeOfRequest::Add); t.add_operand(args[1]); @@ -144,8 +147,9 @@ void Runtime::process_args(std::vector args) { } if (args[0] == "switch") { if (args.size() != 4) { - fprintf(stderr, "ERROR: USAGE: switch takes three arguments: switch " - " "); + std::cerr << "ERROR: USAGE: switch takes three arguments: switch " << + " \n" << std::endl; + return; } else { Process t(TypeOfRequest::Switch); t.add_operand(args[1]); @@ -161,16 +165,17 @@ void Runtime::process_args(std::vector args) { void Runtime::handle_process(Process p) { std::vector ops = p.get_ops(); + // handle remove if (p.get_type() == TypeOfRequest::Remove) { uint32_t student_code; try { student_code = stoi(ops[0]); } catch (std::exception e) { - fprintf(stderr, "ERROR: The string %s is not a student_code.", ops[0].c_str()); + std::cerr << "ERROR: The string " << ops[0].c_str() << " is not a student_code." << std::endl; return; } uint16_t uc_code = parse_uc_gen(ops[1]); - if (auto itr = students.find(student_code); itr != students.end()) { + if (auto itr = students.find(Student(student_code, "")); itr != students.end()) { Student s = *itr; std::vector sched = s.get_schedule(); for (auto a : sched) { @@ -180,7 +185,10 @@ void Runtime::handle_process(Process p) { return; } } - return; + } else { + std::cerr << "ERROR: There is no such student with code: " << student_code << std::endl; } + return; } + // End Remove } diff --git a/src/Runtime.hpp b/src/Runtime.hpp index 4bf4140..d64a814 100644 --- a/src/Runtime.hpp +++ b/src/Runtime.hpp @@ -19,7 +19,7 @@ class Runtime { private: - std::set> students; + std::set students; std::vector classes; std::queue procs; std::stack history; diff --git a/src/Student.cpp b/src/Student.cpp index 9b3ffe3..aa8dae7 100644 --- a/src/Student.cpp +++ b/src/Student.cpp @@ -145,9 +145,15 @@ bool Student::operator<(const Student &other) const { return this->code < other.code; } -bool Student::operator<(const uint32_t &other) const { - return this->code < other; -} +// SmolKey::SmolKey(uint32_t a): uc_code_(a) {} +// uint32_t SmolKey::get_key() const {return uc_code_;} + +// bool operator<(const Student& s,const SmolKey &other) { +// return s.get_code() < other.get_key(); +// } +// bool operator<(const SmolKey& s,const Student &other) { +// return other.get_code() < s.get_key(); +// } uint32_t Student::get_code() const { return code; } diff --git a/src/Student.hpp b/src/Student.hpp index f6f149c..2806417 100644 --- a/src/Student.hpp +++ b/src/Student.hpp @@ -8,6 +8,14 @@ #include "Utils.hpp" #include +// class SmolKey { +// uint32_t uc_code_; +// public: +// SmolKey(uint32_t a); +// uint32_t get_key() const; + +// }; + class Student { private: @@ -21,7 +29,6 @@ class Student { Student(uint32_t code, std::string name); uint32_t get_code() const; bool operator<(const Student& other) const; - bool operator<(const uint32_t& other) const; std::vector& get_schedule(); void add_to_class(ClassSchedule* c); OperationResult verify_add(ClassSchedule* c);