Compare commits

...

3 Commits

Author SHA1 Message Date
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
5 changed files with 83 additions and 43 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

@ -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

@ -10,7 +10,7 @@ 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 2;
@ -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++)

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::vector : " << ((double) (time_vec_stop - time_vec_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl;
}
}