Compare commits

...

9 Commits

Author SHA1 Message Date
f4a5053ff6 finish 2023-12-20 15:01:05 +00:00
9699c0c218 fix 2023-12-20 13:41:56 +00:00
ca8912c56e ex02: fix: parsing 2023-12-16 15:27:01 +01:00
0852968609 RPN: use float 2023-12-16 14:13:52 +01:00
fee188de95 rename: executables 2023-12-16 14:11:47 +01:00
eb1819276f ex01: fix: maybe useless 2023-12-16 12:22:02 +01:00
481de18bbd fix: do calcul in right order 2023-12-16 12:19:37 +01:00
a37a07615b ex00: fix valid date 2023-12-16 12:19:09 +01:00
9965c6d49d ex01: clean remove trash file 2023-12-16 12:18:58 +01:00
9 changed files with 108 additions and 69 deletions

View File

@ -2,7 +2,7 @@ CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex00
NAME := btc
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))

View File

@ -21,7 +21,6 @@ static bool is_leap(int year)
static bool date_is_valid(int d, int m, int y)
{
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
@ -50,7 +49,7 @@ static int check_date(const std::string& line, unsigned int& out)
mounth = std::atoi(line.substr(5).c_str());
day = std::atoi(line.substr(8).c_str());
if (date_is_valid(day, mounth, year))
if (!date_is_valid(day, mounth, year))
return 1;
out = year * 10000 + mounth * 100 + day;
return 0;
@ -80,7 +79,7 @@ static int check_value(const std::string& value_str, int flag, float& out)
static int check_line(const std::string& line, regex_t& reegex, const std::string& delimiter, int flag, std::pair<unsigned int, float>& out)
{
if (line == std::string("date") + delimiter + std::string(flag == DATABASE ? "exchange_rate" : "value"))
if (line == std::string("date") + delimiter + (flag == DATABASE ? "exchange_rate" : "value"))
return 1;
if (regexec(&reegex, line.c_str(), 0, NULL, 0) == REG_NOMATCH)
{
@ -89,8 +88,10 @@ static int check_line(const std::string& line, regex_t& reegex, const std::strin
}
unsigned int date;
if (check_date(line, date))
{
std::cout << "Error: bad input => " << line << std::endl;
return 1;
}
float value;
if (check_value(line.substr(10 + delimiter.length()), flag, value))
return 1;
@ -107,7 +108,7 @@ static int get_data(std::map<unsigned int, float>& out)
if (!file.good())
{
std::cout << "error: file: " << "data.csv" << std::endl;
std::cout << "Error: could not open file." << std::endl;
return 1;
}
@ -115,7 +116,7 @@ static int get_data(std::map<unsigned int, float>& out)
if (regcomp(&reegex, DATA_PATERN, REG_EXTENDED))
{
std::cout << "regex compilation fail" << std::endl;
std::cout << "Error: regex compilation fail." << std::endl;
return 1;
}
@ -186,11 +187,8 @@ static int get_input(const std::string& file_path, const std::map<unsigned int,
void get_price(const std::string& file_path)
{
static bool db_init = 0;
static std::map<unsigned int, float> database;
if (db_init == 0)
get_data(database);
std::map<unsigned int, float> database;
get_data(database);
get_input(file_path, database);
}

View File

@ -5,7 +5,7 @@ int main(int ac, char** av)
{
if (ac != 2)
{
std::cout << "error: missing file" << std::endl;
std::cout << "error: could not open file." << std::endl;
return 1;
}
get_price(std::string(av[1]));

View File

@ -2,7 +2,7 @@ CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex01
NAME := RPN
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))

View File

@ -1,4 +0,0 @@
#include <stack>
#include <iostream>

0
ex01/src/RPN.hpp Normal file
View File

View File

@ -10,16 +10,16 @@ int addition(int a, int b) { return (a + b); }
int division(int a, int b) { return (a / b); }
int multiplication(int a, int b) { return (a * b); }
int calculate(std::stack<int>& nums, std::stack<char>& operators)
int calculate(std::stack<float>& nums, std::stack<char>& operators)
{
if (nums.size() < 2 || operators.size() == 0)
return 0;
return 2;
int ret;
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
int a = nums.top();
nums.pop();
switch (operators.top()) {
case '+':
@ -50,7 +50,7 @@ int main(int ac, char** av)
return 1;
}
std::stack<int> nums;
std::stack<float> nums;
std::stack<char> operators;
for (size_t i = 0; av[1][i] != '\0'; i++)
@ -60,17 +60,22 @@ int main(int ac, char** av)
if (std::isdigit(av[1][i]))
nums.push(av[1][i] - '0');
else if (std::strchr("/+-*", av[1][i]) != NULL)
{
operators.push(av[1][i]);
int ret = calculate(nums, operators);
if (ret == 1)
{
std::cout << "error: division by 0" << std::endl;
return 1;
}
else if (ret == 2)
break;
}
else
{
std::cout << "error: '" << av[1][i] << "' unknown char" << std::endl;
return 1;
}
if (calculate(nums, operators))
{
std::cout << "error: division by 0" << std::endl;
return 1;
}
}
if (nums.size() != 1)
{
@ -79,7 +84,7 @@ int main(int ac, char** av)
}
if (operators.size() != 0)
{
std::cout << "error: too manu operators" << std::endl;
std::cout << "error: too many operators" << std::endl;
return 1;
}
std::cout << nums.top();

View File

@ -2,6 +2,7 @@
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
@ -14,14 +15,13 @@ std::pair<int, int>* create_pairs(const T& array, size_t len)
{
std::pair<int, int>* pairs = new std::pair<int, int>[len];
pairs[len - 1].second = -1;
for (size_t i = 0; i < len; i++)
{
pairs[i].second = array[i * 2];
pairs[i].first = array[i * 2];
if (array.size() > i * 2 + 1)
pairs[i].first = array[i * 2 + 1];
pairs[i].second = array[i * 2 + 1];
}
if (len % 2)
pairs[len - 1].first = pairs[len - 1].second;
return pairs;
}

View File

@ -3,6 +3,7 @@
#include <cctype>
#include <cstddef>
#include <cstdlib>
#include <string>
#include <time.h>
#include <deque>
#include <iostream>
@ -12,7 +13,72 @@ template<typename T>
void display(const T& array)
{
for (size_t i = 0; i != array.size(); i++)
std::cout << array[i] << " ";
if (array[i] != -1)
std::cout << array[i] << " ";
}
template <typename T>
int parsing_arg(const char* str, T& array)
{
int number = -1;
for (size_t i = 0; true; i++)
{
if (std::isdigit(str[i]))
{
if (number == -1)
number = 0;
number = number * 10 + str[i] - '0';
}
else if (str[i] == '\0' || str[i] == ' ')
{
if (number != -1)
{
array.push_back(number);
number = -1;
}
if (str[i] == '\0')
return 0;
}
else
{
std::cout << "Error: " << str[i] << i << " invalid character." << std::endl;
return 1;
}
}
}
template <typename T>
int parsing_args(const char* const * args, size_t len, T& array)
{
for (size_t i = 0; i != len; i++)
{
if (parsing_arg(args[i], array))
return 1;
}
return 0;
}
template <typename T>
int parsing_duplicate(T& array)
{
for (size_t i = 0; i + 1 != array.size(); i++)
{
for (size_t j = i + 1; j != array.size(); j++)
{
if (array[i] == array[j])
{
std::cout << "Error: " << array[i] << " is duplicated." << std::endl;
return 1;
}
}
}
return 0;
}
template <typename T>
int parsing(const char* const* args, size_t len, T& array)
{
return parsing_args(args, len, array) || parsing_duplicate(array);
}
int main(int ac, char** av)
@ -20,21 +86,8 @@ int main(int ac, char** av)
{
std::vector<int> array;
for (int i = 1; i != ac; i++)
{
for (int j = 0; av[i][j] != '\0'; j++)
{
if (j == 0 || av[i][j] == ' ')
array.push_back(atoi(av[i] + j + (j != 0)));
if (!std::isdigit(av[i][j]) and av[i][j] != ' ')
{
std::cout << "Error" << std::endl;
return 1;
}
}
}
if (array.size() > 0)
array.pop_back();
if (parsing(av + 1, (size_t) ac - 1, array))
return 1;
std::cout << "Before: ";
display(array);
@ -54,28 +107,15 @@ int main(int ac, char** av)
{
std::deque<int> array;
for (int i = 1; i != ac; i++)
{
for (int j = 0; av[i][j] != '\0'; j++)
{
if (j == 0 || av[i][j] == ' ')
array.push_back(atoi(av[i] + j + (j != 0)));
else if (!std::isdigit(av[i][j]))
{
std::cout << "Error" << std::endl;
return 1;
}
}
}
if (array.size() > 0)
array.pop_back();
if (parsing(av + 1, (size_t) ac - 1, array))
return 1;
const clock_t time_deque_start = clock();
const clock_t time_vec_start = clock();
PmergeMe(array);
const clock_t time_deque_stop = clock();
std::cout << "Time to process a range of " << array.size() <<" elements with std::vector : " << ((double) (time_deque_stop - time_deque_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl;
const clock_t time_vec_stop = clock();
std::cout << "Time to process a range of " << array.size() <<" elements with std::deque : " << ((double) (time_vec_stop - time_vec_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl;
}
}
}