Skip to content
Snippets Groups Projects
Commit 0513733e authored by Florian Burgener's avatar Florian Burgener
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
tp008
test
*.o
copy.sh
Makefile 0 → 100644
TARGET = tp008
LIBS = -lm
CC = gcc
CFLAGS = -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak
.PHONY: default clean
default: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET): main.o stack.o
$(CC) main.o stack.o $(CFLAGS) $(LIBS) -o $@
test: stack_tests.o stack.o
$(CC) stack_tests.o stack.o $(CFLAGS) $(LIBS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)
-rm -f test
main.c 0 → 100644
/**
* @file main.c
* @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Démonstration de libraire stack avec l'implémentation du tri à deux piles.
* @version 1.0
* @date 2021-11-23
*
* @copyright Copyright (c) 2021
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void sort_array(int *array, int array_length) {
stack s_a;
stack_init(&s_a, array_length);
stack s_b;
stack_init(&s_b, array_length);
for (int i = 0; i < array_length; i += 1) {
// Pop values from left stack to right stack.
while (!stack_is_empty(s_a)) {
int tmp;
stack_peek(s_a, &tmp);
if (array[i] < tmp) {
break;
}
stack_pop(&s_a, &tmp);
stack_push(&s_b, tmp);
}
// Pop values from right stack to left stack.
while (!stack_is_empty(s_b)) {
int tmp;
stack_peek(s_b, &tmp);
if (array[i] > tmp) {
break;
}
stack_pop(&s_b, &tmp);
stack_push(&s_a, tmp);
}
// Push to left stack.
stack_push(&s_a, array[i]);
}
// Until right stack is empty pop value from right stack and push it to left stack.
while (!stack_is_empty(s_b)) {
int tmp;
stack_pop(&s_b, &tmp);
stack_push(&s_a, tmp);
}
// Until the left stack is empty, pop the value and place it in the array.
for (int i = 0; i < array_length; i += 1) {
int tmp;
stack_pop(&s_a, &tmp);
array[i] = tmp;
}
stack_destroy(&s_a);
stack_destroy(&s_b);
}
void print_array(int *array, int array_length) {
for (int i = 0; i < array_length; i += 1) {
printf("%d, ", array[i]);
}
printf("\n");
}
int main(int argc, char **argv) {
int array_length = atoi(argv[1]);
if (array_length != argc - 2) {
printf("The length of the array does not correspond to the number of elements.\n");
return EXIT_FAILURE;
}
int *array = (int *)malloc(sizeof(int) * array_length);
for (int i = 0; i < argc - 2; i += 1) {
array[i] = atoi(argv[i + 2]);
}
print_array(array, array_length);
sort_array(array, array_length);
print_array(array, array_length);
free(array);
return EXIT_SUCCESS;
}
stack.c 0 → 100644
/**
* @file stack.c
* @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Implémentation de librairie stack.
* @version 1.0
* @date 2021-11-23
*
* @copyright Copyright (c) 2021
*
*/
#include "stack.h"
#include <stdbool.h>
#include <stdlib.h>
void stack_init(stack *s, int size) {
s->size = size;
s->top = -1;
s->data = (int *)malloc(sizeof(int) * s->size);
}
bool stack_is_empty(stack s) {
return s.top == -1;
}
void stack_push(stack *s, int val) {
if (s->size == s->top + 1) {
return;
}
s->top += 1;
s->data[s->top] = val;
}
void stack_pop(stack *s, int *val) {
stack_peek(*s, val);
s->top -= 1;
}
void stack_peek(stack s, int *val) {
*val = s.data[s.top];
}
void stack_destroy(stack *s) {
s->size = -1;
s->top = -1;
free(s->data);
s->data = NULL;
}
stack.h 0 → 100644
/**
* @file stack.h
* @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Header de librairie stack.
* @version 1.0
* @date 2021-11-23
*
* @copyright Copyright (c) 2021
*
*/
#ifndef STACK_HEADER
#define STACK_HEADER
#include <stdbool.h>
typedef struct _stack {
int size;
int top;
int *data;
} stack;
void stack_init(stack *s, int size);
bool stack_is_empty(stack s);
void stack_push(stack *s, int val);
void stack_pop(stack *s, int *val);
void stack_peek(stack s, int *val);
void stack_destroy(stack *s);
#endif
/**
* @file stack_tests.c
* @author Florian Burgener (florian.burgener@etu.hesge.ch) et Quentin Fasler
* @brief Seul les tests ont été fait en commun pour gagner du temps.
* @version 1.0
* @date 2021-11-23
*
* @copyright Copyright (c) 2021
*
*/
#include "stack.h"
#include <assert.h>
#include <stdlib.h>
int main() {
const int STACK_LENGTH = 10;
// Test : stack_init
stack s1;
stack_init(&s1, STACK_LENGTH);
assert(s1.size == 10);
assert(s1.top == -1);
assert(s1.data != NULL);
// Test : stack_destroy
stack_destroy(&s1);
assert(s1.size == -1);
assert(s1.top == -1);
assert(s1.data == NULL);
// Test : stack_push
stack s2;
stack_init(&s2, STACK_LENGTH);
stack_push(&s2, 1);
assert(s2.data[0] == 1);
assert(s2.top == 0);
stack_push(&s2, 10);
assert(s2.data[1] == 10);
assert(s2.top == 1);
stack_push(&s2, 100);
assert(s2.data[2] == 100);
assert(s2.top == 2);
stack_destroy(&s2);
// Test : stack_is_empty
stack s3;
stack_init(&s3, STACK_LENGTH);
assert(stack_is_empty(s3));
stack_push(&s3, 1);
assert(!stack_is_empty(s3));
stack_destroy(&s3);
// Test : stack_push
stack s4;
stack_init(&s4, STACK_LENGTH);
stack_push(&s4, 1);
stack_push(&s4, 10);
int val1;
stack_pop(&s4, &val1);
assert(val1 == 10);
assert(s4.top == 0);
stack_pop(&s4, &val1);
assert(val1 == 1);
assert(s4.top == -1);
stack_destroy(&s4);
// Test : stack_peek
stack s5;
stack_init(&s5, STACK_LENGTH);
stack_push(&s5, 1);
int val2;
stack_peek(s5, &val2);
assert(val2 == 1);
assert(s5.top == 0);
stack_destroy(&s5);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment