Skip to content

Commit 71a652e

Browse files
committed
code updated
1 parent 385f110 commit 71a652e

25 files changed

+486
-416
lines changed

2_0_decode_message.cpp

Lines changed: 80 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,22 @@ Decode a message from integers to text, without using arrays or strings.
88
using std::cin;
99
using std::cout;
1010

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};
3215

3316

3417

3518
int main()
3619
{
37-
38-
char digit; /* (AD): Always initialize variables during declaration. */
39-
char ch = '0';
20+
char digit = 0;
21+
char ch = 0;
4022
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;
4224
int modulo = 0;
4325
digit = cin.get();
4426

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 */
4727
while(digit != 10)
4828
{
4929
while (!(digit == 10 || digit == ','))
@@ -63,87 +43,91 @@ int main()
6343
}
6444
nr = 0;
6545
digit = cin.get();
66-
}
67-
/* (AD): Missing return statement */
46+
}
47+
cin.get();
6848
return 0;
6949
}
7050

7151

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;
7755
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;
9472
}
9573

96-
/* (AD): GetCharacter, T_MODE_ENUM, move '{' to one line below, single return statement */
74+
9775
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;
11394
}
11495

11596
/* (AD): GetPunctuation, single return statement, move '{' to one line below. */
11697
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;
147131
}
148132

149133

2_0_luhne_checksum.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ Luhn checksum without saving the numbers, processing each number one by one.
88
*/
99

1010

11-
1211
int main()
1312
{
1413

15-
char digitChar; /* (AD): Always initialize variables during declaration */
14+
char digitChar = 0;
1615
int oddSum = 0;
1716
int evenSum = 0;
1817
int finalSum = 0;
@@ -23,25 +22,20 @@ int main()
2322
while (digitChar != 10)
2423
{
2524
int digit = 0;
26-
digit = digitChar - '0';
27-
/* (AD): You might have noticed that I'm using block line comment even single lines */
28-
/* (AD): That's because some old compilers doesn't recognize // */
29-
/* (AD): Most probably you'll not work with them so you can go with // */
30-
if (pos % 2 == 0) //If position is even from left
25+
digit = digitChar - '0';
26+
if (pos % 2 == 0) /* If position is even from left */
3127
{
3228
evenSum += digit;
3329
digit *= 2;
34-
if (digit >= 10) /* (AD): Try to use rvalues on the left side for conditional statements. Prevents invalid assignment and gives compile error. */
35-
{ /* (AD): So, lets say you want to check following equality */
36-
/* (AD): if (x == 10) {....} This is OK but if you miss one '=' and write if(x = 10), no compile error, waste of time by debugging */
37-
/* (AD): Do it like this: if(10 == x) {...}, if you write if(10 = x) you get compile error(10 is not an Lvalue) and fix your mistake quickly */
30+
if (digit >= 10)
31+
{
3832
digit = 1 + (digit - 10);
3933
}
4034
oddSum += digit;
4135
finalSum = evenSum;
4236
}
4337
else
44-
{ //If position is odd from left
38+
{ /*If position is odd from left*/
4539
oddSum += digit;
4640
digit *= 2;
4741
if (digit >= 10)
@@ -61,8 +55,8 @@ int main()
6155
else
6256
{
6357
cout << "The digit is invalid! \n";
64-
}
65-
/* (AD): Missing return statement is added */
58+
}
59+
cin.get();
6660
return 0;
6761
}
6862

2_0_sideways_triangle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ int main()
3131
{
3232
cout << "#";
3333
}
34-
}
35-
/* (AD): Missing return statement */
34+
}
35+
cin.get();
3636
return 0;
3737
}
3838

2_1_hash_shape.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ int main()
1515
{
1616

1717
int shapeWidth = 8;
18-
19-
/* (AD): Go new line for curly braces to align them. Easier to follow complex loops. Don't worry about #of lines of code. */
18+
2019
for (int row = 0; row < 4; row++)
2120
{
2221
cout << setw(row+1);
@@ -26,6 +25,6 @@ int main()
2625
}
2726
cout << "\n";
2827
}
29-
/* (AD): Missing return statement */
28+
cin.get();
3029
return 0;
3130
}

2_2_hash_shape.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ int main()
3434
continue;
3535
}
3636
}
37-
/* (AD): Missing return statement */
37+
cin.get();
3838
return 0;
3939
}

2_3_hash_shape.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ int main()
5353
indent += change;
5454

5555
}
56-
/* (AD): Missing return statement */
56+
cin.get();
5757
return 0;
5858
}

2_4_hash_shape.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ int main()
9191
hashWidth = hashWidth/2+2;
9292
change = -3;
9393
indentChange = -1;
94-
//indent -= 2;
9594
part = BOTTOM;
9695
}
97-
}
98-
/* (AD): Missing return statement */
96+
}
97+
cin.get();
9998
return 0;
10099
}

0 commit comments

Comments
 (0)