Compare commits
No commits in common. "ca8912c56ee3c6f6776b9f9aaa8eb437b5147c80" and "eb1819276fc3b8b0468424203d37a72365057598" have entirely different histories.
ca8912c56e
...
eb1819276f
@ -2,7 +2,7 @@ CXX := c++
|
||||
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
|
||||
SRCDIR := src
|
||||
OBJDIR := obj
|
||||
NAME := btc
|
||||
NAME := ex00
|
||||
|
||||
SRCS := $(wildcard $(SRCDIR)/*.cpp)
|
||||
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||
|
@ -2,7 +2,7 @@ CXX := c++
|
||||
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
|
||||
SRCDIR := src
|
||||
OBJDIR := obj
|
||||
NAME := RPN
|
||||
NAME := ex01
|
||||
|
||||
SRCS := $(wildcard $(SRCDIR)/*.cpp)
|
||||
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||
|
@ -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<float>& nums, std::stack<char>& operators)
|
||||
int calculate(std::stack<int>& 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<float> nums;
|
||||
std::stack<int> nums;
|
||||
std::stack<char> operators;
|
||||
|
||||
for (size_t i = 0; av[1][i] != '\0'; i++)
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@ -15,13 +14,14 @@ 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].first = array[i * 2];
|
||||
pairs[i].second = array[i * 2];
|
||||
if (array.size() > i * 2 + 1)
|
||||
pairs[i].second = array[i * 2 + 1];
|
||||
pairs[i].first = array[i * 2 + 1];
|
||||
}
|
||||
if (len % 2)
|
||||
pairs[len - 1].first = pairs[len - 1].second;
|
||||
return pairs;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
@ -13,72 +12,7 @@ template<typename T>
|
||||
void display(const T& array)
|
||||
{
|
||||
for (size_t i = 0; i != array.size(); 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);
|
||||
std::cout << array[i] << " ";
|
||||
}
|
||||
|
||||
int main(int ac, char** av)
|
||||
@ -86,8 +20,21 @@ int main(int ac, char** av)
|
||||
{
|
||||
std::vector<int> array;
|
||||
|
||||
if (parsing(av + 1, (size_t) ac - 1, array))
|
||||
return 1;
|
||||
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();
|
||||
|
||||
std::cout << "Before: ";
|
||||
display(array);
|
||||
@ -107,15 +54,28 @@ int main(int ac, char** av)
|
||||
{
|
||||
std::deque<int> array;
|
||||
|
||||
if (parsing(av + 1, (size_t) ac - 1, array))
|
||||
return 1;
|
||||
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();
|
||||
|
||||
const clock_t time_vec_start = clock();
|
||||
const clock_t time_deque_start = clock();
|
||||
|
||||
PmergeMe(array);
|
||||
|
||||
const clock_t time_vec_stop = clock();
|
||||
const clock_t time_deque_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;
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user