Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e1773f

Browse files
First revision
1 parent 8b29533 commit 7e1773f

17 files changed

+169
-85
lines changed

‎2_0_decode_message.cpp‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,77 @@ 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);
12-
char getCharacter(int nr, int mode);
13-
char getPunctuation(int nr);
14-
enum modeType {UPPERCASE, LOWERCASE, PUNCTUATION};
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+
*/
32+
33+
1534

1635
int main()
1736
{
1837

19-
char digit;
38+
char digit;/* (AD): Always initialize variables during declaration. */
2039
char ch = '0';
2140
int nr = 0;
22-
int mode = UPPERCASE;
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; */
2342
int modulo = 0;
2443
digit = cin.get();
2544

26-
27-
while(digit != 10) {
28-
while (!(digit == 10 || digit == ',')) {
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+
while(digit != 10)
48+
{
49+
while (!(digit == 10 || digit == ','))
50+
{
2951
nr = nr*10 + digit - '0';
3052
digit = cin.get();
3153
}
3254
modulo = getModulo(nr, mode);
33-
if (modulo == 0) {
55+
if (modulo == 0)
56+
{
3457
mode = ++mode % 3;
35-
} else {
58+
}
59+
else
60+
{
3661
ch = getCharacter(modulo, mode);
3762
cout << ch;
3863
}
3964
nr = 0;
4065
digit = cin.get();
4166
}
42-
67+
/* (AD): Missing return statement */
68+
return 0;
4369
}
4470

4571

46-
47-
int getModulo(int nr, int mode) {
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 */
4877
if (nr == 0) return 0;
49-
switch(mode) {
78+
switch(mode) {/* (AD): Prefer to use it one line below. This is also valid for while, if, for, do etc... */
5079
case UPPERCASE:
51-
return nr % 27;
52-
break;
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 */
5382
case LOWERCASE:
5483
return nr % 27;
5584
break;
@@ -63,7 +92,8 @@ int getModulo(int nr, int mode) {
6392

6493

6594
}
66-
95+
96+
/* (AD): GetCharacter, T_MODE_ENUM, move '{' to one line below, single return statement */
6797
char getCharacter(int modulo, int mode)
6898
{
6999
switch(mode) {
@@ -81,7 +111,8 @@ char getCharacter(int modulo, int mode)
81111
break;
82112
}
83113
}
84-
114+
115+
/* (AD): GetPunctuation, single return statement, move '{' to one line below. */
85116
char getPunctuation(int modulo)
86117
{
87118
switch(modulo) {

‎2_0_luhne_checksum.cpp‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,43 @@ Luhn checksum without saving the numbers, processing each number one by one.
99

1010

1111

12-
1312
int main()
1413
{
1514

16-
char digitChar;
17-
15+
char digitChar; /* (AD): Always initialize variables during declaration */
1816
int oddSum = 0;
1917
int evenSum = 0;
2018
int finalSum = 0;
2119

2220
int pos = 1;
2321
cout << "Enter a number and press enter to validate: ";
2422
digitChar = cin.get();
25-
while (digitChar != 10) {
23+
while (digitChar != 10)
24+
{
2625
int digit = 0;
27-
digit = digitChar - '0';
28-
if (pos % 2 == 0) { //If position is even from left
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
31+
{
2932
evenSum += digit;
3033
digit *= 2;
31-
if (digit >= 10) {
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 */
3238
digit = 1 + (digit - 10);
3339
}
3440
oddSum += digit;
3541
finalSum = evenSum;
36-
} else { //If position is odd from left
42+
}
43+
else
44+
{ //If position is odd from left
3745
oddSum += digit;
3846
digit *= 2;
39-
if (digit >= 10) {
47+
if (digit >= 10)
48+
{
4049
digit = 1 + (digit - 10);
4150
}
4251
evenSum += digit;
@@ -48,11 +57,13 @@ int main()
4857
}
4958
if (finalSum % 10 == 0) {
5059
cout << "The digit is valid! \n";
51-
} else {
60+
}
61+
else
62+
{
5263
cout << "The digit is invalid! \n";
5364
}
54-
55-
65+
/* (AD): Missing return statement is added */
66+
return0;
5667
}
5768

5869

‎2_0_sideways_triangle.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ int main()
3232
cout << "#";
3333
}
3434
}
35-
35+
/* (AD): Missing return statement */
36+
return 0;
3637
}
3738

3839

‎2_1_hash_shape.cpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ int main()
1515
{
1616

1717
int shapeWidth = 8;
18-
19-
for (int row = 0; row < 4; row++) {
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. */
20+
for (int row = 0; row < 4; row++)
21+
{
2022
cout << setw(row+1);
21-
for(int hashNum = 0; hashNum < shapeWidth-2*row; hashNum++) {
23+
for(int hashNum = 0; hashNum < shapeWidth-2*row; hashNum++)
24+
{
2225
cout << "#";
2326
}
2427
cout << "\n";
25-
}
28+
}
29+
/* (AD): Missing return statement */
30+
return 0;
2631
}

‎2_2_hash_shape.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ using std::setw;
1313

1414
int main()
1515
{
16-
1716
int shapeWidth = 8;
1817
int repeat = shapeWidth;
1918
int change = 2;
2019

2120

22-
for (int width = 2; width > 0; width+=change) {
21+
for (int width = 2; width > 0; width+=change)
22+
{
2323
cout << setw((shapeWidth - width)/2+1);
24-
for(int hashNum = 0; hashNum < width; hashNum++) {
24+
for(int hashNum = 0; hashNum < width; hashNum++)
25+
{
2526
cout << "#";
2627
}
2728
cout << "\n";
@@ -32,5 +33,7 @@ int main()
3233
repeat = -1;
3334
continue;
3435
}
35-
}
36+
}
37+
/* (AD): Missing return statement */
38+
return 0;
3639
}

‎2_3_hash_shape.cpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ int main()
2323
int change = 1;
2424

2525

26-
for (int width = 1; width > 0; width += change) {
26+
for (int width = 1; width > 0; width += change)
27+
{
2728
pos = LEFT;
28-
while (pos != NEXT) {
29-
if (pos == LEFT) {
29+
while (pos != NEXT)
30+
{
31+
if (pos == LEFT)
32+
{
3033
cout << setw(indent);
3134
}
3235
else {
@@ -49,5 +52,7 @@ int main()
4952
spaceWidth -= 4*change;
5053
indent += change;
5154

52-
}
55+
}
56+
/* (AD): Missing return statement */
57+
return 0;
5358
}

‎2_4_hash_shape.cpp‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ int main()
3030

3131

3232

33-
for (int hashWidth=2; hashWidth > 0; hashWidth+=change) {
33+
for (int hashWidth=2; hashWidth > 0; hashWidth+=change)
34+
{
3435
pos = LEFT;
3536
cout << part;
3637

37-
while (pos != NEXT) {
38+
while (pos != NEXT)
39+
{
3840
if (pos == LEFT)
3941
{
4042
cout << setw(indent+extraIndent);
4143
indent += indentChange;
42-
} else {
44+
}
45+
else
46+
{
4347
cout << setw(space);
4448
space += spaceChange;
4549
}
46-
for (int hashNum = 0; hashNum < hashWidth; hashNum++) {
50+
for (int hashNum = 0; hashNum < hashWidth; hashNum++)
51+
{
4752
cout << "#";
4853
}
4954

@@ -52,7 +57,8 @@ int main()
5257
{
5358
pos = ++pos % 3;
5459
}
55-
else {
60+
else
61+
{
5662
pos = NEXT;
5763
}
5864

@@ -88,7 +94,7 @@ int main()
8894
//indent -= 2;
8995
part = BOTTOM;
9096
}
91-
9297
}
93-
98+
/* (AD): Missing return statement */
99+
return 0;
94100
}

‎2_5_ISBN_validator.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using std::cout;
77
ISBN validator without using arrays.
88
*/
99

10-
10+
/* (AD): Define a new type for enum (T_ISBNPART_ENUM) and name enums as T_ISBNPART_PREFIX, T_ISBNPART_GROUP... */
1111
enum isbnPart {PREFIX, GROUP, REGISTRANT, PUBLICATION, CHECK, COMPLETED};
1212

1313
bool verifyPart(int nr, int digitCount, int part);

‎2_6_binary_decimal_converter.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Binary and decimal converter without using arrays (or strings)
1212

1313
int main()
1414
{
15-
int dec;
15+
int dec;/* (AD): Initialize */
1616
int integerSize = 32;
1717

1818
cout << "Type in decimal number to convert to binary:\n";
@@ -28,13 +28,15 @@ int main()
2828
int c = 0;
2929
int n = -1;
3030
int digit = 0;
31-
while (temp) {
31+
while (temp)
32+
{
3233
n++;
3334
temp /= 2;
3435
}
3536
temp = dec;
3637

37-
do {
38+
do
39+
{
3840
c = pow(2, n--);
3941
digit = (temp / c) % 2;
4042
cout << digit;
@@ -57,5 +59,7 @@ int main()
5759
cout << "The decimal number is:\n";
5860
cout << dec;
5961

60-
cin.get();
62+
cin.get();
63+
/* (AD): Missing return statement */
64+
return 0;
6165
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /