uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit b093e1929907bd74dd59b325c1b9e11c4a7ab4d8
parent 8d37d837a1cadc785283f15bd1933404beb44015
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 31 Mar 2020 22:25:57 +0300

new assignment

Diffstat:
Massignment-1.1-basic-elements/C1a-CORRECTED.c | 0
Massignment-1.1-basic-elements/Cube-Sphere.c | 0
Massignment-1.1-basic-elements/IO-Exercise.c | 0
Massignment-1.1-basic-elements/examples/C1a.c | 0
Massignment-1.2-conditional-statements/examples/C2a.c | 0
Massignment-1.2-conditional-statements/examples/C2b.c | 0
Massignment-1.2-conditional-statements/examples/C2c.c | 0
Massignment-1.2-conditional-statements/int-comparison.c | 0
Massignment-1.2-conditional-statements/quadratic-equation.c | 0
Massignment-1.3-loops/int-calcs.c | 0
Massignment-1.3-loops/shapes.c | 0
Massignment-1.3-loops/sine-taylor.c | 0
Massignment-1.4-functions/menu/src/main.c | 0
Massignment-1.4-functions/sine-cos-taylor/src/main.c | 0
Massignment-1.5-arrays-pointers-files/combinations/bin/combs | 0
Massignment-1.5-arrays-pointers-files/fcombinations/bin/fcombs | 0
Massignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs | 0
Massignment-1.5-arrays-pointers-files/minecurses/bin/minesweeper | 0
Massignment-2.1-fromctocpp/Ex1.cpp | 0
Aassignment-2.3-operoverloading/BigIntv3.cpp | 444+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-2.3-operoverloading/Makefile | 40++++++++++++++++++++++++++++++++++++++++
Aassignment-2.3-operoverloading/bin/operoverloading | 0
Aassignment-2.3-operoverloading/obj/main.o | 0
Aassignment-2.3-operoverloading/obj/student.o | 0
Aassignment-2.3-operoverloading/obj/subject.o | 0
Aassignment-2.3-operoverloading/src/main.cpp | 216+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-2.3-operoverloading/src/student.cpp | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-2.3-operoverloading/src/student.h | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-2.3-operoverloading/src/subject.cpp | 4++++
Aassignment-2.3-operoverloading/src/subject.h | 21+++++++++++++++++++++
30 files changed, 855 insertions(+), 0 deletions(-)

diff --git a/assignment-1.1-basic-elements/C1a-CORRECTED.c b/assignment-1.1-basic-elements/C1a-CORRECTED.c diff --git a/assignment-1.1-basic-elements/Cube-Sphere.c b/assignment-1.1-basic-elements/Cube-Sphere.c diff --git a/assignment-1.1-basic-elements/IO-Exercise.c b/assignment-1.1-basic-elements/IO-Exercise.c diff --git a/assignment-1.1-basic-elements/examples/C1a.c b/assignment-1.1-basic-elements/examples/C1a.c diff --git a/assignment-1.2-conditional-statements/examples/C2a.c b/assignment-1.2-conditional-statements/examples/C2a.c diff --git a/assignment-1.2-conditional-statements/examples/C2b.c b/assignment-1.2-conditional-statements/examples/C2b.c diff --git a/assignment-1.2-conditional-statements/examples/C2c.c b/assignment-1.2-conditional-statements/examples/C2c.c diff --git a/assignment-1.2-conditional-statements/int-comparison.c b/assignment-1.2-conditional-statements/int-comparison.c diff --git a/assignment-1.2-conditional-statements/quadratic-equation.c b/assignment-1.2-conditional-statements/quadratic-equation.c diff --git a/assignment-1.3-loops/int-calcs.c b/assignment-1.3-loops/int-calcs.c diff --git a/assignment-1.3-loops/shapes.c b/assignment-1.3-loops/shapes.c diff --git a/assignment-1.3-loops/sine-taylor.c b/assignment-1.3-loops/sine-taylor.c diff --git a/assignment-1.4-functions/menu/src/main.c b/assignment-1.4-functions/menu/src/main.c diff --git a/assignment-1.4-functions/sine-cos-taylor/src/main.c b/assignment-1.4-functions/sine-cos-taylor/src/main.c diff --git a/assignment-1.5-arrays-pointers-files/combinations/bin/combs b/assignment-1.5-arrays-pointers-files/combinations/bin/combs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/fcombinations/bin/fcombs b/assignment-1.5-arrays-pointers-files/fcombinations/bin/fcombs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs b/assignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/bin/minesweeper b/assignment-1.5-arrays-pointers-files/minecurses/bin/minesweeper Binary files differ. diff --git a/assignment-2.1-fromctocpp/Ex1.cpp b/assignment-2.1-fromctocpp/Ex1.cpp diff --git a/assignment-2.3-operoverloading/BigIntv3.cpp b/assignment-2.3-operoverloading/BigIntv3.cpp @@ -0,0 +1,444 @@ +#include <stdio.h> +#include <string.h> +#include <string> +#include <new> +#include <iostream> + +using namespace std; + +class BigInt +{ +public: + static int MaxSize; + static char *MaxNumb; + static void PrintMax (); +private: + int size; + char *numb; +public: + BigInt (); + BigInt (const char *); + BigInt (int); + BigInt (const BigInt &); + ~BigInt (); + void Print () const; + void operator+= (const BigInt &); + void operator+= (int ); + BigInt operator+ (BigInt &); + BigInt operator+ (const char *); + BigInt operator+ (const int); + BigInt operator* (const BigInt &); + BigInt operator= (const BigInt &); + BigInt operator++ (); + BigInt operator++ (int); + BigInt operator-- (); + BigInt operator-- (int); + int operator== (const BigInt &); + int operator!= (const BigInt &); + +}; + +class LibExc +{ +public: + int ErrCode; + string ErrMess; + LibExc (int C, string M) + { + ErrCode = C; + ErrMess = M; + } +}; + +int BigInt::MaxSize = 0; + +char *BigInt::MaxNumb = NULL; + +void BigInt::PrintMax () +{ + int i; + if (MaxNumb) + { + printf ("*"); + for ( i = MaxSize - 1; i >= 0; i--) + printf ("%c", MaxNumb[i] + 48); + printf ("*\n"); + } +} + +BigInt::BigInt () +{ + size = 1; + numb = new (nothrow) char [1]; + if (numb == NULL) + throw (1); + numb[0] = 0; +} + +BigInt::BigInt (const char *s) +{ + //printf("constructing string %s \n",s); + int i; + int l = strlen(s); + for (i = 0; i <l; i++) + { + if (s[i] < '0' || s[i] > '9') + throw (2); + } + if (l) + { + numb = new (nothrow) char [l]; + if (!numb) + throw (1); + for(i = 0; i < l; i++) + numb[i] = s[l - 1 - i] - 48; + size = l; + } + if (l > BigInt::MaxSize) + { + MaxSize = l; + MaxNumb = numb; + } +} + +BigInt::BigInt (int nu) +{ + //printf("constructing int %d\n",nu); + char *s; + s = new char [12]; + sprintf (s, "%d", nu); + int i; + int l = strlen(s); + if (l) + { + numb = new (nothrow) char [l]; + if (numb == NULL) + throw (1); + for(i = 0; i < l; i++) + numb[i] = s[l - 1 - i] - 48; + size = l; + } + delete[] s; + if (l > BigInt::MaxSize) + { + MaxSize = l; + MaxNumb = numb; + } +} + +BigInt::BigInt (const BigInt &x) //Σβήστε το const και δείτε τι συμβαίνει!!! +{ + //printf("Copy constructing\n"); + size = x.size; + numb = new (nothrow) char [size]; + if (numb == NULL) + throw (1); + memcpy (numb, x.numb, size); +} + +BigInt::~BigInt () +{ + if (numb == MaxNumb) + MaxNumb = NULL; + delete [] numb; +} + +void BigInt::Print () const +{ + int i; + printf ("*"); + for ( i = size - 1; i >= 0; i--) + printf ("%c", numb[i] + 48); + printf ("*\n"); +} + +void BigInt::operator+= (const BigInt &x) +{ + int S1, S2, m, Kr = 0; + int i; + + for(i = 0; i < size; i++) + { + numb[i] += x.numb[i] + Kr; + if (numb[i] > 10) + { + numb[i] -= 10; + Kr = 1; + } + else + Kr = 0; + } +} + +void BigInt::operator+= (int i) +{ + printf ("I will not add %d\n", i); +} + +BigInt BigInt::operator+ (BigInt &x) +{ + int i, j, s, Kr = 0, r, D1, D2, D; + s = (size > x.size) ? size : x.size; + char buf[s + 1]; + char buf1[s + 2]; + for (i = 0; i < s; i++) + { + D1 = (i < size) ? numb[i] : 0; + D2 = (i < x.size) ? x.numb[i] : 0; + D = D1 + D2 + Kr; + Kr = 0; + if (D > 9) + { + D -= 10; + Kr = 1; + } + buf[i] = D; + } + if (Kr) + buf[i] = Kr; + else + i--; + for (j = 0; i >= 0; j++, i--) + buf1[j] = buf[i] + 48; + buf1[j] = '\0'; + BigInt Q(buf1); + return Q; +} + +BigInt BigInt::operator+ (const char *s) +{ + BigInt Tmp(s); + return *this + Tmp; +} + +BigInt BigInt::operator+ (int s) +{ + BigInt Tmp(s); + return *this + Tmp; +} + +BigInt BigInt::operator= (const BigInt &x) //Σβήστε το const και δείτε τι συμβαίνει +{ + delete[] numb; + size = x.size; + numb = new (nothrow) char [size]; + if (numb == NULL) + throw (1); + memcpy (numb, x.numb, size); + return *this; +} + +BigInt BigInt::operator++() +{ + int i; + numb[0]++; + for(i = 0; i < size - 1; i++) + if (numb[i] == 10) + { + numb[i] = 0; + numb[i + 1]++; + } + if (numb[size - 1] == 10) + { + numb[size - 1] = 0; + char *buf1 = new (nothrow) char[++size]; + if (numb == NULL) + throw (1); + memcpy (buf1, numb, size - 1); + buf1[size - 1] = 1; + delete [] numb; + numb = buf1; + } + return *this; +} + +BigInt BigInt::operator++ (int ) +{ + int i; + BigInt tmp = *this; + numb[0]++; + for(i = 0; i < size - 1; i++) + if (numb[i] == 10) + { + numb[i] = 0; + numb[i + 1]++; + } + if (numb[size - 1] == 10) + { + numb[size - 1] = 0; + char *buf1 = new (nothrow) char[++size]; + if (numb == NULL) + throw (1); + memcpy (buf1, numb, size - 1); + buf1[size - 1] = 1; + delete [] numb; + numb = buf1; + } + return tmp; +} + +BigInt BigInt::operator* (const BigInt &x) +{ + BigInt res = "0"; + BigInt t = x; + BigInt z = 0; + while (t != z) + { + res = res + *this; +// printf("----"); +// res.Print(); + --t; + } + return res; +} + + +BigInt BigInt::operator-- () +{ + int i; + if ((numb[0] == 0) && (size == 1)) + return *this; + numb[0]--; + for(i = 0 ; i < size; i++) + { + if (numb[i] == -1) + { + numb[i] = 9; + numb[i + 1]--; + } + } + if (numb[size - 1] == 0 && size > 1) + { + char *buf1 = new char[--size]; + memcpy (buf1, numb, size); + delete [] numb; + numb = buf1; + } + + return *this; +} + +BigInt BigInt::operator--(int) +{ + BigInt tmp = *this; + --*this; + return tmp; +} + +int BigInt::operator== (const BigInt &x) +{ + if (size != x.size) + return 0; + for(int i = 0; i < size; i++) + if (numb[i] != x.numb[i]) + return 0; + return 1; +} + +int BigInt::operator != (const BigInt &x) +{ + if (*this == x) + return 0; + return 1; +} + +BigInt BigProduct (BigInt Num) +{ + Num++; + BigInt I = 1; + BigInt P = 1; + while (I != Num) + { + P = P * I; + I = I + 1; + } + return P; +} + +BigInt Power (char *B, int E) +{ + if (E < 0) + { + string m("Invalid Expmponent"); + LibExc e(2, m); + throw e; + } + try + { + BigInt Ex(E); + BigInt Ba(B); + BigInt Res = 1; + BigInt I = 0; + while (I != Ex) + { + Res = Res * Ba; + I++; + //Res.Print (); + //cout << endl; + } + return Res; + } + catch (int e) + { + if (e == 1) + throw; + if (e == 2) + throw LibExc (1, *(new string ("Invalid Base:"))); + } +} + +main () +{ + int i; + char N[100]; + bool cont; + do + { + cont = false; + try + { + cout << "Enter Number to find Product:"; + cin >> N; + BigInt x; + BigInt k(N); + x = BigProduct (k); + x.Print(); + } + catch (int e) + { + if (e == 1) + { + cout << "Memory Error: " << BigInt::MaxNumb; + cout << BigInt::MaxSize << " "; BigInt::PrintMax (); + } + if (e == 2) + { + cout << "Initialization Error" << endl; + cont = true; + } + } + } + while (cont); + cout << "========================================================================" << endl; + try + { + char B[100]; + int E; + cout << "Base: "; + cin >> B; + cout << "Exponent: "; + cin >> E; + BigInt a = Power (B, E); + a.Print (); + } + catch (int e) + { + cout << "Memory Error: " << BigInt::MaxNumb; + cout << BigInt::MaxSize << " "; BigInt::PrintMax (); + } + catch (LibExc e) + { + cout << "Parameter Error" << endl; + cout << e.ErrCode << " " << e.ErrMess << endl; + } +} diff --git a/assignment-2.3-operoverloading/Makefile b/assignment-2.3-operoverloading/Makefile @@ -0,0 +1,40 @@ +TARGET = operoverloading +#INSTALL_PATH = /usr/local/bin + +SRC_DIR = src +OBJ_DIR = obj +BIN_DIR = bin + +SRC = $(wildcard $(SRC_DIR)/*.cpp) +OBJ = $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) + +MOVE = mv +MKDIR_P = mkdir -p + +CC = g++ +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +#LDLIBS += -lm + +.PHONY: all clean + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + $(MOVE) $(TARGET) $(BIN_DIR) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp + $(MKDIR_P) $(OBJ_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(BIN_DIR)/$(TARGET) + +install: $(TARGET) + cp $(BIN_DIR)/$(TARGET) $(INSTALL_PATH) + +clean: + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET) diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/subject.o b/assignment-2.3-operoverloading/obj/subject.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -0,0 +1,216 @@ +#include "student.h" +#include "subject.h" + +inline std::ostream& operator<< (std::ostream& stream, const Student& s) +{ + return stream << "AM: " << s.get_AM() << std::endl << "Name: " << s.get_name() << std::endl; +} + +void cont(); +void constructor1(const Student& s1); +void ostream_overload(const Student& s1); +void constructor2(const Student& s2); +void copy_constructor(const Student& copystud); +void conditional_overload(const Student& s1, const Student& s2, const Student& copystud); +void constructor3(const Student& s3); +void detprint (const Student& s3); +void setters(Student& s3); +void getters(const Subject& s3); +void setters(Subject& s3); + + +int main(int argc, char **argv) +{ + Student *s1 = new Student("12345678", "Name Surname"); + system("clear"); + constructor1(*s1); cont(); + ostream_overload(*s1); cont(); + Student *s2 = new Student("12345678", "Name Surname", 2); + constructor2(*s2); cont(); + + Student *copystud = new Student(*s2); + copy_constructor(*copystud); cont(); + conditional_overload(*s1, *s2, *copystud); cont(); + delete s1; + delete copystud; + delete s2; + + float grd[4] = {9.4f, 8.4f, 5.5f, 6.3f}; + Student *s3 = new Student("12345678", "Name Surname", 2, 4, grd); + constructor3(*s3); cont(); + detprint(*s3); cont(); + setters(*s3); cont(); + delete s3; + + Subject *sb = new Subject("35643", "OOP", 2); + getters(*sb); cont(); + setters(*sb); + delete sb; + + return 0; +} + +void cont() +{ + std::cout << std::endl; + std::cout << "Press any key to continue"; + if (std::cin.get()) system("clear"); +} + +void constructor1(const Student& s1) +{ + std::cout << "Constructor for s1 (AM, Name)" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "s1.get_AM(): " << s1.get_AM() << std::endl; + std::cout << "s1.get_name(): " << s1.get_name() << std::endl; + std::cout << "s1.get_semester(): " << s1.get_semester() << std::endl; + std::cout << "s1.get_psubj(): " << s1.get_psubj() << std::endl << std::endl; +} + +void ostream_overload(const Student& s1) +{ + std::cout << "std::ostream overload" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << s1 << std::endl; +} + +void constructor2(const Student& s2) +{ + std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "s2.get_AM(): " << s2.get_AM() << std::endl; + std::cout << "s2.get_name(): " << s2.get_name() << std::endl; + std::cout << "s2.get_semester(): " << s2.get_semester() << std::endl; + std::cout << "s2.get_psubj(): " << s2.get_psubj() << std::endl << std::endl; +} + +void copy_constructor(const Student& copystud) +{ + std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "copystud.get_AM(): " << copystud.get_AM() << std::endl; + std::cout << "copystud.get_name(): " << copystud.get_name() << std::endl; + std::cout << "copystud.get_semester(): " << copystud.get_semester() << std::endl; + std::cout << "copystud.get_psubj(): " << copystud.get_psubj() << std::endl << std::endl; +} + +void conditional_overload(const Student& s1, const Student& s2, const Student& copystud) +{ + std::cout << "Conditional operator overloading" << std::endl; + std::cout << "----------------------------" << std::endl; + if (s2 == copystud) std::cout << "s2 == copystud" << std::endl; + if (s1 != s2) std::cout << "s1 != s2" << std::endl; + if (s1 < s2) std::cout << "s1 < s2" << std::endl; + if (s1 <= s2) std::cout << "s1 <= s2" << std::endl; + if (s2 > s1) std::cout << "s2 > s1" << std::endl; + if (s2 >= s1) std::cout << "s2 >= s1" << std::endl; + std::cout << std::endl; +} + +void constructor3(const Student& s3) +{ + std::cout << "Constructor for s3 (AM, Name, Semester, Subjects Passed, Grades)" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "s3.get_AM(): " << s3.get_AM() << std::endl; + std::cout << "s3.get_name(): " << s3.get_name() << std::endl; + std::cout << "s3.get_semester(): " << s3.get_semester() << std::endl; + std::cout << "s3.get_psubj(): " << s3.get_psubj() << std::endl; + + float *gr = s3.get_grades(); + std::cout << "s3.get_grades(): "; + for (int i = 0; i < s3.get_psubj(); i++) + { + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl << std::endl; + } +} + +void detprint (const Student& s3) +{ + std::cout << "Detailed print of s3's grades" << std::endl; + std::cout << "----------------------------" << std::endl; + s3.detailed_print(); +} + +void setters(Student& s3) +{ + s3.set_AM("01010101"); + s3.set_name("AAAAAAA"); + s3.set_semester(100); + s3.set_psubj(2); // πρώτα η set_psubj() και μετα η set_grades() !!!!! + float gg[2] = {0.1f, 2.2f}; + s3.set_grades(gg); + + std::string ss[3] = {"Math", "Physics", "Programming"}; + s3.set_num_submitted_subjects(3); + s3.set_submitted_subjects(ss); + + std::cout << "Setters example using s3" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: s3.set_AM(\"01010101\")" << '\t'; + std::cout << "Output: s3.get_AM(): " << s3.get_AM() << std::endl; + std::cout << "Input: s3.set_name(\"AAAAAAA\")" << '\t'; + std::cout << "Output: s3.get_name(): " << s3.get_name() << std::endl; + std::cout << "Input: s3.set_semester(100):" << '\t'; + std::cout << "Output: s3.get_semester(): " << s3.get_semester() << std::endl; + std::cout << "Input: s3.set_psubj(2):" << '\t'; + std::cout << "Output: s3.get_psubj(): " << s3.get_psubj() << std::endl; + + float *gr = s3.get_grades(); + std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; + std::cout << "Output: s3.get_grades(): "; + for (int i = 0; i < s3.get_psubj(); i++) + { + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + s3.add_grade(7.5f); + gr = s3.get_grades(); + std::cout << "Input: s3.add_grade(7.5f)" << '\t'; + std::cout << "Output: s3.get_grades(): "; + for (int i = 0; i < s3.get_psubj(); i++) + { + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + + std::cout << std::endl; + std::cout << "Input: s3.set_num_submitted_subjects(3)" << std::endl;; + std::cout << "Output: s3.get_num_submitted_subjects(): " << s3.get_num_submitted_subjects() << std::endl << std::endl; + + std::string *ssj = s3.get_submitted_subjects(); + std::cout << "Input: {\"Math\", \"Physics\", \"Programming\"}" << std::endl;; + std::cout << "Output: s3.get_submitted_subjects(): "; + for (int i = 0; i < s3.get_num_submitted_subjects(); i++) + { + if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i] << ", "; + else std::cout << ssj[i] << std::endl; + } +} + +void getters(const Subject& sb) +{ + std::cout << "Getters example using Subject object" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Constructor: Subject(\"35643\", \"OOP\", 2)" << std::endl; + std::cout << "sb.get_code(): " << sb.get_code() << std::endl; + std::cout << "sb.get_sname(): " << sb.get_sname() << std::endl; + std::cout << "sb.get_subjsemester(): " << sb.get_subjsemester() << std::endl; +} + +void setters(Subject& sb) +{ + sb.set_code("14556"); + sb.set_sname("Calculus I"); + sb.set_subjsemester(1); + + std::cout << "Setters example using Subject object" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: sb.set_code(\"14556\")" << '\t' << '\t'; + std::cout << "Output: sb.get_code(): " << sb.get_code() << std::endl; + std::cout << "Input: sb.set_sname(\"Calculus I\")" << '\t'; + std::cout << "Output: sb.get_sname(): " << sb.get_sname() << std::endl; + std::cout << "Input: sb.set_subjsemester(1):" << '\t' << '\t'; + std::cout << "Output: sb.get_subjsemester(): " << sb.get_subjsemester() << std::endl; + +} diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -0,0 +1,71 @@ +#include "student.h" + +Student::Student(const char *AM, const std::string& name) + :AM(convert_AM(AM)), name(name), semester(1), psubj(0) {} + +Student::Student(const char *AM, const std::string& name, unsigned int semester) + :AM(convert_AM(AM)), name(name), semester(semester), psubj(0) {} + +Student::Student(const char *AM, const std::string& name, unsigned int semester, + unsigned int psubj, const float *grades) + :AM(convert_AM(AM)), name(name), semester(semester), psubj(psubj), grades(convert_PSG(grades)) {} + +Student::Student(const Student& s) + :name(s.name), semester(s.semester), psubj(s.psubj) +{ + int sl = strlen(s.AM); + this->AM = new char[sl + 1]; + std::copy(s.AM, s.AM+sl, AM); + + this->grades = new float[psubj]; + std::copy(s.grades, s.grades+psubj, grades); +} + +Student::~Student() +{ + delete[] this->AM; + delete[] this->grades; +} + +char *Student::convert_AM(const char *AM) +{ + int len = strlen(AM); + this->AM = new char[len+1]; + std::copy(AM, AM+len, this->AM); + return this->AM; +} + +float *Student::convert_PSG(const float *grades) +{ + this->grades = new float[psubj]; + std::copy(grades, grades+psubj, this->grades); + return this->grades; +} + +void Student::add_grade(float grade) +{ + float *tmp = new float[psubj+1]; + std::copy(this->grades, this->grades+psubj, tmp); + tmp[this->psubj] = grade; + this->grades = tmp; + psubj++; +} + +void Student::detailed_print() const +{ + for (int i = 0; i < psubj; i++) + { + std::cout << "Subject " << i+1 << ": "; + std::cout << grades[i] << std::endl; + } + std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; +} + +float Student::calc_average() const +{ + float sum = 0; + for (int i = 0; i < psubj; i++) + sum += grades[i]; + float average = sum / psubj; + return average; +} diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -0,0 +1,59 @@ +#include <iostream> +#include <iomanip> +#include <algorithm> +#include <string> +#include <string.h> + +class Student +{ + private: + char *AM; + std::string name; + std::string *submittedSubjects; + unsigned int numSubmittedSubjects; + unsigned int semester; + unsigned int psubj; + float *grades; + int size; + + public: + Student(const char *AM, const std::string& name); + Student(const char *AM, const std::string& name, unsigned int semester); + Student(const char *AM, const std::string& name, unsigned int semester, + unsigned int psubj, const float *grades); + Student(const Student& s); + ~Student(); + + // += overload + // = overload + + inline bool operator== (const Student& s) const {return (this->semester == s.semester) ? true : false;} + inline bool operator!= (const Student& s) const {return (this->semester != s.semester) ? true : false;} + inline bool operator< (const Student& s) const {return (this->semester < s.semester) ? true : false;} + inline bool operator<= (const Student& s) const {return (this->semester <= s.semester) ? true : false;} + inline bool operator> (const Student& s) const {return (this->semester > s.semester) ? true : false;} + inline bool operator>= (const Student& s) const {return (this->semester >= s.semester) ? true : false;} + + inline const char *get_AM() const {return this->AM;} + inline const std::string& get_name() const {return this->name;} + inline unsigned int get_semester() const {return this->semester;} + inline unsigned int get_psubj() const {return this->psubj;} + inline float *get_grades() const {return this->grades;} + inline unsigned int get_num_submitted_subjects() const {return this->numSubmittedSubjects;} + inline std::string *get_submitted_subjects() const {return this->submittedSubjects;} // const? + + inline void set_AM(const char *AM) {this->AM = convert_AM(AM);} + inline void set_name(const std::string& name) {this->name = name;} + inline void set_semester(unsigned int semester) {this->semester = semester;} + inline void set_psubj(unsigned int psubj) {this->psubj = psubj;} + inline void set_grades(float *grades) {this->grades = convert_PSG(grades);} + inline void set_num_submitted_subjects(unsigned int numSubmittedSubjects) {this->numSubmittedSubjects = numSubmittedSubjects;} + inline void set_submitted_subjects(std::string* submittedSubjects) {this->submittedSubjects = submittedSubjects;} // reference?? + inline void print_3first() const {std::cout << this->AM << " " << this->name << " " << this->semester << std::endl;} + + char *convert_AM(const char *AM); + float *convert_PSG(const float *grades); + void add_grade(float grade); + void detailed_print() const; + float calc_average() const; +}; diff --git a/assignment-2.3-operoverloading/src/subject.cpp b/assignment-2.3-operoverloading/src/subject.cpp @@ -0,0 +1,4 @@ +#include "subject.h" + +Subject::Subject(const std::string& code, const std::string& sname, unsigned int subjsemester) + :code(code), sname(sname), subjsemester(subjsemester) {} diff --git a/assignment-2.3-operoverloading/src/subject.h b/assignment-2.3-operoverloading/src/subject.h @@ -0,0 +1,21 @@ +#include <iostream> +#include <string> + +class Subject +{ + private: + std::string code; + std::string sname; + unsigned int subjsemester; + + public: + Subject(const std::string& code, const std::string& sname, unsigned int subjsemester); + + inline const std::string& get_code() const {return this->code;} + inline const std::string& get_sname() const {return this->sname;} + inline unsigned int get_subjsemester() const {return this->subjsemester;} + + inline void set_code(const std::string& code) {this->code = code;} + inline void set_sname(const std::string& sname) {this->sname = sname;} + inline void set_subjsemester(unsigned int subjsemester) {this->subjsemester = subjsemester;} +};