Skip to content

Commit 13f75a5

Browse files
committed
chore: Refactor BitcoinExchange processInputFile to handle bad input values and improve error handling
1 parent b4186a9 commit 13f75a5

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

module 09/ex00/BitcoinExchange.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,28 @@ void BitcoinExchange::processInputFile(const std::string &filename) const
192192
continue;
193193
}
194194

195-
try
195+
std::stringstream ss(valueStr);
196+
float value;
197+
198+
ss >> value;
199+
200+
if (!ss.eof())
196201
{
197-
if (valueStr.find_first_not_of("0123456789.-") != std::string::npos)
198-
throw std::runtime_error("Invalid value format");
199-
float value = atof(valueStr.c_str());
202+
std::cout << "Error: bad input => " << valueStr << std::endl;
203+
continue;
204+
}
200205

201-
if (!isValidValue(value))
202-
{
203-
if (value < 0)
204-
std::cout << "Error: not a positive number." << std::endl;
205-
else
206-
std::cout << "Error: too large a number." << std::endl;
207-
continue;
208-
}
206+
if (!isValidValue(value))
207+
{
208+
if (value < 0)
209+
std::cout << "Error: not a positive number." << std::endl;
210+
else
211+
std::cout << "Error: too large a number." << std::endl;
212+
continue;
213+
}
209214

215+
try
216+
{
210217
float exchangeRate = getExchangeRate(date);
211218
std::cout << date << " => " << value << " = " << value * exchangeRate << std::endl;
212219
}

module 09/ex02/PmergeMe.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bool PmergeMe::isValidNumber(const char *str)
3030
char *ptr;
3131
long num = strtol(str, &ptr, 10);
3232

33-
if (*ptr != '\0' || num <= 0 || num > INT_MAX)
33+
if (*ptr != '\0' || num < 0 || num > INT_MAX)
3434
return false;
3535

3636
return true;
@@ -73,8 +73,9 @@ std::vector<int> PmergeMe::getJacob(size_t size)
7373
}
7474

7575

76-
std::vector<int> PmergeMe::getInsertPos(std::vector<int> &jacobSeq, size_t size)
76+
std::vector<int> PmergeMe::getInsertPos(size_t size)
7777
{
78+
std::vector<int> jacobSeq = getJacob(size);
7879
std::vector<int> order;
7980
std::vector<bool> used(size, false);
8081
used[0] = true;
@@ -157,8 +158,7 @@ void PmergeMe::sortVector()
157158

158159
if (pendChain.size() > 1)
159160
{
160-
std::vector<int> jacobSeq = getJacob(pendChain.size());
161-
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pendChain.size());
161+
std::vector<int> insertionOrder = getInsertPos(pendChain.size());
162162

163163
for (size_t i = 0; i < insertionOrder.size(); i++)
164164
{
@@ -234,8 +234,7 @@ void PmergeMe::sortDeque()
234234

235235
if (pendChain.size() > 1)
236236
{
237-
std::vector<int> jacobSeq = getJacob(pendChain.size());
238-
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pendChain.size());
237+
std::vector<int> insertionOrder = getInsertPos(pendChain.size());
239238

240239
for (size_t i = 0; i < insertionOrder.size(); i++)
241240
{
@@ -272,6 +271,15 @@ void PmergeMe::sort()
272271
clock_t deqEnd = clock();
273272
double deqTime = static_cast<double>(deqEnd - deqStart) / CLOCKS_PER_SEC * 1000000;
274273

274+
// for (size_t i = 1; i < _vec.size(); i++)
275+
// {
276+
// if (_vec[i] < _vec[i - 1])
277+
// {
278+
// std::cerr << "Error: std::vector is not sorted" << std::endl;
279+
// return;
280+
// }
281+
// }
282+
275283
printSeq(original, "Before");
276284
printSeq(_vec, "After");
277285

module 09/ex02/PmergeMe.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PmergeMe
2121

2222
bool isValidNumber(const char *str);
2323
std::vector<int> getJacob(size_t size);
24-
std::vector<int> getInsertPos(std::vector<int> &jacobSeq, size_t size);
24+
std::vector<int> getInsertPos(size_t size);
2525

2626
void sortVector();
2727
void sortDeque();

0 commit comments

Comments
 (0)