-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_0_decode_message.cpp
134 lines (118 loc) · 2.37 KB
/
2_0_decode_message.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
/*
Decode a message from integers to text, without using arrays or strings.
*/
using std::cin;
using std::cout;
int getModulo(int nr, int mode);
char getCharacter(int nr, int mode);
char getPunctuation(int nr)
enum modeType {UPPERCASE, LOWERCASE, PUNCTUATION};
int main()
{
char digit = 0;
char ch = 0;
int nr = 0;
int mode = UPPERCASE;
int modulo = 0;
digit = cin.get();
while(digit != 10)
{
while (!(digit == 10 || digit == ','))
{
nr = nr*10 + digit - '0';
digit = cin.get();
}
modulo = getModulo(nr, mode);
if (modulo == 0)
{
mode = ++mode % 3;
}
else
{
ch = getCharacter(modulo, mode);
cout << ch;
}
nr = 0;
digit = cin.get();
}
cin.get();
return 0;
}
int getModulo(int nr, int mode)
{
int modulo = 0;
if (nr == 0) return 0;
switch(mode)
{
case UPPERCASE:
modulo = nr % 27;
break;
case LOWERCASE:
modulo = nr % 27;
break;
case PUNCTUATION:
modulo = nr % 9;
break;
default:
modulo = 0;
break;
}
return modulo;
}
char getCharacter(int modulo, int mode)
{
char ch = 0;
switch(mode)
{
case 0:
ch = 'A' + modulo - 1;
break;
case 1:
ch = 'a' + modulo - 1;
break;
case 2:
ch = getPunctuation(modulo);
break;
default:
ch = 0;
break;
}
return ch;
}
/* (AD): GetPunctuation, single return statement, move '{' to one line below. */
char getPunctuation(int modulo)
{
char punct = 0;
switch(modulo)
{
case 1:
punct = '!';
break;
case 2:
punct = '?';
break;
case 3:
punct = ',';
break;
case 4:
punct = '.';
break;
case 5:
punct = ' ';
break;
case 6:
punct = ';';
break;
case 7:
punct = '"';
break;
case 8:
punct = '\'';
break;
default:
punct = 0;
break;
}
return punct;
}