Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Fixing some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme-ds-matos committed Oct 30, 2023
1 parent 83f25a8 commit 5bd264d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ int main(int argc, char** argv) {
//ac.display();

Runtime rt(sc, cpu, ac);

rt.run();

return 0;
}
30 changes: 19 additions & 11 deletions src/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
*/
#include "Runtime.hpp"
#include "ClassSchedule.hpp"
#include "Student.hpp"
#include "Utils.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <iostream>
#include <ostream>
#include <queue>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -118,21 +121,21 @@ void Runtime::run() {
void Runtime::process_args(std::vector<std::string> args) {
if (args[0] == "remove") {
if (args.size() != 3) {
fprintf(stdout, "ERROR: USAGE: remove takes two arguments: remove "
"<student_code> <uc_code>");
std::cerr << "ERROR: USAGE: remove takes two arguments: remove " << "<student_code> <uc_code>" << 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 "
"<student_code> <uc_code> <class_code>");
std::cerr << "ERROR: USAGE: add takes three arguments: add " <<
"<student_code> <uc_code> <class_code>" << std::endl;
return;
} else {
Process t(TypeOfRequest::Add);
t.add_operand(args[1]);
Expand All @@ -144,8 +147,9 @@ void Runtime::process_args(std::vector<std::string> args) {
}
if (args[0] == "switch") {
if (args.size() != 4) {
fprintf(stderr, "ERROR: USAGE: switch takes three arguments: switch "
"<student_code> <student_code> <uc_code>");
std::cerr << "ERROR: USAGE: switch takes three arguments: switch " <<
"<student_code> <student_code> <uc_code>\n" << std::endl;
return;
} else {
Process t(TypeOfRequest::Switch);
t.add_operand(args[1]);
Expand All @@ -161,16 +165,17 @@ void Runtime::process_args(std::vector<std::string> args) {

void Runtime::handle_process(Process p) {
std::vector<std::string> 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<ClassSchedule*> sched = s.get_schedule();
for (auto a : sched) {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion src/Runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class Runtime {
private:
std::set<Student, std::less<>> students;
std::set<Student> students;
std::vector<ClassSchedule> classes;
std::queue<Process> procs;
std::stack<Process> history;
Expand Down
12 changes: 9 additions & 3 deletions src/Student.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
9 changes: 8 additions & 1 deletion src/Student.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
#include "Utils.hpp"
#include <string>

// class SmolKey {
// uint32_t uc_code_;
// public:
// SmolKey(uint32_t a);
// uint32_t get_key() const;

// };


class Student {
private:
Expand All @@ -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<ClassSchedule*>& get_schedule();
void add_to_class(ClassSchedule* c);
OperationResult verify_add(ClassSchedule* c);
Expand Down

0 comments on commit 5bd264d

Please sign in to comment.