Created
June 20, 2018 15:05
-
-
Save mascot27/a7a369df30981ed32fea96907a80ba24 to your computer and use it in GitHub Desktop.
te2-INF2 - correction
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
// déclarations: | |
double x = 12.35; | |
int i = 123; | |
char c = 'a'; | |
int* pt = &i; | |
printf("exercice 1!\n"); | |
/* | |
* pour l'exercice 1.1 | |
*/ | |
printf("|%-8.2f|\n", x); | |
/* | |
* pour l'exercice 1.2 | |
*/ | |
printf("|%6.2f|\n", x); | |
/* | |
* pour l'exercice 1.3 | |
*/ | |
printf("|%06d|\n", i); | |
/* | |
* pour l'exercice 1.4 | |
*/ | |
scanf("%d", pt); | |
scanf("/%d/", &i); | |
scanf("%f %d %c", &x, &i, &c); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or 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
#define GENERIC_ADD(typeL, typeR) \ | |
typeL typeL##Add(typeL l, typeR r){ \ | |
return l + (typeL) r; \ | |
} | |
GENERIC_ADD(int, double); | |
GENERIC_ADD(double, int) |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
// déclarations: | |
char* tab[] = {"INFO", "Test", "mai", "2018"}; | |
char* pt1 = *tab; | |
char** pt2 = tab; | |
/* | |
* Exercice | |
*/ | |
printf("1. %s\n", 0[tab+1]); | |
printf("2. %c\n", *tab[1]); | |
printf("3. %c\n", *pt1); | |
printf("4. %s\n", pt1+2); | |
printf("5. %s\n", ++pt1); | |
printf("6. %s\n", ++*(++pt2)); | |
printf("7. %s\n", *(pt2+2)); | |
printf("8. %s\n", *pt2); | |
printf("9. %c\n", (char) (*(pt2[-1])+1)); | |
printf("10. %d\n", ++pt2-tab); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
// déclarations: | |
const in | |
t constInt = 3; | |
int varInt = 12; | |
float varFloat = 3.14f; | |
const int tab[10] = {0, 1, 2, 3, 4, 5, 6}; | |
/* | |
* Exercice 4 -te2 (INF2) GMB mai 2018 | |
*/ | |
// 1) déclarer un pointeur constant sur la constante "constInt" | |
int* const ptrConstInt = &constInt; | |
// 2) Déclarer un pointeur sur une constante entière initilisé à NULL | |
// puis affecter ce pointeur sur varInt | |
const int* ptrConstInt2 = NULL; | |
ptrConstInt2 = &varInt; | |
// 3) Par le biais du pointeur précédent, modifier la valeur pointé à 12 | |
*(int*) ptrConstInt2 = 12; | |
// 4) déclarer un pointeur constant pointant sur "varInt" | |
int* const ptr3 = &varInt; | |
// 5) déclarer un pointeur void pointant sur "varInt" | |
void* ptrVoid = (void*) &varInt; | |
// 6) afficher la valeur de varInt par le biais du pointeur | |
printf("%d", *(int*)ptrVoid); | |
// 7) déclarer un pointeur sur le début du tableau | |
int* ptrTab = tab[0]; | |
// 8) Déclarer un pointeur sur l'identificateur tab | |
int* ptrTab2 = tab; | |
// 9) Déclarer un pointeur sur le tableau "tab" de 10 entiers | |
const int (*ptrTab3) [10] = &tab; | |
// 10) ? | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or 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
#include <assert.h> | |
#include <string.h> | |
#include <stdlib.h> | |
const int* merge(const int* tab1, const int* tab2, size_t tab1Size, size_t tab2Size){ | |
assert(tab1); | |
assert(tab2); | |
int* newTab = (int*) calloc(tab1Size + tab2Size, sizeof(int)); | |
if(!newTab){ | |
return NULL; | |
} | |
// constantes | |
const int* tabLong = tab1Size>tab2Size ? tab1 : tab2; | |
const size_t tailleMin = tab1Size<tab2Size ? tab1Size : tab2Size; | |
const size_t tailleMax = tab1Size>tab2Size ? tab1Size : tab2Size; | |
// recopie des plages communes | |
for(size_t i=0; i < tailleMin; ++i){ | |
*(newTab+2*i) = *(tab1+i); | |
*(newTab+(2*i)+1) = *(tab2+i); | |
} | |
// copie du reste du tableau le plus long | |
memcpy(newTab+2*tailleMin, tabLong+tailleMin, sizeof(int) * (tailleMax-tailleMin)); | |
} |
This file contains hidden or 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
#include <assert.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
bool nextPtr(const int* tab, const int** ptr, size_t size){ | |
if(!tab || !ptr || !size || *ptr-tab < 0 || *ptr-tab > size){ | |
return false; | |
} | |
int start = *ptr - tab; | |
for(int i = start+1; i < size; ++i){ | |
if(**ptr == tab[i]){ | |
*ptr = &tab[i]; | |
return true; | |
} | |
} | |
*ptr = NULL; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment