-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tape.cpp
104 lines (87 loc) · 1.97 KB
/
Tape.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Tape.h"
#define TAPE_CELL Tape::TapeCell
Tape::Tape(string str, char blank) : Tape(blank) {
insertStringToTape(str);
}
Tape::Tape(const Tape& other) : blank_symbol(other.blank_symbol){
head = copyOther(other);
}
Tape& Tape::operator=(const Tape& other) {
if (this != &other) {
deleteContent();
head = copyOther(other);
blank_symbol = other.blank_symbol;
}
return *this;
}
Tape::~Tape() {
deleteContent();
}
void Tape::goLeft() {
if (!head->left) {
return;
}
head = head->left;
}
void Tape::goRight() {
if (!head->right) {
head->right = new TapeCell(head, nullptr, blank_symbol);
}
head = head->right;
}
void Tape::replaceSymbol(char new_symbol) {
head->symbol = new_symbol;
}
char Tape::getSymbol() const {
return head->symbol;
}
Tape::operator string() const {
TapeCell* ptr = getFirst();
string* str = new string();
while (ptr != nullptr) {
*str += ptr->symbol;
ptr = ptr->right;
}
return *str;
}
void Tape::insertStringToTape(const string& str) {
TapeCell* begin = head;
for (char c : str) {
head->symbol = c;
if (head->right == nullptr) {
head->right = new TapeCell(head, nullptr, blank_symbol);
}
head = head->right;
}
head = begin;
}
TAPE_CELL* Tape::copyOther(const Tape& other) {
TapeCell* curr_head = new TapeCell(nullptr, nullptr, blank_symbol);
TapeCell* other_head = other.getFirst();
TapeCell* head_place = nullptr;
while (other_head != nullptr) {
curr_head->symbol = other_head->symbol;
if (other_head == other.head) {
head_place = curr_head;
}
other_head = other_head->right;
curr_head->right = (other_head == nullptr) ? nullptr : new TapeCell(curr_head, nullptr, blank_symbol);
curr_head = curr_head->right;
}
return head_place;
}
void Tape::deleteContent() {
head = getFirst();
while (head != nullptr) {
TapeCell* next = head->right;
delete head;
head = next;
}
}
TAPE_CELL* Tape::getFirst() const{
TapeCell* ptr = head;
while (ptr->left != nullptr) {
ptr = ptr->left;
}
return ptr;
}