-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rename _has_overflow * refactor test_is_invalid_argument * implement generate_stack * update generate_stack with coordinate compression * add dev mode * implement nano_sort case 2 * implement nano_sort case 3 * update gitignore * add visualizer to .gitignore * fix preprocessor in main.c * update lst->content, (char *) -> (int *)
- Loading branch information
Showing
8 changed files
with
255 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/11 15:45:14 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/12 16:02:39 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -25,6 +25,10 @@ | |
|
||
bool is_invalid_argument(int argc, char **argv); | ||
|
||
t_list *generate_stack(int argc, char **argv); | ||
|
||
void nano_sort(t_list *stack, int argc); | ||
|
||
void exit_with_error(void); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* generate_stack.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 12:56:26 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
int *_generate_int_array(int array_size) | ||
{ | ||
int *array; | ||
|
||
array = malloc(sizeof(int) * array_size); | ||
if (!array) | ||
exit_with_error(); | ||
return (array); | ||
} | ||
|
||
int *_generate_numberd_array(int array_size, char **argv) | ||
{ | ||
int i; | ||
int *numberd_array; | ||
|
||
numberd_array = _generate_int_array(array_size); | ||
i = 0; | ||
while (i < array_size) | ||
{ | ||
numberd_array[i] = ft_atoi(argv[i + 1]); | ||
i++; | ||
} | ||
return (numberd_array); | ||
} | ||
|
||
int *_generate_sorted_array(int argc, char **argv) | ||
{ | ||
int array_size; | ||
int *sorted_array; | ||
int i; | ||
int j; | ||
|
||
array_size = argc - 1; | ||
sorted_array = _generate_numberd_array(array_size, argv); | ||
i = array_size - 1; | ||
while (i > 0) | ||
{ | ||
j = 0; | ||
while (j < i) | ||
{ | ||
if (sorted_array[j] > sorted_array[j + 1]) | ||
ft_swap(sorted_array + j, sorted_array + j + 1); | ||
j++; | ||
} | ||
i--; | ||
} | ||
return (sorted_array); | ||
} | ||
|
||
int *_coordinate_compression(int argc, char **argv) | ||
{ | ||
int *compressred_array; | ||
int *sorted_array; | ||
int array_size; | ||
int i; | ||
int j; | ||
|
||
array_size = argc - 1; | ||
compressred_array = _generate_int_array(array_size); | ||
sorted_array = _generate_sorted_array(argc, argv); | ||
i = 0; | ||
while (i < array_size) | ||
{ | ||
j = 0; | ||
while (j < array_size) | ||
{ | ||
if (ft_atoi(argv[i + 1]) == sorted_array[j]) | ||
compressred_array[i] = j + 1; | ||
j++; | ||
} | ||
i++; | ||
} | ||
free(sorted_array); | ||
return (compressred_array); | ||
} | ||
|
||
t_list *generate_stack(int argc, char **argv) | ||
{ | ||
t_list *stack; | ||
int *compressed_array; | ||
int i; | ||
int *ptr; | ||
|
||
compressed_array = _coordinate_compression(argc, argv); | ||
i = 0; | ||
stack = NULL; | ||
while (i < argc - 1) | ||
{ | ||
ptr = ft_calloc(1, sizeof(int)); | ||
if (!ptr) | ||
exit_with_error(); | ||
*ptr = compressed_array[i]; | ||
ft_lstadd_back(&stack, ft_lstnew(ptr)); | ||
i++; | ||
} | ||
free(compressed_array); | ||
return (stack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/11 14:44:55 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/11 16:18:03 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/11 18:16:19 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -28,7 +28,7 @@ static bool _has_not_digit(int argc, char **argv) | |
return (false); | ||
} | ||
|
||
static bool _has_overflowed(int argc, char **argv) | ||
static bool _has_overflow(int argc, char **argv) | ||
{ | ||
int i; | ||
long num; | ||
|
@@ -67,6 +67,6 @@ static bool _has_duplicate(int argc, char **argv) | |
bool is_invalid_argument(int argc, char **argv) | ||
{ | ||
return (_has_not_digit(argc, argv) | ||
|| _has_overflowed(argc, argv) | ||
|| _has_overflow(argc, argv) | ||
|| _has_duplicate(argc, argv)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,42 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/11 15:56:52 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/15 12:04:04 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
#ifdef DEV | ||
|
||
# ifdef __APPLE__ | ||
|
||
__attribute__((destructor)) | ||
void destructor(void) | ||
{ | ||
system("leaks -q push_swap"); | ||
} | ||
|
||
# endif | ||
|
||
void put_void(void *content) | ||
{ | ||
puts(content); | ||
} | ||
|
||
#endif | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
t_list *stack_a; | ||
|
||
if (argc < 2) | ||
return (0); | ||
if (is_invalid_argument(argc, argv)) | ||
exit_with_error(); | ||
stack_a = generate_stack(argc, argv); | ||
if (argc < 5) | ||
nano_sort(stack_a, argc); | ||
ft_lstclear(&stack_a, free); | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* nano_sort.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 12:58:12 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
static void _handle_2(t_list *stack) | ||
{ | ||
int first; | ||
int second; | ||
|
||
first = *(int *)stack->content; | ||
second = *(int *)stack->next->content; | ||
if (first > second) | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
} | ||
|
||
static void _handel_3(t_list *stack) | ||
{ | ||
int first; | ||
int second; | ||
int third; | ||
|
||
first = *(int *)stack->content; | ||
second = *(int *)stack->next->content; | ||
third = *(int *)stack->next->next->content; | ||
if (first == 2 && second == 1 && third == 3) | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
else if (first == 3 && second == 2 && third == 1) | ||
{ | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
} | ||
else if (first == 3 && second == 1 && third == 2) | ||
ft_putendl_fd("ra", STDOUT_FILENO); | ||
else if (first == 1 && second == 3 && third == 2) | ||
{ | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
ft_putendl_fd("ra", STDOUT_FILENO); | ||
} | ||
else if (first == 2 && second == 3 && third == 1) | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
} | ||
|
||
void nano_sort(t_list *stack, int argc) | ||
{ | ||
int num_of_element; | ||
|
||
num_of_element = argc - 1; | ||
if (num_of_element == 2) | ||
_handle_2(stack); | ||
else if (num_of_element == 3) | ||
_handel_3(stack); | ||
} |
Oops, something went wrong.