All pastes #9450 Raw Edit

Untitled

public text v1 · immutable
#9450 ·published 2005-04-12 23:38 UTC
rendered paste body
///* ========================================================================== */
///*                                                                            */
///*  exams.cpp                                                                 */
///*  Isidoros Passadis // Comp Sci 11 // Lab F                                 */                      */
///*                                                                            */
///*  Description:                                                              */
///*  a program that checks if a student’s final exams schedule is valid.       */
///* ========================================================================== */

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

// examInfo structure
struct examInfo {
    string name;
    int month;
    int day;
    int hour;
    int min;
    string ampm;
    int duration;
};

// Functions Declarations
void readFile(string, examInfo&, examInfo& , examInfo&, string&); // reads and fills out structs
void outputInfo(examInfo); // outputs struct info
void isConflict(examInfo, examInfo, examInfo); // exam conflict?
string month(int); // determines alphabetical (vs numerical) month

int main () {
    // Variable Declarations
    string fileName, userName;
    examInfo exam1, exam2, exam3;
    
    // Output Header
    cout    << "\n*********************************"
            << "\n     student: Isidoros Passadis"
            << "\n     lab section F, Wed. 1.30pm"
            << "\n*********************************\n";
    
    // Filename Input
    cout    << "\nEnter the name of the file: ";
    getline(cin, fileName);
    
    // Populate the structures
    readFile(fileName, exam1, exam2, exam3, userName);
    
    // Student's name
    cout    << "\nExams schedule for: " << userName << endl;
    
    // Output the info in the structures
    // |- Broke this function down to parse each exam separately as per the 
    // \- wording in the assignment: "display information about *each* exam".
    outputInfo(exam1);
    outputInfo(exam2);
    outputInfo(exam3);
    
    // Output of exam conflicts
    isConflict(exam1, exam2, exam3);
    
    return 0;
}

void readFile(string dataFile, examInfo& exam1,  examInfo& exam2, examInfo& exam3, string &stuName) {
    // Open the file and make sure it actually exists
    ifstream examFile;
    examFile.open( dataFile.c_str() ); // Opening the file
    while (!examFile)  // Loop in case the user messes up the name
    {
        examFile.clear();
        cout    << "ERROR: File does not exist.\n"
                << "Please re-enter the name of the file: ";
        getline(cin, dataFile);
        examFile.open(dataFile.c_str());
    }
    
    // Go through the file and populate the exams
    getline(examFile, stuName); // Get the student's name
    // Get the info for exam1 (had we been able to use arrays (next chapter,
    // grrrr) i could have made an array of examFile structs and used a for
    // loop instead of this hard-coded method.

    examFile    >> exam1.name >> exam1.month >> exam1.day >> exam1.hour
                >> exam1.min >> exam1.ampm >> exam1.duration;
    examFile    >> exam2.name >> exam2.month >> exam2.day >> exam2.hour
                >> exam2.min >> exam2.ampm >> exam2.duration;
    examFile    >> exam3.name >> exam3.month >> exam3.day >> exam3.hour
                >> exam3.min >> exam3.ampm >> exam3.duration;        
    examFile.close();
}


void outputInfo(examInfo exam) {
    cout << left << setw(9) << exam.name; // Exam name
    cout << month(exam.month) << " " << exam.day << "," << endl; // Exam date
    cout << left << setw(9) << "" << "Exam starts at: " << exam.hour << ":"; // time (hours)
    if (exam.min < 10)
        cout << "0";
    cout << exam.min << " " << exam.ampm // time (minutes and am/pm)
         << " and lasts " << exam.duration << " minutes." << endl;
}

string month(int wMonth) {
    string rMonth;
    // Test to see which month it is
    switch (wMonth) {
        case 1:
            rMonth = "January";
            break;
        case 2:
            rMonth = "February";
            break;
        case 3:
            rMonth = "March";
            break;
        case 4:
            rMonth = "April";
            break;
        case 5:
            rMonth = "May";
            break;
        case 6:
            rMonth = "June";
            break;
        case 7:
            rMonth = "July";
            break;
        case 8:
            rMonth = "August";
            break;
        case 9:
            rMonth = "September";
            break;
        case 10:
            rMonth = "October";
            break;
        case 11:
            rMonth = "November";
            break;
        case 12:
            rMonth = "December";
            break;
        default: rMonth = "error";
    }
    return rMonth;
}


void isConflict(examInfo exam1, examInfo exam2, examInfo exam3) {
    int e1minutes = 0, e2minutes = 0, e3minutes = 0; // total minute variables
    
    if (exam1.month == exam2.month || exam1.month == exam3.month || exam2.month == exam3.month) {
    // month is the same
        if (exam1.day == exam2.day || exam1.day == exam3.day || exam2.day == exam3.day) {
        // day is the same
            // i will make comparisons on times based on total minutes
            if (exam1.ampm == "pm")     // anything after 12 gets bumped up
                e1minutes = (12*60);
            e1minutes += (exam1.hour*60) + exam1.min;

            if (exam2.ampm == "pm")
                e2minutes = (12*60);
            e2minutes += (exam2.hour*60) + exam2.min;
                            
            if (exam3.ampm == "pm")
                e3minutes = (12*60);
            e3minutes += (exam3.hour*60) + exam3.min;
                        
            // -- 1st group of possible overlaps
            // Conflict between 1 and 2            
            if (    (e1minutes + exam1.duration) > (e2minutes - 15) &&
                    (e1minutes + exam1.duration) < (e2minutes + exam2.duration)        ) 
                cout << "\nThere is conflict between " << exam1.name << " exam and " << exam2.name 
                     << " exam.";
            // Conflict between 2 and 1
            if (    (e2minutes + exam2.duration) > (e1minutes - 15) &&
                    (e2minutes + exam2.duration) < (e1minutes + exam1.duration)        ) 
                cout << "\nThere is conflict between " << exam2.name << " exam and " << exam1.name 
                     << " exam.";
                
            // --- 2nd group of possible overlaps
            // Conflict between 2 and 3
            if (    (e2minutes + exam2.duration) > (e3minutes - 15) &&
                    (e2minutes + exam2.duration) < (e3minutes + exam3.duration)        ) 
                cout << "\nThere is conflict between " << exam2.name << " exam and " << exam3.name 
                     << " exam.";
            // Conlflict between 3 and 2
            if (    (e3minutes + exam3.duration) > (e2minutes - 15) &&
                    (e3minutes + exam3.duration) < (e2minutes + exam2.duration)        ) 
                cout << "\nThere is conflict between " << exam3.name << " exam and " << exam2.name 
                     << " exam.";
            
            // --- 3rd group of possible overlaps    
            // Conflict between 1 and 3            
            if (    (e1minutes + exam1.duration) > (e3minutes - 15) &&
                    (e1minutes + exam1.duration) < (e3minutes + exam3.duration)        ) 
                cout << "\nThere is conflict between " << exam1.name << " exam and " << exam3.name 
                     << " exam.";
            // Conflict between 3 and 1
            if (    (e3minutes + exam3.duration) > (e1minutes - 15) &&
                    (e3minutes + exam3.duration) < (e1minutes + exam1.duration)        ) 
                cout << "\nThere is conflict between " << exam3.name << " exam and " << exam1.name 
                     << " exam.";

        }
    }
    else
        cout << "\nThere are no exam conflicts.\n";
}