example using the course class

Dependencies:   4DGL-uLCD-SE Course mbed SDFileSystem

Committer:
kkizirian
Date:
Mon Dec 05 02:50:59 2016 +0000
Revision:
3:4984f0a32f32
Parent:
2:84925a4bf964
Child:
4:d4a652a577a0
Fixed addCourseToVector function so that it works if course is being added to very end of vector. Also updated the way the LCD display is done so it loops through all vector elements.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkizirian 0:6ced83c58574 1 #include "mbed.h"
kkizirian 0:6ced83c58574 2 #include "Course.h"
kkizirian 0:6ced83c58574 3 #include <string>
kkizirian 0:6ced83c58574 4 #include <vector>
kkizirian 0:6ced83c58574 5 #include "uLCD_4DGL.h"
kkizirian 0:6ced83c58574 6
kkizirian 0:6ced83c58574 7 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
kkizirian 0:6ced83c58574 8 vector<Course> courseVec;
kkizirian 0:6ced83c58574 9 int addCourseToVector(vector<Course>& cVec, Course newCourse);
kkizirian 0:6ced83c58574 10
kkizirian 0:6ced83c58574 11 int main() {
kkizirian 0:6ced83c58574 12 Course course1("HWY", 6, 5, "PM");
kkizirian 0:6ced83c58574 13 Course course2("LVE", 8, 10, "AM");
kkizirian 0:6ced83c58574 14 Course course3("CLH", 8, 25, "AM");
kkizirian 0:6ced83c58574 15 Course course4("KLS", 8, 15, "AM");
kkizirian 0:6ced83c58574 16 Course course5("LVE", 2, 30, "PM");
kkizirian 3:4984f0a32f32 17 Course course6("CLH", 2, 30, "PM");
kkizirian 3:4984f0a32f32 18 Course course7("CLH", 8, 15, "PM");
kkizirian 3:4984f0a32f32 19
kkizirian 3:4984f0a32f32 20 int test = 0;
kkizirian 3:4984f0a32f32 21 test = addCourseToVector(courseVec, course1);
kkizirian 3:4984f0a32f32 22 test = addCourseToVector(courseVec, course2);
kkizirian 3:4984f0a32f32 23 test = addCourseToVector(courseVec, course3);
kkizirian 3:4984f0a32f32 24 test = addCourseToVector(courseVec, course4);
kkizirian 3:4984f0a32f32 25 test = addCourseToVector(courseVec, course5);
kkizirian 3:4984f0a32f32 26 test = addCourseToVector(courseVec, course6);
kkizirian 3:4984f0a32f32 27 if (test == 0)
kkizirian 3:4984f0a32f32 28 uLCD.printf("uh oh!");
kkizirian 3:4984f0a32f32 29 test = addCourseToVector(courseVec, course7);
kkizirian 3:4984f0a32f32 30
kkizirian 0:6ced83c58574 31 uLCD.cls();
kkizirian 3:4984f0a32f32 32 for (int i = 0; i < courseVec.size(); i++) {
kkizirian 3:4984f0a32f32 33 uLCD.locate(0, i);
kkizirian 3:4984f0a32f32 34 uLCD.printf("%s", courseVec[i].getDisplayString());
kkizirian 3:4984f0a32f32 35 }
kkizirian 0:6ced83c58574 36 }
kkizirian 0:6ced83c58574 37
kkizirian 0:6ced83c58574 38 // this adds the paramter newCourse to the vector cVec in the correct
kkizirian 2:84925a4bf964 39 // location in the vector by time, returns 1 if successful and 0 if
kkizirian 2:84925a4bf964 40 // unsuccessful
kkizirian 0:6ced83c58574 41 int addCourseToVector(vector<Course>& cVec, Course newCourse) {
kkizirian 0:6ced83c58574 42
kkizirian 3:4984f0a32f32 43 int numIterations = 0;
kkizirian 3:4984f0a32f32 44
kkizirian 0:6ced83c58574 45 if (cVec.size() == 0) {
kkizirian 0:6ced83c58574 46 cVec.push_back(newCourse);
kkizirian 0:6ced83c58574 47 return 1;
kkizirian 0:6ced83c58574 48 }
kkizirian 0:6ced83c58574 49
kkizirian 0:6ced83c58574 50 for (int i = 0; i < cVec.size(); i++) {
kkizirian 0:6ced83c58574 51 if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt())
kkizirian 0:6ced83c58574 52 continue;
kkizirian 0:6ced83c58574 53 else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) {
kkizirian 0:6ced83c58574 54 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 55 return 1;
kkizirian 0:6ced83c58574 56 }
kkizirian 0:6ced83c58574 57 else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) {
kkizirian 0:6ced83c58574 58 if (cVec[i].getHour() < newCourse.getHour())
kkizirian 0:6ced83c58574 59 continue;
kkizirian 0:6ced83c58574 60 else if (newCourse.getHour() < cVec[i].getHour()) {
kkizirian 0:6ced83c58574 61 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 62 return 1;
kkizirian 0:6ced83c58574 63 }
kkizirian 0:6ced83c58574 64 else if (cVec[i].getHour() == newCourse.getHour()) {
kkizirian 0:6ced83c58574 65 if (cVec[i].getMinute() < newCourse.getMinute())
kkizirian 0:6ced83c58574 66 continue;
kkizirian 0:6ced83c58574 67 else if (newCourse.getMinute() < cVec[i].getMinute()) {
kkizirian 0:6ced83c58574 68 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 69 return 1;
kkizirian 0:6ced83c58574 70 }
kkizirian 0:6ced83c58574 71 else if (cVec[i].getMinute() == newCourse.getMinute())
kkizirian 0:6ced83c58574 72 return 0;
kkizirian 0:6ced83c58574 73 }
kkizirian 0:6ced83c58574 74 }
kkizirian 0:6ced83c58574 75 }
kkizirian 3:4984f0a32f32 76
kkizirian 3:4984f0a32f32 77 if (numIterations == cVec.size()) {
kkizirian 3:4984f0a32f32 78 cVec.push_back(newCourse);
kkizirian 3:4984f0a32f32 79 return 1;
kkizirian 3:4984f0a32f32 80 }
kkizirian 3:4984f0a32f32 81
kkizirian 0:6ced83c58574 82 return 0;
kkizirian 0:6ced83c58574 83 }