Skip to content

Commit ca0d6a0

Browse files
committed
Semester 2 exam task added
1 parent 39e37b6 commit ca0d6a0

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed

Exam/Semester_2/Task_3/main.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
class Vector {
6+
private:
7+
int *arr;
8+
size_t size;
9+
10+
public:
11+
Vector(size_t size);
12+
Vector(Vector& v);
13+
~Vector();
14+
Vector& operator=(Vector& v);
15+
Vector& set(size_t index, int value);
16+
void print();
17+
};
18+
19+
Vector::Vector(size_t size) {
20+
this->size = size;
21+
arr = new int[size];
22+
}
23+
24+
Vector::Vector(Vector& v) {
25+
this->size = v.size;
26+
arr = new int[size];
27+
memcpy(arr, v.arr, sizeof(int) * size);
28+
}
29+
30+
Vector& Vector::operator=(Vector& v) {
31+
if (&v == this) return *this; // присваивание самому себе
32+
this->size = v.size;
33+
delete[] arr;
34+
arr = new int[size];
35+
memcpy(arr, v.arr, sizeof(int) * size);
36+
return *this;
37+
}
38+
39+
Vector::~Vector() {
40+
delete[] arr;
41+
}
42+
43+
Vector& Vector::set(size_t index, int value) {
44+
arr[index] = value;
45+
return *this;
46+
}
47+
48+
void Vector::print() {
49+
for (size_t i = 0; i < size; i++)
50+
std::cout << arr[i] << " ";
51+
cout << endl;
52+
}
53+
54+
int main() {
55+
Vector a(4);
56+
a.set(0, 0).set(1, 1).set(2, 42).set(3, 618).print();
57+
58+
Vector b(a);
59+
b.print();
60+
61+
a.set(0, 777).print();
62+
b.print();
63+
64+
b = a;
65+
b.print();
66+
}

Exam/Semester_2/Task_4/main.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <stdlib.h>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class node_t {
6+
private:
7+
int value;
8+
node_t* next;
9+
public:
10+
node_t(int v = 0);
11+
node_t* get_next();
12+
int get_value();
13+
void set_next(node_t *n);
14+
};
15+
16+
node_t::node_t(int v) {
17+
value = v;
18+
next = NULL;
19+
}
20+
21+
node_t* node_t::get_next() {
22+
return next;
23+
}
24+
25+
int node_t::get_value() {
26+
return value;
27+
}
28+
29+
void node_t::set_next(node_t *n) {
30+
this->next = n;
31+
}
32+
33+
class list_t {
34+
private:
35+
node_t *head;
36+
size_t size;
37+
public:
38+
list_t();
39+
list_t(list_t& l);
40+
~list_t();
41+
42+
list_t& push_back(int value);
43+
list_t& set(int index, int value);
44+
void print();
45+
};
46+
47+
list_t::list_t() {
48+
head = NULL;
49+
size = 0;
50+
}
51+
52+
list_t::list_t(list_t& l) {
53+
head = NULL;
54+
node_t *last = NULL;
55+
for (node_t *lcurr = l.head; lcurr != NULL; lcurr = lcurr->get_next()) {
56+
node_t *curr = new node_t(lcurr->get_value());
57+
if (head == NULL) {
58+
head = curr;
59+
} else {
60+
last->set_next(curr);
61+
}
62+
last = curr;
63+
}
64+
size = l.size;
65+
}
66+
67+
list_t::~list_t() {
68+
node_t *curr = head;
69+
while (curr != NULL) {
70+
node_t *next = curr->get_next();
71+
delete curr;
72+
curr = next;
73+
}
74+
size = 0; head = NULL;
75+
}
76+
77+
list_t& list_t::push_back(int value) {
78+
node_t *n = new node_t(value);
79+
if (head == NULL) {
80+
head = n;
81+
} else {
82+
node_t *curr = head;
83+
while (curr->get_next() != NULL) {
84+
curr = curr->get_next();
85+
}
86+
curr->set_next(n);
87+
}
88+
return *this;
89+
}
90+
91+
list_t& list_t::set(int index, int value) {
92+
node_t *curr = head;
93+
for (int id = 0; id != index; id++) {
94+
curr = curr->get_next();
95+
}
96+
97+
return *this;
98+
}
99+
100+
void list_t::print() {
101+
for (node_t *curr = head; curr != NULL; curr = curr->get_next()) {
102+
cout << curr->get_value() << " ";
103+
}
104+
cout << endl;
105+
}
106+
107+
108+
int main() {
109+
list_t l;
110+
l.push_back(1).push_back(2).push_back(42).push_back(618).print();
111+
112+
list_t l2(l);
113+
l2.push_back(0);
114+
l2.print();
115+
l.print();
116+
}

Exam/Semester_2/Task_5/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// class Figure {
2+
// private:
3+
// int x, y;
4+
// };
5+
6+
// class Rect : public Figure {
7+
// private:
8+
// int h, w;
9+
// };
10+
11+
#include <iostream>
12+
using namespace std;
13+
14+
class Date {
15+
private:
16+
int y, m, d;
17+
18+
public:
19+
friend ostream& operator<<(std::ostream& out, const Date& d) {
20+
out << d.y << "." << d.m << "." << d.d;
21+
return out;
22+
}
23+
};

Exam/Semester_2/Task_9/hugeint.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
class HugeInt {
6+
public:
7+
HugeInt(string str = "0", int digits_count = 40);
8+
HugeInt(const HugeInt &temp, bool invert = false);
9+
HugeInt(long long n);
10+
11+
void set(string str);
12+
bool is_zero();
13+
bool has_minus();
14+
string to_str();
15+
void remove_leading_zeros();
16+
17+
HugeInt& operator=(const HugeInt &temp);
18+
HugeInt& operator=(const long long &temp);
19+
HugeInt& operator=(const string &str);
20+
21+
friend istream& operator>>(istream &in, HugeInt &a);
22+
friend ifstream& operator>>(ifstream &in, HugeInt &a);
23+
24+
friend ostream& operator<<(ostream &out, const HugeInt &a);
25+
friend ofstream& operator<<(ofstream &out, HugeInt &a);
26+
27+
friend bool operator==(const HugeInt &a, const HugeInt &b);
28+
friend bool operator!=(const HugeInt &a, const HugeInt &b);
29+
30+
friend bool operator>(const HugeInt&a, const HugeInt &b);
31+
friend bool operator>=(const HugeInt &a, const HugeInt &b);
32+
33+
friend bool operator<(const HugeInt &a, const HugeInt &b);
34+
friend bool operator<=(const HugeInt &a, const HugeInt &b);
35+
36+
// friend HugeInt operator+(const HugeInt &a, const HugeInt &b);
37+
// friend HugeInt operator-(const HugeInt &a, const HugeInt &b);
38+
39+
const HugeInt operator-();
40+
41+
friend HugeInt operator+(HugeInt a, const HugeInt &b);
42+
// friend HugeInt operator+(HugeInt &a, HugeInt &b);
43+
friend HugeInt operator+(const HugeInt &a, long long value);
44+
friend HugeInt operator+(long long value, const HugeInt &a);
45+
46+
friend HugeInt operator-(HugeInt left, const HugeInt &right);
47+
// friend HugeInt operator-(HugeInt &a, HugeInt &b);
48+
friend HugeInt operator-(const HugeInt &a, long long value);
49+
friend HugeInt operator-(long long value, const HugeInt &a);
50+
51+
52+
private:
53+
vector<unsigned char> array;
54+
unsigned int digit_count;
55+
unsigned int capacity;
56+
bool minus = false;
57+
};
58+
59+
bool operator==(const HugeInt &a, const HugeInt &b) {
60+
if (a.digit_count == b.digit_count && a.minus == b.minus) {
61+
for (int i = 0; i < a.digit_count; i++)
62+
if (a.array[i] != b.array[i]) return false;
63+
return true;
64+
} else return false;
65+
}
66+
67+
bool operator<(const HugeInt &a, const HugeInt &b) {
68+
if (a.minus == b.minus) {
69+
if (a.minus) {
70+
// если оба отрицательные
71+
if (a.digit_count == b.digit_count) {
72+
// сравниваем каждую цифру
73+
for (int i = 0; i < a.digit_count; i++)
74+
if (a.array[i] == b.array[i]) continue;
75+
else return (a.array[i] > b.array[i]);
76+
return false;
77+
} else return (a.digit_count > b.digit_count);
78+
} else {
79+
// если оба положительные
80+
if (a.digit_count == b.digit_count) {
81+
for (int i = 0; i < a.digit_count; i++)
82+
if (a.array[i] == b.array[i]) continue;
83+
else return (a.array[i] < b.array[i]);
84+
return false;
85+
// сравниваем каждую цифру
86+
} else return (a.digit_count < b.digit_count);
87+
}
88+
} else return a.minus;
89+
}
90+
91+
ostream& operator<<(ostream &out, const HugeInt &a) {
92+
if (a.minus) out << "-";
93+
for (int i = a.digit_count - 1; i > -1; i--)
94+
out << (char) (a.array[i] + '0');
95+
return out;
96+
}
97+
98+
class complex {
99+
private:
100+
double re, im;
101+
public:
102+
complex(double re = 0, double im = 0) : re(re), im(im) {}
103+
friend ostream& operator<<(ostream& out, const complex& c) {
104+
out << "(" << c.re << "+" << c.im << "*i)";
105+
}
106+
};

0 commit comments

Comments
 (0)