From: Jérôme Benoit Date: Tue, 14 Feb 2017 22:39:56 +0000 (+0100) Subject: Add exo2 preliminary structure X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bd8b2f2e8d06af1508d0422814ba184a610608b3;p=TD_C.git Add exo2 preliminary structure Signed-off-by: Jérôme Benoit --- diff --git a/exo2/Makefile b/exo2/Makefile new file mode 100644 index 0000000..184cdd5 --- /dev/null +++ b/exo2/Makefile @@ -0,0 +1,31 @@ +TARGET = exo2 +LIBS = +CC = gcc +# Enforce C11 ISO standard for now +CFLAGS = -std=c11 -g -Wall +LDFLAGS = -g -Wall + +.PHONY: default all clean + +default: $(TARGET) +all: default + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@ + +clean: + -rm -f $(TARGET) $(OBJECTS) + +disassemble: $(TARGET) + objdump -d $< | less + +symbols: $(TARGET) + objdump -t $< | sort | less diff --git a/exo2/exo2.c b/exo2/exo2.c new file mode 100644 index 0000000..8bb939d --- /dev/null +++ b/exo2/exo2.c @@ -0,0 +1,70 @@ +#include +//#include + +//FIXME: Comment the code !!! + +void displayMenuEntry(int num, const char* option) { + printf("%d - %s\n", num, option); +} + +void displayMenu() { + displayMenuEntry(1, "Addition"); + displayMenuEntry(2, "Soustraction"); + displayMenuEntry(3, "Multiplication"); + displayMenuEntry(4, "Division"); + displayMenuEntry(5, "Puissance"); + displayMenuEntry(6, "Quitter"); +} + +int promptValue(const char* invite) { + int value = 0; + puts(invite); + scanf("%d", &value); + return value; +} + +int promptOperation() { + displayMenu(); + return promptValue("Veuillez saisir un choix ?"); +} + +int doAddition(int val1, int val2) { + return val1 + val2; +} + +int doSubstraction(int val1, int val2) { + return val1 - val2; +} + +int doMultiplication(int val1, int val2) { + return val1 * val2; +} + +int doDivision(int val1, int val2) { + if (val2 == 0) { + printf("Division par zero !\n"); + // FIXME: I'm not very fond of this convention ... + return 0; + } else { + return val1 / val2; + } +} + +int doPuissance(int base, int expo) { + int power; + if ( expo == 1) { + power = base; + } else if ( expo % 2 == 0 ) { + power = doPuissance(base*base, expo/2); + } else { + power = base*doPuissance(base*base,(expo-1)/2); + } + return power; +} + +int main() { + int choice = promptOperation(); + printf("Choice: %d\n", choice); + + return 0; +}