Skip to content

Commit a670065

Browse files
committed
first commit to cpp repo
0 parents  commit a670065

15 files changed

+790
-0
lines changed

array/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
TARGET=$(shell basename `pwd`)
2+
SOURCES=$(wildcard *.cpp)
3+
OBJECTS=$(SOURCES:%.cpp=%.o)
4+
5+
all: $(TARGET)
6+
7+
$(OBJECTS): $(SOURCES)
8+
9+
$(TARGET): $(OBJECTS)
10+
$(CXX) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LOADLIBES) $(LDLIBS)
11+
12+
clean:
13+
$(RM) $(OBJECTS) $(TARGET)
14+
15+
.PHONY: all clean

array/array

14.2 KB
Binary file not shown.

array/array.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
template <class T> class array {
6+
private:
7+
T *arr;
8+
size_t msize;
9+
10+
public:
11+
size_t size() const {
12+
return msize;
13+
}
14+
15+
void resize(size_t esize) {
16+
T *earr = new T[esize];
17+
if (esize < msize) for (size_t i = 0; i < esize; i++) earr[i] = arr[i];
18+
else for (size_t i = 0; i < msize; i++) earr[i] = arr[i];
19+
msize = esize;
20+
arr = new T[esize];
21+
arr = earr;
22+
}
23+
24+
T& operator [] (size_t i) {
25+
return arr[i];
26+
}
27+
28+
inline T operator [] (size_t i) const {
29+
return arr[i];
30+
}
31+
32+
void operator = (array b) {
33+
msize = b.size();
34+
arr = new int[b.size()];
35+
for (size_t i = 0; i < b.size(); i++) arr[i] = b[i];
36+
}
37+
38+
T get (size_t index) {
39+
return arr[index];
40+
}
41+
42+
void set (size_t index, T value) {
43+
if (index == msize) {
44+
resize(msize+1);
45+
arr [msize] = value;
46+
}
47+
else if ((index >= 0) && (index < msize)) arr[index] = value;
48+
else if (index < 0) set (0, value);
49+
else if (index >= msize) set (msize - 1, value);
50+
}
51+
52+
void push_back(T value) {
53+
resize(msize + 1);
54+
set(msize-1, value);
55+
}
56+
57+
array() {
58+
msize = 0;
59+
}
60+
61+
array(size_t esize) {
62+
msize = esize;
63+
arr = new T[esize];
64+
}
65+
66+
array(const array &oth) {
67+
msize = oth.size();
68+
arr = new T[msize];
69+
for (size_t i = 0; i < msize; i++) {
70+
set(i,oth[i]);
71+
}
72+
}
73+
74+
~array() {
75+
delete[] arr;
76+
}
77+
78+
};
79+
80+
81+
int main () {
82+
array <int> a;
83+
array <float> b;
84+
cout << a.size() << endl;
85+
for (int i = 0; i < 10; i++) {
86+
a.push_back(i+1);
87+
cout << a[i] << " ";
88+
}
89+
cout << endl;
90+
a.resize(5);
91+
cout << a.size() << endl;
92+
for (int i = 0; i < a.size(); i++) {
93+
cout << a[i] << " ";
94+
}
95+
cout << endl;
96+
for (int i = 0; i < 10; i++) {
97+
float t = i;
98+
b.push_back((t+1)*3332/1000);
99+
cout << b[i] << " ";
100+
}
101+
cout << endl;
102+
array <int> c = a;
103+
array <int> d(a);
104+
return 0;
105+
}

array/array.o

12.1 KB
Binary file not shown.

ptr/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
TARGET=$(shell basename `pwd`)
2+
SOURCES=$(wildcard *.cpp)
3+
OBJECTS=$(SOURCES:%.cpp=%.o)
4+
5+
all: $(TARGET)
6+
7+
$(OBJECTS): $(SOURCES)
8+
9+
$(TARGET): $(OBJECTS)
10+
$(CXX) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LOADLIBES) $(LDLIBS)
11+
12+
clean:
13+
$(RM) $(OBJECTS) $(TARGET)
14+
15+
.PHONY: all clean

ptr/ptr

20.6 KB
Binary file not shown.

ptr/ptr.cpp

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
4+
using namespace std;
5+
6+
class object {
7+
public:
8+
int x;
9+
10+
object(int t = 0) { x = t; }
11+
};
12+
13+
// DELETERS
14+
template <class T>
15+
class deleter {
16+
public:
17+
virtual void operator() (T* d) const { cout << "object deleted" << endl; }
18+
};
19+
20+
template <class T>
21+
class mfree : public deleter <T> {
22+
public:
23+
virtual void operator() (T* d) const { free(d); }
24+
};
25+
26+
template <class T>
27+
class arrdeleter : public deleter <T> {
28+
public:
29+
virtual void operator() (T* d) const { delete[] d; }
30+
};
31+
32+
template <class T>
33+
class objdeleter : public deleter <T> {
34+
public:
35+
virtual void operator() (T* d) const { delete d; }
36+
};
37+
38+
39+
40+
// SCOPED POINTER
41+
template <class T>
42+
class scoped_ptr {
43+
private:
44+
explicit scoped_ptr(const scoped_ptr <T>& p) {}
45+
scoped_ptr <T>& operator = (const scoped_ptr <T>& p) const {}
46+
T* ptr;
47+
deleter <T> del;
48+
49+
public:
50+
explicit scoped_ptr(T* p = 0) { ptr = p; }
51+
explicit scoped_ptr(T* p, const deleter <T>& d) { ptr = p; del = d; }
52+
~scoped_ptr() {
53+
if (!isNull())
54+
del(ptr);
55+
}
56+
57+
T& operator *() const { return *ptr; }
58+
T* operator ->() const { return ptr; }
59+
T* pointer() const { return ptr; }
60+
61+
bool isNull() const { return ptr == 0; }
62+
};
63+
64+
// AUTO POINTER
65+
template <class T>
66+
class auto_ptr {
67+
private:
68+
T* ptr;
69+
deleter <T> del;
70+
71+
public:
72+
explicit auto_ptr(T* p = 0) { ptr = p; }
73+
explicit auto_ptr(T* p, const deleter <T>& d) { ptr = p; del = d; }
74+
~auto_ptr() {
75+
if (!isNull())
76+
del(ptr);
77+
}
78+
auto_ptr(auto_ptr <T>& p) {
79+
ptr = p.pointer();
80+
p.ptr = 0;
81+
}
82+
83+
auto_ptr <T>& operator = (auto_ptr <T>& p) const {
84+
if (this != &p) {
85+
if (!isNull()) {
86+
del(ptr);
87+
}
88+
ptr = p.pointer();
89+
p.ptr = 0;
90+
}
91+
return *this;
92+
}
93+
94+
T& operator *() const { return *ptr; }
95+
T* operator ->() const { return ptr; }
96+
T* pointer() const { return ptr; }
97+
98+
bool isNull() const { return ptr == 0; }
99+
};
100+
101+
// MAIN
102+
int main () {
103+
int* arrrr = new int[14];
104+
int* marrr;
105+
marrr = (int*) malloc(14);
106+
for (int i = 0; i < 14; i++) {
107+
arrrr[i] = i;
108+
marrr[i] = i + 1;
109+
}
110+
111+
// SCOPED POINTER
112+
cout << "SCOPED_PTR: " << endl;
113+
114+
scoped_ptr<int> arr(arrrr, arrdeleter<int>());
115+
scoped_ptr<int> array(marrr, mfree<int>());
116+
cout << "Array created with 'new': ";
117+
for (int i = 0; i < 14; i++)
118+
cout << arr.pointer()[i] << " ";
119+
cout << "DONE" << endl;
120+
121+
cout << "Array created with 'malloc': ";
122+
for (int i = 0; i < 14; i++)
123+
cout << array.pointer()[i] << " ";
124+
cout << "DONE" << endl << endl;
125+
126+
cout << "Objects of class 'object' created with 'new' in loop of 3: " << endl;
127+
for (int i = 0; i < 3; i++) {
128+
object* t = new object(i);
129+
scoped_ptr<object> tre(t, objdeleter<object>());
130+
cout << "Pointer object address: " << &tre << " -> " << tre.pointer()->x << " ";
131+
}
132+
cout << endl;
133+
134+
// AUTO POINTER
135+
cout << "AUTO_PTR: " << endl;
136+
137+
auto_ptr<int> aarr(arrrr, arrdeleter<int>());
138+
auto_ptr<int> aarray(marrr, mfree<int>());
139+
cout << "Array created with 'new': ";
140+
for (int i = 0; i < 14; i++)
141+
cout << aarr.pointer()[i] << " ";
142+
cout << "points at address " << aarr.pointer() << endl;
143+
144+
cout << "Array created with 'malloc': ";
145+
for (int i = 0; i < 14; i++)
146+
cout << aarray.pointer()[i] << " ";
147+
cout << "points at address " << aarray.pointer() << endl << endl;
148+
149+
auto_ptr<int> copy = aarr;
150+
cout << "Copy of pointer to first array: ";
151+
for (int i = 0; i < 14; i++)
152+
cout << copy.pointer()[i] << " ";
153+
cout << "points at address " << copy.pointer() << endl;
154+
cout << "Old pointer points to address " << aarr.pointer() << endl << endl;
155+
156+
cout << "Objects of class 'object' created with 'new' in loop of 3: " << endl;
157+
for (int i = 0; i < 3; i++) {
158+
object* t = new object(i);
159+
auto_ptr<object> tre(t, objdeleter<object>());
160+
cout << "Pointer object address: " << &tre << " -> " << tre.pointer()->x << " ";
161+
}
162+
cout << endl;
163+
return 0;
164+
}

ptr/ptr.o

28.9 KB
Binary file not shown.

rational/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
TARGET=$(shell basename `pwd`)
2+
SOURCES=$(wildcard *.cpp)
3+
OBJECTS=$(SOURCES:%.cpp=%.o)
4+
5+
all: $(TARGET)
6+
7+
$(OBJECTS): $(SOURCES)
8+
9+
$(TARGET): $(OBJECTS)
10+
$(CXX) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LOADLIBES) $(LDLIBS)
11+
12+
clean:
13+
$(RM) $(OBJECTS) $(TARGET)
14+
15+
.PHONY: all clean

rational/Rational.h

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
#include <ostream>
3+
4+
class Rational {
5+
private:
6+
unsigned n;
7+
unsigned d;
8+
bool s;
9+
10+
public:
11+
// getters, setters & modificators;
12+
unsigned numerator() const;
13+
unsigned denominator() const;
14+
15+
void set_numerator(unsigned num);
16+
void set_denominator(unsigned den);
17+
18+
bool sign() const; // returns current sign
19+
void set_sign(bool es);
20+
21+
void revert(); // reverts sign
22+
Rational& invert(); // inverts fraction
23+
24+
// additional methods
25+
Rational& reduce();
26+
27+
// constructors & destructors
28+
Rational();
29+
Rational(unsigned num, unsigned den);
30+
Rational(unsigned num, unsigned den, bool sign);
31+
Rational(int num, int den);
32+
Rational(unsigned num);
33+
Rational(int num);
34+
Rational(const Rational& r);
35+
36+
// operators
37+
Rational& operator= (const Rational& r);
38+
Rational& operator+= (const Rational& r);
39+
Rational& operator-= (const Rational& r);
40+
Rational& operator*= (const Rational& r);
41+
Rational& operator/= (const Rational& r);
42+
Rational& operator++();
43+
Rational operator++(int);
44+
Rational& operator--();
45+
Rational operator--(int);
46+
47+
operator double ();
48+
};
49+
50+
unsigned abs(int a);
51+
bool common_sign(int a, int b);
52+
unsigned gcd(unsigned a, unsigned b);
53+
54+
Rational operator+ (const Rational& a, const Rational& b);
55+
Rational operator- (const Rational& a, const Rational& b);
56+
Rational operator* (const Rational& a, const Rational& b);
57+
Rational operator/ (const Rational& a, const Rational& b);
58+
59+
bool operator== (const Rational& a, const Rational& b);
60+
bool operator!= (const Rational& a, const Rational& b);
61+
bool operator<= (const Rational& a, const Rational& b);
62+
bool operator>= (const Rational& a, const Rational& b);
63+
bool operator< (const Rational& a, const Rational& b);
64+
bool operator> (const Rational& a, const Rational& b);
65+
66+
std::ostream& operator<<(std::ostream &s, const Rational &d);

0 commit comments

Comments
 (0)