Skip to content

Commit 04e4e68

Browse files
committed
refactor: load old code
1 parent 13be537 commit 04e4e68

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

exercises/ex4.cpp

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
/* Surprise me. */
22

3-
#include <iostream>
4-
#include <string>
5-
#include <map>
3+
#include <stdio.h>
64

7-
const int ZERO = 48;
8-
const int NINE = 57;
9-
const int MAX_NUMBER = 10;
10-
const int MINUS = 45;
5+
int main()
6+
{
7+
int month;
118

12-
const std::string ERROR_COLOR = "\033[31m";
13-
const std::string NORMAL_COLOR = "\033[0m";
9+
/* Input month number from user */
10+
printf("Enter month number(1-12): ");
11+
scanf("%d", &month);
1412

15-
int insertNumber();
16-
bool checkStringIsNumber(std::string in);
13+
switch (month)
14+
{
15+
case 1:
16+
printf("31 days");
17+
break;
18+
case 2:
19+
printf("28/29 days");
20+
break;
21+
case 3:
22+
printf("31 days");
23+
break;
24+
case 4:
25+
printf("30 days");
26+
break;
27+
case 5:
28+
printf("31 days");
29+
break;
30+
case 6:
31+
printf("30 days");
32+
break;
33+
case 7:
34+
printf("31 days");
35+
break;
36+
case 8:
37+
printf("31 days");
38+
break;
39+
case 9:
40+
printf("30 days");
41+
break;
42+
case 10:
43+
printf("31 days");
44+
break;
45+
case 11:
46+
printf("30 days");
47+
break;
48+
case 12:
49+
printf("31 days");
50+
break;
51+
default:
52+
printf("Invalid input! Please enter month number between 1-12");
53+
}
1754

18-
int main(){
19-
int month = insertNumber();
20-
std::map<int, std::string> calendary = {
21-
{1, "31"},
22-
{2, "28/29"},
23-
{3, "31"},
24-
{4, "30"},
25-
{5, "31"},
26-
{6, "30"},
27-
{7, "31"},
28-
{8, "31"},
29-
{9, "30"},
30-
{10, "31"},
31-
{11, "30"},
32-
{12, "31"},
33-
};
34-
std::cout << calendary[month] << " days";
3555
return 0;
36-
}
37-
38-
int insertNumber(){
39-
std::string insert;
40-
bool isCorrect = true;
41-
do {
42-
if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR;
43-
std::cout << "Insert Mounth: ";
44-
getline(std::cin, insert);
45-
} while(!(isCorrect = checkStringIsNumber(insert)));
46-
return std::stoi(insert);
47-
}
48-
49-
bool checkStringIsNumber(std::string in){
50-
if(in.empty()) return false;
51-
for(int i = 0; i < in.size(); i++)
52-
if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false;
53-
if(stoi(in) > MAX_NUMBER) return false;
54-
return true;
5556
}

0 commit comments

Comments
 (0)