mirror of
https://github.com/didyouexpectthat/cs-300.git
synced 2025-03-12 05:35:24 -07:00
Add files via upload
This commit is contained in:
parent
d2277eacb1
commit
ee2cd2b534
243
CoursePlanner.cpp
Normal file
243
CoursePlanner.cpp
Normal file
@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Vector-based Course Tool
|
||||
* Author: Cody Cook
|
||||
* Date: 2023/08/10
|
||||
* Southern New Hampshire University
|
||||
* CS-300-J7303 DSA: Analysis and Design 23EW6
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// build a Course struct
|
||||
struct Course {
|
||||
string courseNumber;
|
||||
string title;
|
||||
vector<string> prerequisites;
|
||||
};
|
||||
|
||||
// global variables
|
||||
vector<Course> courses;
|
||||
|
||||
// functions
|
||||
|
||||
// function to display a course
|
||||
void displayCourse(const Course& c) {
|
||||
|
||||
// if courseNumber is empty, let's just skip it
|
||||
if (c.courseNumber.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// display the course number and course title
|
||||
cout << "Course Number: " << c.courseNumber << endl;
|
||||
cout << "Course Title: " << c.title << endl;
|
||||
|
||||
// display prereqs
|
||||
cout << "Course Prerequisites: ";
|
||||
|
||||
// if there are no prerequisites, print "None"
|
||||
if (c.prerequisites.size() == 0) {
|
||||
cout << "None";
|
||||
}
|
||||
else {
|
||||
for (const auto& prereq : c.prerequisites) {
|
||||
// if this is the last prerequisite, don't print the comma
|
||||
if (prereq != c.prerequisites.back()) {
|
||||
cout << prereq << ", ";
|
||||
}
|
||||
else {
|
||||
cout << prereq;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << endl << endl;
|
||||
}
|
||||
|
||||
// Function to get course info from the user
|
||||
Course getCourse() {
|
||||
string getCourseNumber;
|
||||
|
||||
// Default "empty" course to return in case of an error or if not found
|
||||
Course emptyCourse;
|
||||
|
||||
if (courses.size() == 0) {
|
||||
cout << "\nCourses are not currently loaded. Please load courses.\n\n";
|
||||
return emptyCourse;
|
||||
}
|
||||
else {
|
||||
|
||||
cout << "Enter course number: ";
|
||||
cin >> getCourseNumber;
|
||||
cout << endl;
|
||||
|
||||
for (unsigned int i = 0; i < courses.size(); i++) {
|
||||
if (courses[i].courseNumber == getCourseNumber) {
|
||||
return courses[i];
|
||||
}
|
||||
}
|
||||
|
||||
// if we didn't return already, then
|
||||
// the course wasn't found
|
||||
cout << "Course not found.";
|
||||
cout << "\n\n";
|
||||
}
|
||||
return emptyCourse;
|
||||
}
|
||||
|
||||
// Selection sort function for the vector sorting
|
||||
void selectionSortCourses(vector<Course>& courses) {
|
||||
// Get the total number of courses
|
||||
int n = courses.size();
|
||||
|
||||
// Traverse through all courses in the vector
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Assume the current index holds the smallest courseNumber
|
||||
int min_idx = i;
|
||||
|
||||
// Traverse the vector from the element next to i up to the end
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
// If a smaller courseNumber is found, update min_idx
|
||||
if (courses[j].courseNumber < courses[min_idx].courseNumber) {
|
||||
min_idx = j;
|
||||
}
|
||||
}
|
||||
// Swap the found minimum course with the course at current index 'i'
|
||||
swap(courses[i], courses[min_idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to load courses from a CSV file
|
||||
void loadCourses() {
|
||||
string filePath;
|
||||
string line;
|
||||
|
||||
// empty courses for a fresh import
|
||||
courses.clear();
|
||||
|
||||
// prompt user for the path
|
||||
cout << "\nEnter the Courses csv file (Default: courses.csv): ";
|
||||
|
||||
// clear the newline left in the buffer from previous inputs
|
||||
// we will get the whole link for an absolute path; relative works, too.
|
||||
cin.ignore();
|
||||
getline(cin, filePath);
|
||||
cout << endl;
|
||||
|
||||
if (filePath.front() == '\"' && filePath.back() == '\"') {
|
||||
filePath = filePath.substr(1, filePath.length() - 2);
|
||||
}
|
||||
|
||||
// open the file for reading
|
||||
ifstream file(filePath);
|
||||
|
||||
// if we didn't open the file
|
||||
if (!file.is_open()) {
|
||||
cout << "Error opening file: " << filePath << endl << endl;
|
||||
// error
|
||||
return;
|
||||
}
|
||||
|
||||
// while we go through each line
|
||||
while (getline(file, line)) {
|
||||
stringstream ss(line);
|
||||
Course c;
|
||||
getline(ss, c.courseNumber, ',');
|
||||
getline(ss, c.title, ',');
|
||||
string prereq;
|
||||
while (getline(ss, prereq, ',')) {
|
||||
c.prerequisites.push_back(prereq);
|
||||
}
|
||||
courses.push_back(c);
|
||||
}
|
||||
|
||||
// sort the courses
|
||||
selectionSortCourses(courses);
|
||||
}
|
||||
|
||||
|
||||
void menu() {
|
||||
int menuOption = 0;
|
||||
|
||||
while (menuOption != 9) {
|
||||
// display the menu options
|
||||
cout << "1. Load Data Structure\n";
|
||||
cout << "2. Print Course List\n";
|
||||
cout << "3. Print Course\n";
|
||||
cout << "9. Exit\n\n";
|
||||
|
||||
// ask the user to select an option
|
||||
cout << "What would you like to do? ";
|
||||
cin >> menuOption;
|
||||
|
||||
// check if cin failed
|
||||
if (cin.fail()) {
|
||||
|
||||
// clear the error state
|
||||
cin.clear();
|
||||
|
||||
// remove the erroneous input from the stream
|
||||
cin.ignore(INT_MAX, '\n');
|
||||
cout << "Please enter a valid number." << endl << endl;
|
||||
|
||||
// skip the rest of the loop iteration
|
||||
continue;
|
||||
}
|
||||
|
||||
// process the user's input
|
||||
switch (menuOption) {
|
||||
case 1:
|
||||
// load data structure
|
||||
loadCourses();
|
||||
break;
|
||||
case 2:
|
||||
|
||||
// print course list
|
||||
if (courses.size() == 0) {
|
||||
cout << "\nCourses are not currently loaded. Please load courses.\n\n";
|
||||
}
|
||||
else {
|
||||
for (unsigned int i = 0; i < courses.size(); i++) {
|
||||
// show each course
|
||||
displayCourse(courses[i]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
// get the course
|
||||
Course c = getCourse();
|
||||
|
||||
// show the course details
|
||||
displayCourse(c);
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
// we're just going to edit next loop
|
||||
break;
|
||||
default:
|
||||
// no valid options
|
||||
cout << "Invalid option. Please try again." << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// greet the user
|
||||
cout << "Welcome to the course planner.\n\n";
|
||||
|
||||
// display the menu
|
||||
menu();
|
||||
|
||||
// salutate the user
|
||||
cout << "Thank you for using the course planner!\n\n";
|
||||
|
||||
// exit the program
|
||||
return 0;
|
||||
}
|
||||
|
8
courses.csv
Normal file
8
courses.csv
Normal file
@ -0,0 +1,8 @@
|
||||
MATH201,Discrete Mathematics
|
||||
CSCI300,Introduction to Algorithms,CSCI200,MATH201
|
||||
CSCI350,Operating Systems,CSCI300
|
||||
CSCI101,Introduction to Programming in C++,CSCI100
|
||||
CSCI100,Introduction to Computer Science
|
||||
CSCI301,Advanced Programming in C++,CSCI101
|
||||
CSCI400,Large Software Development,CSCI301,CSCI350
|
||||
CSCI200,Data Structures,CSCI101
|
|
Loading…
x
Reference in New Issue
Block a user