-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabeth.cpp
More file actions
73 lines (57 loc) · 1.82 KB
/
alphabeth.cpp
File metadata and controls
73 lines (57 loc) · 1.82 KB
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
#include "alphabeth.hpp"
std::string Alphabeth::getResult() {
return this->result;
}
Alphabeth& Alphabeth::setResult(std::string res) {
this->result = res;
return *this;
}
/* new_result = left + right */
std::string Alphabeth::add( std::string left, std::string right ) {
std::string new_result = "";
char chl, chr, res;
int sum, CARRY = 0;
std::string::iterator itl = left.end();
std::string::iterator itr = right.end();
if( left.length() == 0 || right.length() == 0 )
throw "Cannot add empty strings.";
/* Add two strings to each other and without storing */
while( itl > left.begin() || itr > right.begin() || CARRY == 1) {
--itl; --itr;
/* Neutral character */
chl = chr = NEUTRAL;
if( !(itl < left.begin()) )
chl = *itl;
if( !(itr < right.begin()) )
chr = *itr;
sum = LETTERS.find(chl) + LETTERS.find(chr);
res = LETTERS.at( (sum + CARRY) % l_length );
sum >= l_length ? CARRY = 1 : CARRY = 0;
new_result.insert(new_result.begin(), res );
}
/* Remove a's from the beginning */
while( new_result.at(0) == 'a' )
new_result.erase(new_result.begin());
return new_result;
}
/* result += left */
std::string Alphabeth::add( std::string left ) {
result = this->add( left, result );
return result;
}
std::string Alphabeth::operator+(const std::string right) {
return this->add( result, right );
}
Alphabeth Alphabeth::operator+( Alphabeth& right) {
Alphabeth temp;
temp.setResult( this->add( result, right.result ) );
return temp;
}
std::string Alphabeth::operator=(const std::string right) {
result = right;
return result;
}
Alphabeth& Alphabeth::operator=(const Alphabeth& right) {
result = right.result;
return *this;
}