commit b06adde84825823a56f0658b552cf3cb06537812
parent 48df82e88e9fd09eb699bdc0c75a1cc9cb512745
Author: Christos Margiolis <christos@margiolis.net>
Date: Mon, 16 Mar 2020 20:44:13 +0200
new assignment
Diffstat:
3 files changed, 193 insertions(+), 0 deletions(-)
diff --git a/assignment-2.2-classes/BigIntv1.cpp b/assignment-2.2-classes/BigIntv1.cpp
@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <string.h>
+#include <string>
+#include <new>
+
+using namespace std;
+
+class BigInt
+{
+ int size;
+ char *numb;
+public:
+ BigInt ();
+ BigInt (const char *);
+ BigInt (int);
+ BigInt (const BigInt &);
+ ~BigInt ();
+ void Print () const;
+
+};
+
+BigInt::BigInt ()
+{
+ size = 1;
+ numb = new char [1];
+ numb[0] = 0;
+}
+
+BigInt::BigInt (const char *s)
+{
+ printf("constructing string %s \n",s);
+ int i;
+ int l = strlen(s);
+ if (l)
+ {
+ numb = new char [l];
+ for(i = 0; i < l; i++)
+ numb[i] = s[l - 1 - i] - 48;
+ size = l;
+ }
+}
+
+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 char [l];
+ for(i = 0; i < l; i++)
+ numb[i] = s[l - 1 - i] - 48;
+ size = l;
+ }
+ delete[] s;
+}
+
+BigInt::BigInt (const BigInt &x) //Σβήστε το const (εδώ και στο πρότυπο της κλάσης) και δείτε τι συμβαίνει!!!
+{
+ printf("Copy constructing\n");
+ size = x.size;
+ numb = new char [size];
+ memcpy (numb, x.numb, size);
+}
+
+BigInt::~BigInt ()
+{
+ printf("deleting ");
+ Print();
+ delete [] numb;
+}
+
+void BigInt::Print () const
+{
+ int i;
+ printf ("*");
+ for ( i = size - 1; i >= 0; i--)
+ printf ("%c", numb[i] + 48);
+ printf ("*\n");
+}
+
+
+void XXX (BigInt x) //Τι συμβαίνει αν μπει αναφορά; Πως είναι καλύτερα;
+{
+ //Some functionality
+ x.Print ();
+}
+
+int main()
+{
+ BigInt a;
+ a.Print ();
+ BigInt b = "123456789987654321";
+ b.Print ();
+ BigInt c = 100;
+ c.Print ();
+ BigInt d (123); //Είναι ΑΚΡΙΒΩΣ το ίδιο με το προηγούμενο;; Σε τι θα μπορούσε να διαφέρει;;;
+ d.Print ();
+ BigInt e = b;
+ e.Print ();
+ BigInt f(b);
+ f.Print ();
+ printf ("================================\n");
+ XXX (b); //Τι αλλάζει αν υπάρχει και όταν δεν υπάρχει αντιγραφέας;
+ b.Print ();
+
+ XXX (25);
+
+ //string q = "123";
+ //BigInt z = q;
+ //z.Print ();
+}
+
diff --git a/assignment-2.2-classes/a.out b/assignment-2.2-classes/a.out
Binary files differ.
diff --git a/assignment-2.2-classes/assignment-2.2-classes.cpp b/assignment-2.2-classes/assignment-2.2-classes.cpp
@@ -0,0 +1,77 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+class Student
+{
+ private:
+ std::vector<char> AM;
+ std::string name;
+ unsigned int semester;
+ unsigned int psubj;
+ std::vector<float> psubjGrades;
+
+ public:
+ Student(const std::vector<char>& AM, const std::string& name)
+ :AM(AM), name(name), semester(1), psubj(0) {}
+
+ Student(const std::vector<char>& AM, const std::string& name, unsigned int semester)
+ :psubj(0) {}
+
+ Student(const std::vector<char>& AM, const std::string& name, unsigned int semester,
+ unsigned int psubj, const std::vector<float>& psubjGrades)
+ :AM(AM), name(name), semester(semester), psubj(psubj), psubjGrades(psubjGrades) {}
+
+ Student(const Student& s)
+ :AM(s.AM), name(s.name), semester(s.semester), psubj(s.psubj), psubjGrades(s.psubjGrades) {}
+
+ ~Student() {}
+
+ inline const std::vector<char>& get_AM() const {return AM;}
+ inline const std::string& get_name() const {return name;}
+ inline unsigned int get_semester() const {return semester;}
+ inline unsigned int get_psubj() const {return psubj;}
+ inline const std::vector<float>& get_psubjGrades() const {return psubjGrades;}
+
+ inline void set_AM(const std::vector<char>& AM) {this->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_psubjGrades(const std::vector<float>& psubjGrades) {this->psubjGrades = psubjGrades;}
+ inline void add_grade(float grade) {psubjGrades.push_back(grade);}
+
+ inline void print_3first()
+ {
+ for (std::size_t i = 0; i < AM.size(); i++)
+ std::cout << AM[i];
+ std::cout << " " << name << " " << semester << std::endl;
+ }
+};
+
+int main(int argc, char **argv)
+{
+ std::vector<char> AM = {'1','9','3','9','0','1','3','3'};
+ std::vector<float> psubjGrades = {9.5f, 8.4f, 5.6f};
+ Student *s1 = new Student(AM, "Christos Margiolis");
+ AM = s1->get_AM();
+ std::cout << s1->get_name() << " " << s1->get_semester() << " " << s1->get_psubj() << std::endl;
+ delete s1;
+
+ Student *s2 = new Student(AM, "Christos Margiolis", 2);
+ s2->set_AM(AM);
+ s2->set_name("AAAA");
+ s2->set_semester(2);
+ s2->set_psubj(3);
+ s2->set_psubjGrades(psubjGrades);
+ std::cout << s2->get_name() << " " << s2->get_semester() << " " << s2->get_psubj() << std::endl;
+ delete s2;
+
+ Student *s3 = new Student(AM, "Christos Margiolis", 2, 3, psubjGrades);
+ s3->add_grade(7.5f);
+ s3->print_3first();
+ std::vector<float>().swap(psubjGrades);
+ std::vector<char>().swap(AM);
+ delete s3;
+
+ return 0;
+}