From ab0d95347c23697be4bc0d0857d2ca7757a0bf33 Mon Sep 17 00:00:00 2001 From: Alnotz Date: Sun, 24 Jul 2022 19:15:05 +0200 Subject: [PATCH] Code example added --- codes/codes.tex | 312 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 codes/codes.tex diff --git a/codes/codes.tex b/codes/codes.tex new file mode 100644 index 0000000..2bdbe77 --- /dev/null +++ b/codes/codes.tex @@ -0,0 +1,312 @@ +\documentclass[a4paper,11pt]{report} +\usepackage[T1]{fontenc} % Format de sortie. +\usepackage[utf8]{inputenc} % Pour écrire en UTF-8. +\usepackage[official]{eurosym} % Pour la monnaie euro : \euro{}. +\DeclareUnicodeCharacter{20AC}{\euro{}} % Pour bien taper €. +\usepackage[francais]{babel} % Pour adapter en français. +\usepackage{xcolor} % Pour la couleur. +\usepackage{amsmath} % Pour les maths. +\usepackage{amssymb} % Symboles de maths. +\usepackage{hyperref} % Les hyperliens. +\hypersetup{ + backref=true, %permet d'ajouter des liens dans... + pagebackref=true, %...les bibliographies + hyperindex=true, %ajoute des liens dans les index. + colorlinks=true, %colorise les liens + breaklinks=true, %permet le retour à la ligne dans les liens trop longs + urlcolor= blue, %couleur des hyperliens + linkcolor= blue, %couleur des liens internes + bookmarks=true, %créé des signets pour Acrobat + bookmarksopen=true, %si les signets Acrobat sont créés, + %les afficher complètement. + linktocpage=true, %lier la table des matières. + pdftitle={Code C}, %informations apparaissant dans + pdfauthor={Alnotz}, %dans les informations du document + pdfsubject={Code C} %sous Acrobat. +} +\usepackage{minted} % Pour un bon bloc de code. + % Ne pas oublier la commande + % "pdflatex -shell-escape". +\newcommand{\oneone}{1\!\!1} % Opérateur identité. +\newcommand{\gui}[1]{{\og}#1{\fg}} % Guillemets français. +\def\TikZ{Ti{\color{orange}\textit{k}}Z} +\date{\today} % "\title{Un document-test}" si on veut plus simple. +\title{ + \begin{minipage}\linewidth + \centering\bfseries\sffamily +Titre + \vskip3pt + \large Sous-titre + \end{minipage} +} +\author{Alnotz} +\begin{document} + \maketitle + \chapter{Le commencement} + Un peu de code C ? + \begin{center} + \begin{minted}{c} + +/* + * Fichier : tp02.c + * Auteur : Alnotz + * Diplôme : Lpro Vision robotique + * Date : 28/09/2018 + * Version : 0.1 + */ +/* En-tête : Bibliothèque standard pour I/O. */ +#include +/* Fonction secondaire : H + 1 seconde. + * On demande le temps et renvoie le temps une seconde plus tard. + */ +void exo09(void) +{ + unsigned int seconde ; + unsigned int minute ; + unsigned int heure ; + printf("Exercise 09.\n\nYou have to give the time to know the") ; + printf(" future time after 1 second.\n\nGive the hour.\n") ; + printf("Hour :\t\t") ; + /* Temps demandé. */ + scanf("%u", &heure) ; + printf("\nGive the minute.\nMinute :\t") ; + scanf("%u", &minute) ; + printf("\nGive the second.\nSecond :\t") ; + scanf("%u", &seconde) ; + printf("\nActual time :\t%u:%u'%u\"\n\n", heure, minute, seconde) ; + /* Une seconde est passée. */ + seconde = seconde + 1 ; + if (seconde >= 60) + { + minute = minute + seconde/60 ; + seconde = seconde % 60 ; + } + if (minute >= 60) + { + heure = heure + minute/60 ; + minute = minute % 60 ; + } + /* Résultat. */ + printf("One second later, :\n") ; + printf("New time :\t%u:%u'%u\"\n", heure, minute, seconde) ; +} +/* Fonction secondaire : reprographie. + * Dans un magasin, on vend des articles. + * Les 10 premiers exemplaires consécutifs valent 0.10 €. + * Les 20 suivants valent 0.09 €. + * Ceux qui suivent valent 0.08 €. + */ +void exo10(void) +{ + /* Nombre d'articles. */ + unsigned int nombre ; + /* Facture à payer. */ + unsigned int facture = 0 ; + printf("Exercise 10.\n\nYou buy many same items.You have to ") ; + printf("give the number of items to know how number to pay.\n\n") ; + printf("How many do you buy ?\nItem number :\t") ; + scanf("%u", &nombre) ; + /* On calcule le coût total. */ + if (nombre <= 10) + { + nombre = nombre * 10 ; + } + if (nombre <= 30) + { + nombre = 10 + (nombre-10) * 9 ; + } + if (nombre > 30 ) + { + nombre = 10 + 180 + (nombre-30) * 8 ; + } + /* Affichage du coût. */ + printf("\nYou have to pay %u.%u euro.", (nombre - (nombre%100)) / 100, nombre % 100 ) ; +} +/* Fonction secondaire : imposition. + * On demande quel est le sexe et l'âge d'un individu pour + * indiquer s'il est imposable + * Les hommes (m) de plus de 20 ans et plus sont imposables. + * Les femmes (w) de 18 à 35 ans sont imposables. + */ +void exo11(void) +{ + /* Sexe du villagois : homme(m) ou femme(w). */ + unsigned int sexe ; + /* Âge du villagois : nombre entier naturel. */ + unsigned int age ; + printf("Exercise 11.\n\nOne needs age and sex of the villager to") ; + printf(" know its imposability.\nWhat is the sex : '0' for man or '1' for woman ?\n\n") ; + printf("Sex :\t") ; + scanf("%u", &sexe) ; + printf("\n") ; + /* On vérifie la réponse donnée pour le sexe. */ + while (sexe != 0 && sexe != 1) + { + printf("Sorry, you must give '0' or '1'.\n") ; + printf("What is the sex : '0' for man or '1' for woman ?\n\n") ; + printf("Sex :\t") ; + scanf("%u", &sexe) ; + printf("\n") ; + } + printf("How many year old is the villager ?\n\nYear number :\t") ; + scanf("%u", &age) ; + printf("\n") ; + /* On détermine si le villagois est imposable. */ + if ((sexe=='m' && age>=20) || (sexe=='w' && age>=18 && age<=35)) + { + printf("The villager is imposable.\n") ; + } + else + { + printf("The villager is not imposable.\n") ; + } +} +/* Fonction secondaire : élections. + * On a quatre candidats. Après avoir indiqué leurs part de + * suffage en %, on détermine si le premier candidat est élu ou non. + * On précise s'il est en ballotage favorable ou non, s'il participe + * au second tour. + * Les candidats ayant 12.5% ou plus participent au second tour. + * Le candidat ayant plus de 50% est élu. + */ +void exo12(void) +{ + /* Scores respectifs des 4 candidats. */ + unsigned short score[4] ; + /* Score total. */ + unsigned long total = 0 ; + unsigned short i ; + printf("Exercise 12.\n\nOne needs to know the score for 4 candidates.") ; + printf("\nYou'll give the score of each. After, one will show if the ") ; + printf(" first candidate is elected, not elected or in a second tour.\n\n") ; + /* On collecte les suffrages. */ + for(i=0; i<4; i=i+1) + { + printf("Give the score of candidat #%hu\nCandidat #%hu :\t", i, i) ; + scanf("%hu", &score[i]) ; + printf("\n") ; + /* Précision au 100ème près. */ + /* Les taux ne seront pas affectés. */ + total = total + score[i]*100 ; + } + /* On vérifie si le premier candidat gagne au premier tour. */ + if (score[0]*100 >= total/2) + { + printf("The first candidate is elected.\n") ; + } + /* Si non, perd-il les élections ? */ + else if + ( + score[0]*100 < total/8 || + score[1]*100 >= total/2 || + score[2]*100 >= total/2 || + score[3]*100 >= total/2 + ) + { + printf("The first candidate is not elected.\n") ; + } + /* ... Ou est-ce juste qu'il doit faire un second tour avec + * un ballottage favorable ? + */ + else if + ( + score[0]*100 >= score[1] && + score[0]*100 >= score[2] && + score[0]*100 >= score[3] + ) + { + printf("The first candidate is not elected but can") ; + printf(" participate to an favorable second tour.\n") ; + } + /* Bon, dans ce cas, il est au second tour avec un ballottage + * qui n'est pas favorable. + */ + else + { + printf("The first candidate is not elected but can") ; + printf(" participate to an no favorable second tour.\n") ; + } +} +/* Fonction secondaire : tarification assurance. + * On propose au client quatre niveaux de tarifs. + * Moins de 25 ans abaisse le niveau. + * Moins de 2 ans de permis abaisse le niveau. + * Chaque accident abaisse le niveau. + * Si le niveau est abaissé plus de 3 fois, + * le client est refusé. + * Le client doit alors indiquer : + * -- son âge. + * -- combien d'années il a son permis de conduire. + * -- le nombre d'accidents. + */ +void exo13(void) +{ + /* Âge du client. */ + unsigned short age ; + /* Nombre d'années après obtention du permis. */ + unsigned short permis ; + /* Nombre d'accidents. */ + unsigned short accident ; + /* Les défauts du client. */ + unsigned short defaut = 0 ; + printf("Exercise 13.\n\nOne may offer you 4 fares :\n") ; + printf("-- Green fare : the best !\n") ; + printf("-- Yellow fare : good.\n") ; + printf("-- Orange fare : bad.\n") ; + printf("-- Red fare : very bad.\n\n") ; + printf("For that, you must give how many years old you are,\n") ; + printf(" how many years you have got your car pass\n") ; + printf(" and the car accidents number.\nGive your age.\n\n") ; + printf("Old year :\t") ; + scanf("%hu", &age) ; + if (age < 25) {defaut = defaut + 1 ;} + printf("\nGive the year number you have your pass.\n") ; + printf("Pass year :\t") ; + scanf("%hu", &permis) ; + if (permis < 2) {defaut = defaut + 1 ;} + printf("\nGive the accidents number.\nAccidents number :\t") ; + scanf("%hu", &accident) ; + if (accident > 0) {defaut = defaut + accident ;} + printf("\n") ; + /* On détermine le meilleur tarif possible. */ + /* Un sans faute ! */ + if (defaut == 0) + { + printf("Congratulation ! You have the Green fare !\n") ; + } + /* Une condition ne convient pas. */ + else if (defaut == 1) + { + printf("Good job. You have the Yellow fare.\n") ; + } + /* Un cran plus bas. */ + else if (defaut == 2) + { + printf("That's not so bad. You have the Orange fare.\n") ; + } + else if (defaut == 3) + { + printf("Better than nothing. You have the Red fare.\n") ; + } + else + { + printf("Sorry. You are too bad to have a fare.\n") ; + } +} +/* Fonction principale. */ +int main(void) +{ + printf("Welcome in tp02.\n\n") ; + /* + * exo09() ; + * exo10() ; + * exo11() ; + * exo12() ; + * exo13() ; + */ + return 0 ; +} + + \end{minted} + \end{center} +\end{document}