@@ -8,42 +8,22 @@ Decode a message from integers to text, without using arrays or strings.
8
8
using std::cin;
9
9
using std::cout;
10
10
11
- int getModulo (int nr, int mode); /* (AD): Functions are cammelCase BUT their first letter should be capital as well GetModulo */
12
- char getCharacter (int nr, int mode); /* (AD): Functions are cammelCase BUT their first letter should be capital as well GetCharacter */
13
- char getPunctuation (int nr); /* (AD): Functions are cammelCase BUT their first letter should be capital as well GetPunctuation */
14
- enum modeType {UPPERCASE, LOWERCASE, PUNCTUATION}; /* (AD): I prefer following enumeration, because this type is passed to func as an argument. */
15
-
16
-
17
-
18
- /*
19
- typedef enum mode (AD): Works for both C and C++.
20
- {
21
- T_MODE_UPPERCASE = 0, (AD): Always initialize first enum eventhough it is already 0
22
- T_MODE_LOWERCASE, (AD): Other people can understand it's a typedef enum by seeing 'T' and 'ENUM'
23
- T_MODE_PUNCTUATION You can use '_' between words in enums, constant definitons, typedefs
24
- }T_MODE_ENUM;
25
- */
26
-
27
-
28
- /* Constant definitions
29
- #define D_LIMIT_MAX 100 (AD): So other people can understand it's constant def by seeing 'D' all capital naming.
30
- #define D_LIMIT_MIN 0
31
- */
11
+ int getModulo (int nr, int mode);
12
+ char getCharacter (int nr, int mode);
13
+ char getPunctuation (int nr)
14
+ enum modeType {UPPERCASE, LOWERCASE, PUNCTUATION};
32
15
33
16
34
17
35
18
int main ()
36
19
{
37
-
38
- char digit; /* (AD): Always initialize variables during declaration. */
39
- char ch = ' 0' ;
20
+ char digit = 0 ;
21
+ char ch = 0 ;
40
22
int nr = 0 ;
41
- int mode = UPPERCASE; /* (AD): Why don't you make a typedef enum as above and use it for mode --> T_MODE_ENUM mode = UPPERCASE; */
23
+ int mode = UPPERCASE;
42
24
int modulo = 0 ;
43
25
digit = cin.get ();
44
26
45
- /* (AD): Don't worry about the code lines, move '{' to one line below such that it becomes aligned with '}'s. */
46
- /* (AD): I'm fixing this part, you can edit the other ones */
47
27
while (digit != 10 )
48
28
{
49
29
while (!(digit == 10 || digit == ' ,' ))
@@ -63,87 +43,91 @@ int main()
63
43
}
64
44
nr = 0 ;
65
45
digit = cin.get ();
66
- }
67
- /* (AD): Missing return statement */
46
+ }
47
+ cin. get ();
68
48
return 0 ;
69
49
}
70
50
71
51
72
- /* (AD): Since mode is a typedef now, change its type from int --> T_MODE_ENUM */
73
- /* (AD): This gives clue to other people about what should they pass to this function as an argument. It's not an int but T_MODE_ENUM. */
74
- int getModulo (int nr, int mode) {
75
- /* (AD): I prefer this opening curly bracket '{' right below the function prototype. */
76
- /* (AD): This is not a must but you should choose one and continue with it. It's different for other functions and this function */
52
+ int getModulo (int nr, int mode)
53
+ {
54
+ int modulo = 0 ;
77
55
if (nr == 0 ) return 0 ;
78
- switch (mode) { /* (AD): Prefer to use it one line below. This is also valid for while, if, for, do etc... */
79
- case UPPERCASE:
80
- return nr % 27 ; /* (AD): As long as not must, please try to have single return for each function. */
81
- break ; /* (AD): So you can define int result = 0; Update result in cases and return it at the end. --> Easy to follow code */
82
- case LOWERCASE:
83
- return nr % 27 ;
84
- break ;
85
- case PUNCTUATION:
86
- return nr % 9 ;
87
- break ;
88
- default :
89
- return ' 0 ' ;
90
- break ;
91
- }
92
-
93
-
56
+ switch (mode)
57
+ {
58
+ case UPPERCASE:
59
+ modulo = nr % 27 ;
60
+ break ;
61
+ case LOWERCASE:
62
+ modulo = nr % 27 ;
63
+ break ;
64
+ case PUNCTUATION:
65
+ modulo = nr % 9 ;
66
+ break ;
67
+ default :
68
+ modulo = 0 ;
69
+ break ;
70
+ }
71
+ return modulo;
94
72
}
95
73
96
- /* (AD): GetCharacter, T_MODE_ENUM, move '{' to one line below, single return statement */
74
+
97
75
char getCharacter (int modulo, int mode)
98
- {
99
- switch (mode) {
100
- case 0 :
101
- return ' A' + modulo - 1 ;
102
- break ;
103
- case 1 :
104
- return ' a' + modulo - 1 ;
105
- break ;
106
- case 2 :
107
- return getPunctuation (modulo);
108
- break ;
109
- default :
110
- return ' 0' ;
111
- break ;
112
- }
76
+ {
77
+ char ch = 0 ;
78
+ switch (mode)
79
+ {
80
+ case 0 :
81
+ ch = ' A' + modulo - 1 ;
82
+ break ;
83
+ case 1 :
84
+ ch = ' a' + modulo - 1 ;
85
+ break ;
86
+ case 2 :
87
+ ch = getPunctuation (modulo);
88
+ break ;
89
+ default :
90
+ ch = 0 ;
91
+ break ;
92
+ }
93
+ return ch;
113
94
}
114
95
115
96
/* (AD): GetPunctuation, single return statement, move '{' to one line below. */
116
97
char getPunctuation (int modulo)
117
- {
118
- switch (modulo) {
119
- case 1 :
120
- return ' !' ;
121
- break ;
122
- case 2 :
123
- return ' ?' ;
124
- break ;
125
- case 3 :
126
- return ' ,' ;
127
- break ;
128
- case 4 :
129
- return ' .' ;
130
- break ;
131
- case 5 :
132
- return ' ' ;
133
- break ;
134
- case 6 :
135
- return ' ;' ;
136
- break ;
137
- case 7 :
138
- return ' "' ;
139
- break ;
140
- case 8 :
141
- return ' \' ' ;
142
- break ;
143
- default :
144
- return ' 0' ;
145
- break ;
146
- }
98
+ {
99
+ char punct = 0 ;
100
+ switch (modulo)
101
+ {
102
+ case 1 :
103
+ punct = ' !' ;
104
+ break ;
105
+ case 2 :
106
+ punct = ' ?' ;
107
+ break ;
108
+ case 3 :
109
+ punct = ' ,' ;
110
+ break ;
111
+ case 4 :
112
+ punct = ' .' ;
113
+ break ;
114
+ case 5 :
115
+ punct = ' ' ;
116
+ break ;
117
+ case 6 :
118
+ punct = ' ;' ;
119
+ break ;
120
+ case 7 :
121
+ punct = ' "' ;
122
+ break ;
123
+ case 8 :
124
+ punct = ' \' ' ;
125
+ break ;
126
+ default :
127
+ punct = 0 ;
128
+ break ;
129
+ }
130
+ return punct;
147
131
}
148
132
149
133
0 commit comments