-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuxiliaryMethods.cpp
More file actions
123 lines (110 loc) · 3.48 KB
/
AuxiliaryMethods.cpp
File metadata and controls
123 lines (110 loc) · 3.48 KB
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
#include "AuxiliaryMethods.h"
char AuxiliaryMethods::inputChar() {
string input = "";
char textChar = {0};
while (true) {
getline(cin, input);
if (input.length() == 1) {
textChar = input[0];
break;
}
cout << "To nie jest pojedynczy znak. Wpisz ponownie." << endl;
}
return textChar;
}
string AuxiliaryMethods::inputNumber(string inputText, int charPosition) {
string number = "";
while(isdigit(inputText[charPosition]) == true) {
number += inputText[charPosition];
charPosition ++;
}
return number;
}
string AuxiliaryMethods::inputLine() {
string input = "";
getline(cin, input);
return input;
}
int AuxiliaryMethods::inputInteger() {
string input = "";
int number = 0;
while (true) {
getline(cin, input);
stringstream myStream(input);
if (myStream >> number)
break;
cout << "To nie jest liczba. Wpisz ponownie. " << endl;
}
return number;
}
string AuxiliaryMethods::swapToFirstCapitalLetterThanLowercaseLetters(string inputText) {
if (!inputText.empty()) {
transform(inputText.begin(), inputText.end(), inputText.begin(), ::tolower);
inputText[0] = toupper(inputText[0]);
}
return inputText;
}
bool AuxiliaryMethods::checkDateFormat(string inputDate) {
bool check = true;
int counter = 0;
if (inputDate.length()!=10) {
check=false;
} else {
for (int i=0; (unsigned)i<inputDate.length(); i++) {
if (inputDate[i]==45) counter++;
}
if (counter!=2) {
check = false;
} else {
for (int i=0; (unsigned)i<inputDate.length(); i++) {
if (i==4 || i==7) {
if (inputDate[i] != 45) check = false;
} else if (inputDate[i] < 48 || inputDate[i] > 57) check = false;
}
}
}
return check;
}
bool AuxiliaryMethods::checkDateScope(string inputDate) {
bool check = true;
int year = atoi(inputDate.substr(0,4).c_str());
int month = atoi(inputDate.substr(5,2).c_str());
int day = atoi(inputDate.substr(8,2).c_str());
bool leapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (month > 12) check = false;
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) check = false;
else if (month == 2 && leapYear == 1 && day > 29) check = false;
else if (month == 2 && leapYear == 0 && day > 28) check = false;
else if (day > 31) check = false;
return check;
}
string AuxiliaryMethods::convertDateToIntWithoutDashes(string date) {
string dateWithoutDashes = date;
for (int i=0; (unsigned)i<dateWithoutDashes.length(); i++) {
if (dateWithoutDashes[i]==45)
dateWithoutDashes.erase(i,1);
}
return dateWithoutDashes;
}
string AuxiliaryMethods::convertDateToIntWithDashes(string date) {
string dateWithDashes = date;
if (dateWithDashes.length()==(unsigned)8) {
dateWithDashes.insert(4,"-");
dateWithDashes.insert(7,"-");
}
return dateWithDashes;
}
string AuxiliaryMethods::convertCommaToDot(string value) {
for (int i=0; (unsigned)i<value.length(); i++) {
if (value[i]==44)
value[i]=46;
}
return value;
}
string AuxiliaryMethods::checkValueFormat(string value) {
double newValue = stod(value);
stringstream stream;
stream << fixed << setprecision(2) << newValue;
value = stream.str();
return value;
}