]> Untitled Git - axy/ft/pushswap.git/commitdiff
Finished up
authorCorentin Lefrere <clefrere@k0r3p1.42mulhouse.fr>
Thu, 18 Dec 2025 16:53:01 +0000 (17:53 +0100)
committerCorentin Lefrere <clefrere@k0r3p1.42mulhouse.fr>
Thu, 18 Dec 2025 16:53:01 +0000 (17:53 +0100)
Makefile
main_pushswap.c
main_pushswap.h [new file with mode: 0644]
output_disorder.c [new file with mode: 0644]
pushswap.h
pushswap_args.c [new file with mode: 0644]

index fbe903f75aea063cc8a812b3a154397bd6c906bb..120913801ae748ee00d629968b6625b6ff3d17ba 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ NAME=push_swap
 
 NAME_BONUS=checker
 
-SRCS=algorithm_complex.c algorithm_medium.c algorithm_simple.c algorithm_stdin.c cheatalloc.c clist.c closure.c disorder.c eoutput.c frac.c ft_memcpy.c get_next_op.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c numutils.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c psval.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c
+SRCS=algorithm_complex.c algorithm_medium.c algorithm_simple.c algorithm_stdin.c cheatalloc.c clist.c closure.c disorder.c eoutput.c frac.c ft_memcpy.c get_next_op.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c numutils.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c output_disorder.c psval.c pushswap_args.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c
 
 RESSRCS=_res_leaf_sort_lookup.h
 
index 1669b9213b8b3dce5663a6e87a0306c5a6d30aad..9209c64bf560cb8df262dc99715e5164b976bc28 100644 (file)
@@ -6,97 +6,11 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 22:15:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/18 16:12:13 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/18 17:44:57 by clefrere         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "pushswap.h"
-
-static bool    ft_streq(char *str1, char *str2)
-{
-       size_t  i;
-
-       i = 0;
-       while (str1[i] && str2[i] && str1[i] == str2[i])
-               i++;
-       return (str1[i] == '\0' && str2[i] == '\0');
-}
-
-typedef enum   e_algovar
-{
-       ALGORITHM_SIMPLE,
-       ALGORITHM_MEDIUM,
-       ALGORITHM_COMPLEX,
-       ALGORITHM_ADAPTIVE,
-}      t_algovar;
-
-typedef struct s_args
-{
-       t_stacks        state;
-       t_algovar       algo;
-       bool            bench;
-}      t_args;
-
-static t_algorithm     *algorithm_get(t_algovar var, t_frac disorder)
-{
-       if (var == ALGORITHM_SIMPLE)
-               return (&algorithm_simple);
-       if (var == ALGORITHM_MEDIUM)
-               return (&algorithm_medium);
-       if (var == ALGORITHM_COMPLEX)
-               return (&algorithm_complex);
-       if (frac_lt(disorder, frac_new(1, 5)))
-               return (&algorithm_simple);
-       if (frac_lt(disorder, frac_new(1, 2)))
-               return (&algorithm_medium);
-       return (&algorithm_complex);
-}
-
-#define COMPLEXITY_SIMPLE "Simple / O(N^2)"
-#define COMPLEXITY_MEDIUM "Medium / O(N^(3/2))"
-#define COMPLEXITY_COMPLEX "Complex / O(N log N)"
-#define COMPLEXITY_ADAPTIVE_LOW "Adaptive / O(N^2)"
-#define COMPLEXITY_ADAPTIVE_MEDIUM "Adaptive / O(N^(3/2))"
-#define COMPLEXITY_ADAPTIVE_HIGH "Adaptive / O(N log N)"
-
-static const char      *complexity_get(t_algovar var, t_frac disorder)
-{
-       if (var == ALGORITHM_SIMPLE)
-               return (COMPLEXITY_SIMPLE);
-       if (var == ALGORITHM_MEDIUM)
-               return (COMPLEXITY_MEDIUM);
-       if (var == ALGORITHM_COMPLEX)
-               return (COMPLEXITY_COMPLEX);
-       if (frac_lt(disorder, frac_new(1, 5)))
-               return (COMPLEXITY_ADAPTIVE_LOW);
-       if (frac_lt(disorder, frac_new(1, 2)))
-               return (COMPLEXITY_ADAPTIVE_MEDIUM);
-       return (COMPLEXITY_ADAPTIVE_HIGH);
-}
-
-static void    arg_step(char *str, t_args *arg)
-{
-       if (ft_streq(str, "--simple"))
-               arg->algo = ALGORITHM_SIMPLE;
-       else if (ft_streq(str, "--medium"))
-               arg->algo = ALGORITHM_MEDIUM;
-       else if (ft_streq(str, "--complex"))
-               arg->algo = ALGORITHM_COMPLEX;
-       else if (ft_streq(str, "--adaptive"))
-               arg->algo = ALGORITHM_ADAPTIVE;
-       else if (ft_streq(str, "--bench"))
-               arg->bench = true;
-       else
-               stack_push_back(&arg->state.a, psval_parse(str));
-}
-
-typedef struct s_benchres
-{
-       t_ops_count     pre_opt;
-       t_ops_count     post_opt;
-       const char      *strategy;
-       t_frac          disorder;
-}      t_benchres;
+#include "main_pushswap.h"
 
 static t_benchres      pushswap_solve(t_args *args)
 {
@@ -141,6 +55,9 @@ static void  output_ops(t_ops_count ops)
 static void    output_bench(t_benchres res)
 {
        eoutput_str_ln("[Bench]");
+       eoutput_str("\tDisorder:\t");
+       output_disorder(&res.disorder);
+       eoutput_str_ln("%");
        eoutput_str("\tStrategy:\t");
        eoutput_str_ln(res.strategy);
        eoutput_str("\tPost-Opt:\t");
@@ -149,7 +66,6 @@ static void  output_bench(t_benchres res)
        output_ops(res.pre_opt);
 }
 
-#include <stdio.h>
 int    main(int argc, char **argv)
 {
        t_args          args;
@@ -163,7 +79,8 @@ int  main(int argc, char **argv)
        while (i < argc)
                arg_step(argv[i++], &args);
        res = pushswap_solve(&args);
-       output_bench(res);
+       if (args.bench)
+               output_bench(res);
        output_flush();
        eoutput_flush();
        cheatexit(!stacks_is_solved(&args.state));
diff --git a/main_pushswap.h b/main_pushswap.h
new file mode 100644 (file)
index 0000000..9b31970
--- /dev/null
@@ -0,0 +1,52 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main_pushswap.h                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/18 17:32:58 by clefrere          #+#    #+#             */
+/*   Updated: 2025/12/18 17:48:02 by clefrere         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef MAIN_PUSHSWAP_H
+# define MAIN_PUSHSWAP_H
+
+# define COMPLEXITY_SIMPLE "Simple / O(N^2)"
+# define COMPLEXITY_MEDIUM "Medium / O(N^(3/2))"
+# define COMPLEXITY_COMPLEX "Complex / O(N log N)"
+# define COMPLEXITY_ADAPTIVE_LOW "Adaptive / O(N^2)"
+# define COMPLEXITY_ADAPTIVE_MEDIUM "Adaptive / O(N^(3/2))"
+# define COMPLEXITY_ADAPTIVE_HIGH "Adaptive / O(N log N)"
+
+# include "pushswap.h"
+
+typedef enum e_algovar
+{
+       ALGORITHM_SIMPLE,
+       ALGORITHM_MEDIUM,
+       ALGORITHM_COMPLEX,
+       ALGORITHM_ADAPTIVE,
+}      t_algovar;
+
+typedef struct s_args
+{
+       t_stacks        state;
+       t_algovar       algo;
+       bool            bench;
+}      t_args;
+
+typedef struct s_benchres
+{
+       t_ops_count     pre_opt;
+       t_ops_count     post_opt;
+       const char      *strategy;
+       t_frac          disorder;
+}      t_benchres;
+
+t_algorithm    *algorithm_get(t_algovar var, t_frac disorder);
+const char     *complexity_get(t_algovar var, t_frac disorder);
+void           arg_step(char *str, t_args *arg);
+
+#endif
\ No newline at end of file
diff --git a/output_disorder.c b/output_disorder.c
new file mode 100644 (file)
index 0000000..6b30d2a
--- /dev/null
@@ -0,0 +1,36 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   output_disorder.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/18 15:55:08 by clefrere          #+#    #+#             */
+/*   Updated: 2025/12/18 17:31:47 by clefrere         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+void   output_disorder(t_frac *frac)
+{
+       size_t  nbr;
+       size_t  div;
+       bool    print;
+
+       div = 10000;
+       nbr = 0;
+       if (frac->den != 0)
+               nbr = frac->num * 10000 / frac->den;
+       print = false;
+       while (div)
+       {
+               if (div == 100 || nbr >= div)
+                       print = true;
+               if (div == 10)
+                       eoutput_char('.');
+               if (print)
+                       eoutput_char(((nbr / div) % 10) + '0');
+               div /= 10;
+       }
+}
index d5794850fbf8ac29a377ae66ac7d2d1fbdc1e898..e8dc26e1854a928527041c8a17b4abea11f9dce6 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 11:02:44 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/18 16:04:35 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/18 17:47:21 by clefrere         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -194,4 +194,6 @@ void                        *ft_memcpy(void *restrict dest, const void *src, size_t n);
 
 bool                   get_next_op(t_op *res);
 
+void                   output_disorder(t_frac *frac);
+
 #endif
diff --git a/pushswap_args.c b/pushswap_args.c
new file mode 100644 (file)
index 0000000..1be7a6e
--- /dev/null
@@ -0,0 +1,69 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   pushswap_args.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/18 17:36:59 by clefrere          #+#    #+#             */
+/*   Updated: 2025/12/18 17:41:18 by clefrere         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "main_pushswap.h"
+
+static bool    ft_streq(char *str1, char *str2)
+{
+       size_t  i;
+
+       i = 0;
+       while (str1[i] && str2[i] && str1[i] == str2[i])
+               i++;
+       return (str1[i] == '\0' && str2[i] == '\0');
+}
+
+t_algorithm    *algorithm_get(t_algovar var, t_frac disorder)
+{
+       if (var == ALGORITHM_SIMPLE)
+               return (&algorithm_simple);
+       if (var == ALGORITHM_MEDIUM)
+               return (&algorithm_medium);
+       if (var == ALGORITHM_COMPLEX)
+               return (&algorithm_complex);
+       if (frac_lt(disorder, frac_new(1, 5)))
+               return (&algorithm_simple);
+       if (frac_lt(disorder, frac_new(1, 2)))
+               return (&algorithm_medium);
+       return (&algorithm_complex);
+}
+
+const char     *complexity_get(t_algovar var, t_frac disorder)
+{
+       if (var == ALGORITHM_SIMPLE)
+               return (COMPLEXITY_SIMPLE);
+       if (var == ALGORITHM_MEDIUM)
+               return (COMPLEXITY_MEDIUM);
+       if (var == ALGORITHM_COMPLEX)
+               return (COMPLEXITY_COMPLEX);
+       if (frac_lt(disorder, frac_new(1, 5)))
+               return (COMPLEXITY_ADAPTIVE_LOW);
+       if (frac_lt(disorder, frac_new(1, 2)))
+               return (COMPLEXITY_ADAPTIVE_MEDIUM);
+       return (COMPLEXITY_ADAPTIVE_HIGH);
+}
+
+void   arg_step(char *str, t_args *arg)
+{
+       if (ft_streq(str, "--simple"))
+               arg->algo = ALGORITHM_SIMPLE;
+       else if (ft_streq(str, "--medium"))
+               arg->algo = ALGORITHM_MEDIUM;
+       else if (ft_streq(str, "--complex"))
+               arg->algo = ALGORITHM_COMPLEX;
+       else if (ft_streq(str, "--adaptive"))
+               arg->algo = ALGORITHM_ADAPTIVE;
+       else if (ft_streq(str, "--bench"))
+               arg->bench = true;
+       else
+               stack_push_back(&arg->state.a, psval_parse(str));
+}