summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Course.cpp121
-rw-r--r--Course.h59
-rw-r--r--Makefile9
-rw-r--r--MatrixMultiply.cpp25
-rw-r--r--MatrixMultiply.h8
-rw-r--r--Q1.cpp46
-rw-r--r--Q2.cpp49
-rw-r--r--Q3.cpp182
-rw-r--r--Student.h21
-rw-r--r--Teacher.cpp88
-rw-r--r--Teacher.h56
12 files changed, 670 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..97220bb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+
+*.un~
+*.*~
+Q1
+Q2
+Q3
diff --git a/Course.cpp b/Course.cpp
new file mode 100644
index 0000000..1d4272a
--- /dev/null
+++ b/Course.cpp
@@ -0,0 +1,121 @@
+#include "Course.h"
+
+Course::Course(void)
+{
+ capacity = DEFAULT_CAPACITY;
+ enrollmentNumber = 0;
+ setName(("default class"));
+ teacher = NULL;
+ students = new Student*[ capacity ];
+ for (int i = 0; i < capacity; i++)
+ students[i] = NULL;
+}
+Course::Course(string n, int c)
+{
+ setName(n);
+ capacity = c;
+ enrollmentNumber = 0;
+ teacher = NULL;
+ students = new Student*[ capacity ];
+ for (int i = 0; i < capacity; i++)
+ students[i] = NULL;
+}
+
+Course::~Course(void)
+{
+ removeTeacher();
+ delete students;
+}
+
+void Course::addTeacher(Teacher *t)
+{
+ if (teacher && teacher != t) {
+ cout << name << " already has a teacher!" << endl;
+ } else if (t && t->getMyCourse() && t->getMyCourse() != this) {
+ cout << t->getName() << " already has a course" << endl;
+ } else if ((t && t->getMyCourse() == this) || teacher == t) {
+ cout << t->getName() << " aleady teaches " << name << endl;
+ } else {
+ teacher = t;
+ t->myCourse = this;
+ t->numStudents = enrollmentNumber;
+ }
+}
+
+void Course::removeTeacher(void)
+{
+ teacher->myCourse = NULL;
+ teacher->numStudents = 0;
+ for (int i = 0; i < Teacher::MAX_FAVORITES; i++) {
+ teacher->myFavorites[i] = NULL;
+ }
+ teacher = NULL;
+}
+
+void Course::addStudent(Student *s)
+{
+ int pos = -1;
+ if (s) {
+ for (int i = 0; i < capacity; i++) {
+ if (students[i] == s) {
+ cout << s->getName() << " already enrolled in " << name << endl;
+ return;
+ } else if (!students[i] && pos < 0)
+ pos = i;
+ }
+ if (pos >= 0) {
+ students[pos] = s;
+ enrollmentNumber++;
+ teacher->numStudents++;
+ } else
+ cout << name << " is full" << endl;
+ }
+}
+
+void Course::removeStudent(Student *s)
+{
+ bool did = false;
+ if (s) {
+ for (int i = 0; i < capacity; i++) {
+ if (students[i] == s) {
+ students[i] = NULL;
+ enrollmentNumber--;
+ teacher->numStudents--;
+ for (int j = 0; j < Teacher::MAX_FAVORITES; j++)
+ if (teacher->myFavorites[j] == s)
+ teacher->myFavorites[j] = NULL;
+ did = true;
+ }
+ }
+ if (!did)
+ cout << s->getName() << " is not in this class" << endl;
+ } else
+ cout << "invalid student" << endl;
+}
+
+void Course::setName(string n) { name = n; }
+
+Teacher *Course::getTeacher(void) { return teacher; }
+
+int Course::getCapacity(void) { return capacity; }
+
+int Course::getEnrollmentNumber(void) { return enrollmentNumber; }
+
+string Course::getName(void) { return name; }
+
+void Course::printCourse(void)
+{
+ cout << "Course " << name << " has:" << endl;
+ cout << "A capacity of " << capacity << endl;
+ cout << "an enrollment of " << enrollmentNumber << endl;
+ if (teacher)
+ cout << "a teacher named " << teacher->getName() << endl;
+ else
+ cout << "no teacher" << endl;
+ cout << "and students named:";
+ for (int i = 0; i < capacity; i++) {
+ if (students[i])
+ cout << " " << students[i]->getName() << ",";
+ }
+ cout << endl << endl;
+}
diff --git a/Course.h b/Course.h
new file mode 100644
index 0000000..fa557d1
--- /dev/null
+++ b/Course.h
@@ -0,0 +1,59 @@
+#ifndef COURSE_H
+#define COURSE_H
+
+#include <string>
+using std::string;
+
+#include "Student.h"
+#include "Teacher.h"
+
+class Teacher; // "Forward declaration" required for compilation.
+
+/**
+ * Course class.
+ */
+class Course
+{
+public:
+ /**
+ * Default constructor (aka no-argument constructor).
+ */
+ Course(void);
+
+ /**
+ * This version of the constructor takes the course name and capacity.
+ */
+ Course(string n, int c);
+
+ /**
+ * Destructor.
+ */
+ ~Course(void);
+
+ void addTeacher(Teacher *t);
+ void removeTeacher(void);
+ void addStudent(Student *s);
+ void removeStudent(Student *s);
+ void setName(string n);
+
+ Teacher *getTeacher(void);
+ int getCapacity(void);
+ int getEnrollmentNumber(void);
+ string getName(void);
+
+ void printCourse(void);
+
+ friend class Teacher;
+
+private:
+ Teacher *teacher;
+ static const int DEFAULT_CAPACITY = 100;
+ int capacity;
+ int enrollmentNumber;
+ Student **students;
+ string name;
+
+}; // class Course;
+
+#endif // COURSE_H
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b240b26
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+
+Q1: MatrixMultiply.h MatrixMultiply.cpp Q1.cpp
+ g++ -o Q1 MatrixMultiply.cpp Q1.cpp
+
+Q2: Q2.cpp
+ g++ -o Q2 Q2.cpp
+
+Q3: Q3.cpp Teacher.h Teacher.cpp Course.h Course.cpp Student.h
+ g++ -o Q3 Teacher.cpp Course.cpp Q3.cpp
diff --git a/MatrixMultiply.cpp b/MatrixMultiply.cpp
new file mode 100644
index 0000000..520f930
--- /dev/null
+++ b/MatrixMultiply.cpp
@@ -0,0 +1,25 @@
+
+#include "MatrixMultiply.h"
+
+double** matrixmultiply(double *m1[], double *m2[])
+{
+ const int inner = 3;
+ const int o1 = 3;
+ const int o2 = 2;
+ int i;
+
+ double **result = new double*[ o1 ];
+ for (int i = 0; i < 3; i++) {
+ result[i] = new double[o2];
+ }
+
+ for (i = 0; i < o1; i++) {
+ for (int j = 0; j < o2; j++) {
+ result[i][j] = 0;
+ for(int k = 0; k < inner; k++) {
+ result[i][j] += m1[i][k]*m2[k][j];
+ }
+ }
+ }
+ return result;
+}
diff --git a/MatrixMultiply.h b/MatrixMultiply.h
new file mode 100644
index 0000000..93a039c
--- /dev/null
+++ b/MatrixMultiply.h
@@ -0,0 +1,8 @@
+
+#ifndef MATRIXMULTIPLY_H
+
+#define MATRIXMULTIPLY_H
+
+double** matrixmultiply(double *m1[], double *m2[]);
+
+#endif /* end of include guard: MATRIXMULTIPLY_H */
diff --git a/Q1.cpp b/Q1.cpp
new file mode 100644
index 0000000..2a2b03a
--- /dev/null
+++ b/Q1.cpp
@@ -0,0 +1,46 @@
+
+#include <iostream>
+
+#include "MatrixMultiply.h"
+
+using namespace std;
+int main(int argc, const char *argv[])
+{
+ int i,j;
+ double **m1 = new double*[3];
+ double **m2 = new double*[3];
+ for (i = 0; i < 3; i++) {
+ m1[i] = new double[3];
+ m2[i] = new double[2];
+ }
+ double **res;
+
+ cout << "Enter m1 as a 3x3 matrix row by row:" << endl;
+ for (i = 0; i < 3; i++) {
+ cout << "row" << i+1 << ": ";
+ for (int j = 0; j < 3; j++)
+ cin >> m1[i][j];
+ }
+ cout << "Enter m2 as a 3x2 matrix row by row:" << endl;
+ for (i = 0; i < 3; i++) {
+ cout << "row" << i+1 << ": ";
+ for (int j = 0; j < 2; j++)
+ cin >> m2[i][j];
+ }
+
+ res = matrixmultiply(m1,m2);
+
+ cout << "Result or m1*m2:" << endl;
+
+ cout << "["<< endl;
+ for (i = 0; i < 3; i++) {
+ cout << " [";
+ for (j = 0; j < 2; j++) {
+ cout << " " << res[i][j] << " ";
+ }
+ cout << "]" << endl;
+ }
+ cout << "]" << endl;
+
+ return 0;
+}
diff --git a/Q2.cpp b/Q2.cpp
new file mode 100644
index 0000000..956c343
--- /dev/null
+++ b/Q2.cpp
@@ -0,0 +1,49 @@
+
+#include <iostream>
+
+char *largestCommonSubstr(const char s1[], const char s2[]);
+
+using namespace std;
+int main(int argc, const char *argv[])
+{
+
+ const char *s1[] = {"Boston", "James", "computer" , "abc", "software"};
+ const char *s2[] = {"Killington", "Thames", "Counter", "abba", "MICROSOFT"};
+
+ for (int i = 0; i < 5; i++) {
+ char *substr = largestCommonSubstr(s1[i], s2[i]);
+ if (substr)
+ cout << "longest common substring of " << s1[i] << " and " << s2[i] << " is " << substr << endl;
+ else
+ cout << "no common substrings between " << s1[i] << " and " << s2[i] << endl;
+ }
+
+ return 0;
+}
+
+char *largestCommonSubstr(const char s1[], const char s2[])
+{
+ int len = 0;
+ int longest = 0;
+ int longestpos;
+ int pos1,pos2,ofst;
+
+ for (pos1 = 0; s1[pos1]; pos1++) {
+ for (pos2 = 0; s2[pos2]; pos2++) {
+ for (ofst = 0,len=0; s1[pos1+ofst] == s2[pos2+ofst++]; len++) {}
+ if (len > longest) {
+ longest = len;
+ longestpos = pos1;
+ }
+ }
+ }
+
+ char *result = longest != 0 ? new char(longest+1) : NULL;
+ if (result) {
+ for (int i = 0; i < longest; i++) {
+ result[i] = s1[longestpos+i];
+ }
+ result[longest] = '\0';
+ }
+ return result;
+}
diff --git a/Q3.cpp b/Q3.cpp
new file mode 100644
index 0000000..75e4213
--- /dev/null
+++ b/Q3.cpp
@@ -0,0 +1,182 @@
+
+#include <iostream>
+
+#include "Student.h"
+#include "Teacher.h"
+#include "Course.h"
+
+using namespace std;
+
+int main(int argc, const char *argv[])
+{
+ Student **students = new Student*[100];
+ const char *letters = "0123456789";
+
+ // make 100 students with rather generic names
+ for (int i = 0; i < 10; i++) {
+ for (int j = 0; j < 10; j++) {
+ char *name = new char[3];
+ name[0] = letters[i];
+ name[1] = letters[j];
+ name[2] = '\0';
+ students[10*i+j] = new Student(name);
+ }
+ }
+
+ // make a few courses
+ cout << "making courses" << endl;
+ Course *c1 = new Course(); // default
+ Course *c2 = new Course("english", 50);
+ Course *c3 = new Course("math", 10);
+
+ // make a few teachers
+ cout << "making teachers" << endl;
+ Teacher *t1 = new Teacher();
+ Teacher *t2 = new Teacher("john");
+ Teacher *t3 = new Teacher("steve");
+
+ cout << "teachers:" << endl;
+ t1->printTeacher();
+ t2->printTeacher();
+ t3->printTeacher();
+
+ cout << endl;
+ // mutator
+ t1->setName("barbara");
+ cout << "changed t1:" << endl;
+ t1->printTeacher();
+
+ cout << "courses:" << endl;
+ c1->printCourse();
+ c2->printCourse();
+ c3->printCourse();
+
+ // test mutator
+ c1->setName("comp sci");
+ cout << "changed c1" << endl;
+ c1->printCourse();
+
+ cout << endl;
+
+ cout << "assigning teachers to classes" << endl;
+ c1->addTeacher(t1);
+ t2->setMyCourse(c2);
+ c3->addTeacher(t3);
+
+ // reprint
+ cout << "classes: " << endl;
+ c1->printCourse();
+ c2->printCourse();
+ c3->printCourse();
+
+ // reprint
+ cout << "teachers: " << endl;
+ t1->printTeacher();
+ t2->printTeacher();
+ t3->printTeacher();
+
+
+ // overassign
+ cout << "test assigning more than 1 teacher to a class" << endl;
+ t3->setMyCourse(c1);
+ c3->addTeacher(t1);
+
+ cout << endl;
+
+ cout << "adding students to classes" << endl;
+ // c1
+ cout << "c1" << endl;
+ for (int i = 0; i < 100; i++) {
+ c1->addStudent(students[i]);
+ }
+
+ // c2, but overassigned
+ cout << "c2, overassigned" << endl;
+ for (int i = 0; i < 52; i++) {
+ c2->addStudent(students[i]);
+ }
+
+ // c3
+ cout << "c3" << endl;
+ for (int i = 0; i < 10; i++) {
+ c3->addStudent(students[i]);
+ }
+ // double assign
+ cout << "assign same student to same class" << endl;
+ c3->addStudent(students[0]);
+ c3->addStudent(students[5]);
+
+ // reprint
+ cout << "courses: " << endl;
+ c1->printCourse(); // too long
+ c2->printCourse();
+ c3->printCourse();
+
+ // deleting
+ cout << "test deleting students" << endl;
+ c3->removeStudent(students[3]);
+ c3->removeStudent(students[8]);
+ c3->removeStudent(students[11]);
+
+ // adding new
+ cout << "and re-adding them" << endl;
+ c3->addStudent(students[39]);
+ c3->addStudent(students[33]);
+
+ // favorites
+ cout << endl;
+ cout << "test setting favorites" << endl;
+
+ cout << "t1" << endl;
+ for (int i = 0; i < 7; i++) {
+ t1->addToMyFavorites(students[i]);
+ }
+
+ cout << "t2" << endl;
+ for (int i = 0; i < 10; i++) {
+ t2->addToMyFavorites(students[i]);
+ }
+ // too many
+ cout << "too many favorites" << endl;
+ t2->addToMyFavorites(students[13]);
+ t2->addToMyFavorites(students[32]);
+
+ cout << "t3" << endl;
+ for (int i = 0; i < 8; i++) {
+ t3->addToMyFavorites(students[i]);
+ }
+ // not in class
+ cout << "add favorites not in clas" << endl;
+ t3->addToMyFavorites(students[31]);
+ t3->addToMyFavorites(students[92]);
+
+ // reprint
+ cout << "teachers: " << endl;
+ t1->printTeacher();
+ t2->printTeacher();
+ t3->printTeacher();
+
+ // remove a favorite
+ cout << "test removing a favorite student from a class" << endl;
+ cout << "(should also remove it from a teacher's favorites)" << endl;
+ c2->removeStudent(students[6]);
+ t2->printTeacher();
+
+ cout << endl;
+
+ // removing teachers
+ cout << "test removing teachers" << endl;
+ c2->removeTeacher();
+ c2->printCourse();
+ t2->printTeacher();
+
+ // replace it
+ cout << "replace removed with a different one" << endl;
+ Teacher *t4 = new Teacher("greg");
+ c2->addTeacher(t4);
+ c2->printCourse();
+ t4->printTeacher();
+
+
+ return 0;
+}
diff --git a/Student.h b/Student.h
new file mode 100644
index 0000000..019651b
--- /dev/null
+++ b/Student.h
@@ -0,0 +1,21 @@
+#ifndef STUDENT_H
+#define STUDENT_H
+
+#include <string>
+using std::string;
+
+class Student
+{
+public:
+ Student(void) { name = "default student"; }
+ Student(string n) { name = n; }
+ ~Student(void) { }
+
+ string getName(void) { return name; }
+ void setName(string n) { name = n; }
+
+private:
+ string name;
+}; // class Student
+
+#endif // STUDENT_H
diff --git a/Teacher.cpp b/Teacher.cpp
new file mode 100644
index 0000000..ab08119
--- /dev/null
+++ b/Teacher.cpp
@@ -0,0 +1,88 @@
+
+#include "Teacher.h"
+
+Teacher::Teacher(void)
+{
+ numStudents = 0;
+ setName(("default teacher"));
+ myFavorites = new Student*[ MAX_FAVORITES ];
+ for (int i = 0; i < MAX_FAVORITES; i++) {
+ myFavorites[i] = NULL;
+ }
+}
+
+Teacher::Teacher(string teacher_name)
+{
+ numStudents = 0;
+ setName(teacher_name);
+ myCourse = NULL;
+ myFavorites = new Student*[ MAX_FAVORITES ];
+ for (int i = 0; i < MAX_FAVORITES; i++)
+ myFavorites[i] = NULL;
+}
+
+Teacher::~Teacher(void)
+{
+ myCourse->removeTeacher();
+ delete myFavorites;
+}
+
+void Teacher::addToMyFavorites(Student *fav)
+{
+ bool inclass = false;
+ for (int i = 0; i < myCourse->getCapacity(); i++) {
+ if (myCourse->students[i] == fav)
+ inclass = true;
+ }
+ if (inclass) {
+ for (int i = 0; i < Teacher::MAX_FAVORITES; i++) {
+ if (!myFavorites[i]) {
+ myFavorites[i] = fav;
+ return;
+ } else if (myFavorites[i] == fav)
+ cout << fav->getName() << " is already a favorite of " << name << endl;
+ }
+ cout << name << " already has too many favorites" << endl;
+ } else {
+ cout << fav->getName() << " is not in " << name << "'s class" << endl;
+ }
+}
+
+void Teacher::setMyCourse(Course *c)
+{
+ if (myCourse && myCourse != c) {
+ cout << name << " already has a course!" << endl;
+ } else if (c && c->getTeacher() && c->getTeacher() != this) {
+ cout << c->getName() << " already has a teacher!" << endl;
+ } else if ((c && c->getTeacher() == this) || myCourse == c) {
+ cout << name << " already teaches " << c->getName() << endl;
+ } else {
+ myCourse = c;
+ c->teacher = this;
+ numStudents = c->enrollmentNumber;
+ }
+}
+
+void Teacher::setName(string n) { name = n; }
+
+int Teacher::getNumStudents(void) { return numStudents; }
+
+Course *Teacher::getMyCourse(void) { return myCourse; }
+
+string Teacher::getName(void) { return name; }
+
+void Teacher::printTeacher(void)
+{
+ cout << "Teacher " << name << " has:" << endl;
+ cout << getNumStudents() << " students" << endl;
+ if (myCourse)
+ cout << "teaches a course named " << myCourse->getName() << endl;
+ else
+ cout << "does not teach a course" << endl;
+ cout << "and has favorite students named:";
+ for (int i = 0; i < Teacher::MAX_FAVORITES; i++) {
+ if (myFavorites[i])
+ cout << " " << myFavorites[i]->getName() << ",";
+ }
+ cout << endl << endl;
+}
diff --git a/Teacher.h b/Teacher.h
new file mode 100644
index 0000000..a518489
--- /dev/null
+++ b/Teacher.h
@@ -0,0 +1,56 @@
+#ifndef TEACHER_H
+#define TEACHER_H
+
+#include <string>
+#include <iostream>
+using std::string;
+using namespace std;
+
+#include "Course.h"
+#include "Student.h"
+
+class Course; // "Forward declaration" required for compilation.
+
+/**
+ * Teacher class.
+ */
+class Teacher
+{
+public:
+
+ /**
+ * Default constructor (aka no-argument constructor).
+ */
+ Teacher(void);
+
+ /**
+ * This constructor takes the teacher's name as an argument.
+ */
+ Teacher(string teacher_name);
+
+ /**
+ * Destructor.
+ */
+ ~Teacher(void);
+
+ void addToMyFavorites(Student *fav);
+ void setMyCourse(Course *c);
+ void setName(string n);
+
+ int getNumStudents(void);
+ Course *getMyCourse(void);
+ string getName(void);
+
+ void printTeacher(void);
+
+ friend class Course;
+
+private:
+ int numStudents;
+ string name;
+ Course *myCourse;
+ static const int MAX_FAVORITES = 10;
+ Student **myFavorites;
+}; // class Teacher
+
+#endif // TEACHER_H