DEV/DEV1.1/TP20/TP20_reponses.txt

92 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2024-12-02 14:12:49 +01:00
----- TP20 : Structures -----
1.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void) {
time_t timestamp = time(NULL);
struct tm* date = localtime(&timestamp);
printf("%04d/%02d/%02d", date->tm_year+1900, date->tm_mon+1, date->tm_mday);
}
2.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct enregistrement {
char a;
char b;
char c;
};
int main (void) {
struct enregistrement test = {'Z', 'i', 'o'};
printf("Taille du 1er élément : %zu \nTaille du 2e élément : %zu \nTaille du 3e élément : %zu \n", sizeof(test.a), sizeof(test.b), sizeof(test.c));
}
3.
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
int main (void) {
struct passwd* id = getpwnam("simoes");
printf("UID : %d\n", id->pw_uid);
}
4.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct imaginaire {
int x;
int y;
};
struct imaginaire conjugue(struct imaginaire z) {
struct imaginaire z_conjugue;
z_conjugue.x = z.x;
z_conjugue.y = z.y * -1;
return z_conjugue;
}
double module(struct imaginaire z) {
return sqrt((z.x*z.x) + (z.y*z.y));
}
void afficher_imaginaire(struct imaginaire z) {
if (z.y < 0) {
printf("%d - %di", z.x, z.y*-1);
}
else {
printf("%d + %di", z.x, z.y);
}
putchar('\n');
}
void inverse(struct imaginaire z) {
printf("1 / ");
afficher_imaginaire(z);
}
int main (void) {
struct imaginaire test = {4, 8};
afficher_imaginaire(test);
afficher_imaginaire(conjugue(test));
printf("%f\n",module(test));
inverse(test);
}