TP05 Fichiers

This commit is contained in:
HORVILLE 2021-12-07 15:08:24 +01:00
parent 4986d63d32
commit bb28a80730
11 changed files with 1059 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
int found = 0;
FILE* flux = fopen("rgb.txt", "r");
int r, g, b;
char name[100];
while (!feof(flux)) {
fscanf(flux, "%d %d %d\t\t%99[^\n]", &r, &g, &b, &name);
if (strcmp(name, argv[1]) == 0) {
found++;
printf("Color found : \x1B[38;2;%d;%d;%dm%d, %d, %d\x1B[37m.\n", r, g, b, r, g, b, name);
}
}
if (!found) puts("No color found...");
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,28 @@
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char * argv[]) {
int r, g, b;
r = strtod(argv[1], NULL);
g = strtod(argv[2], NULL);
b = strtod(argv[3], NULL);
int found = 0;
FILE* flux = fopen("rgb.txt", "r");
int r2, g2, b2;
char name[100];
while (!feof(flux)) {
fscanf(flux, "%d %d %d\t\t%99[^\n]", &r2, &g2, &b2, &name);
if (r == r2 && g == g2 && b == b2) {
found++;
printf("Color found : \x1B[38;2;%d;%d;%dm%s\x1B[37m.\n", r, g, b, name);
}
}
if (!found) puts("No color found...");
return EXIT_SUCCESS;
}

121
APL1.2/TP05/gomoku.c Normal file
View File

@ -0,0 +1,121 @@
#include<stdio.h>
#include<stdlib.h>
#include<graph.h>
#include"graph_sup.h"
#define TABLE_SIZE 289 /*17²*/
int* table;
void saveGame() {
FILE* flux = fopen("save.txt", "w");
for (int i = 0; i < 16; i ++) {
for (int i2 = 0; i2 < 16; i2++) {
fprintf(flux, "%d ", table[i*17+i2]);
}
fprintf(flux, "\n");
}
fclose(flux);
}
void loadGame() {
FILE* flux = fopen("save.txt", "r");
if (flux) {
for (int i = 0; i < 16; i++) {
for (int i2 = 0; i2 < 16; i2++) {
int owo;
fscanf(flux, "%d ", &owo);
table[i*17+i2] = owo;
}
fscanf(flux, "\n");
}
fclose(flux);
}
for (int i = 0; i < 16; i++) {
for (int i2 = 0; i2 < 16; i2++) {
if (table[i2*17+i] == 1) {
SetColor(255, 255, 255);
RemplirRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
SetColor(0, 0, 0);
DessinerRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
} else if (table[i2*17+i] == 2) {
RemplirRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
}
}
}
}
void showGame() {
for (int i = 0; i < 16; i ++) {
for (int i2 = 0; i2 < 16; i2++) {
printf("%d ", table[i*17+i2]);
}
printf("\n");
}
}
int main(int argc, char * argv[]) {
table = calloc(TABLE_SIZE , sizeof(int));
InitialiserGraphique();
CreerFenetre(100, 100, 700, 800);
ChoisirCouleurDessin(CouleurParNom("black"));
for (int i = 0; i < 17; i++) {
for (int i2 = 0; i2 < 17; i2++) {
DessinerRectangle(10 + 40*i, 10 + 40*i2, 40, 40);
if (i != 16 && i2 != 16) AddButton(30 + 40*i, 30 + 40*i2, 40, 40, i2*17+i);
}
}
AddButton(50, 720, 200, 50, -2); //Load
AddButton(450, 720, 200, 50, -3); //Save
DessinerRectangle(50, 720, 200, 50);
DessinerRectangle(450, 720, 200, 50);
EcrireTexte(120, 755, "Load", 2);
EcrireTexte(520, 755, "Save", 2);
int turn = 0;
do {
if (DrawNextFrame()) {
if (SourisCliquee()) {
int BT = GetButton(_X, _Y);
if (BT != -1) {
if (BT >= 0) {
if (turn == 0) {
turn = 1;
table[BT] = 1;
} else {
turn = 0;
table[BT] = 2;
}
for (int i = 0; i < 16; i++) {
for (int i2 = 0; i2 < 16; i2++) {
if (table[i2*17+i] == 1) {
SetColor(255, 255, 255);
RemplirRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
SetColor(0, 0, 0);
DessinerRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
} else if (table[i2*17+i] == 2) {
RemplirRectangle(35 + 40*i, 35 + 40*i2, 30, 30);
}
}
}
} else {
if (BT == -2) loadGame();
else if (BT == -3) saveGame();
}
}
}
}
} while (1);
FermerGraphique();
return EXIT_SUCCESS;
}

32
APL1.2/TP05/graph_sup.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef _GRAPH_SUP_H
#define _GRAPH_SUP_H
#define WIDTH 1200
#define HEIGHT 700
struct Button {
int x;
int y;
int w;
int h;
int id;
};
typedef struct Button button;
extern button* Buttons;
extern int BT_Count;
void ClearButtons();
int GetButton(int x, int y);
void AddButton(int x, int y, int w, int h, int id);
int DrawNextFrame(void);
couleur GetColorN(char* name);
couleur GetColor(unsigned char r, unsigned char g, unsigned char b);
void SetColor(unsigned char r, unsigned char g, unsigned char b);
void SetColorC(couleur Color);
void SetColorN(char* name);
#endif

View File

@ -0,0 +1,14 @@
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char * argv[]) {
FILE* flux;
flux = fopen(argv[1], "r");
fseek(flux, 0L, SEEK_END);
long size = ftell(flux);
fclose(flux);
fprintf(stdout, "La taille du fichier est de : \x1B[38;2;125;125;255m%ld\x1B[37m octets.\n", size);
return EXIT_SUCCESS;
}

753
APL1.2/TP05/rgb.txt Normal file

File diff suppressed because it is too large Load Diff

16
APL1.2/TP05/save.txt Normal file
View File

@ -0,0 +1,16 @@
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0
0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 0 1 0 1 2 0 0 0 0 2 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 2 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

2
APL1.2/TP05/t1.csv Normal file
View File

@ -0,0 +1,2 @@
1;2;3;4;5
1;2;3;4;5
1 1 2 3 4 5
2 1 2 3 4 5

3
APL1.2/TP05/t2.csv Normal file
View File

@ -0,0 +1,3 @@
1;2;3;4;5
6;7;8;9;10
11;12;13;14;15
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15

5
APL1.2/TP05/t3.csv Normal file
View File

@ -0,0 +1,5 @@
0;0;0;0;0
1;0;0;0;0
1;2;0;0;0
1;2;3;0;0
1;2;3;4;0
1 0 0 0 0 0
2 1 0 0 0 0
3 1 2 0 0 0
4 1 2 3 0 0
5 1 2 3 4 0

60
APL1.2/TP05/tableur.c Normal file
View File

@ -0,0 +1,60 @@
#include<stdio.h>
#include<stdlib.h>
//Affiche un tableau 2D, donnez la longueur / hauteur du tableau et il l'affichera
void show2dtable(int l, int c, int table[][5]) {
for (int line = 0; line < (l*2)+1; line++) {
if (line == 0 || line == (l*2)+1 || line % 2 == 0) {
for (int col = 0; col < c; col++) printf("+-----");
printf("+\n");
} else {
for (int col = 0; col < c; col++) printf("| %3d ", table[line/2][col]);
printf("|\n");
}
}
}
void writeTable(char* filename, int l, int c, int table[][5]) {
FILE* flux = fopen(filename, "w");
for (int line = 0; line < l; line++) {
for (int column = 0; column < c; column++) {
if (column+1 == c) {
fprintf(flux, "%d", table[line][column]);
} else fprintf(flux, "%d;", table[line][column]);
}
fprintf(flux, "\n");
}
fclose(flux);
}
int main(int argc, char * argv[]) {
int table1[2][5] = {};
int table2[3][5] = {};
int table3[5][5] = {};
for (int i = 0; i < 5; i++) {
table1[0][i] = i+1;
table1[1][i] = i+1;
}
for (int i = 0; i < 15; i++) {
table2[i/5][i%5] = i+1;
}
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
if (y < x) table3[x][y] = y+1;
else table3[x][y] = 0;
}
}
show2dtable(2, 5, table1);
writeTable("t1.csv", 2, 5, table1);
printf("\n");
show2dtable(3, 5, table2);
writeTable("t2.csv", 3, 5, table2);
printf("\n");
show2dtable(5, 5, table3);
writeTable("t3.csv", 5, 5, table3);
return EXIT_SUCCESS;
}