feat: Initial commit

This commit is contained in:
2024-03-22 17:14:57 +01:00
parent dc83234df1
commit db9bafe755
45 changed files with 1996 additions and 0 deletions

110
Laboratorio 2/esercizio2.c Normal file
View File

@@ -0,0 +1,110 @@
#include <stdio.h>
int comprimi(FILE *fin, FILE *fout) {
char lastChar = 0, currChar; // sono sicuro che il file non inizi con un null character
int count = 0;
int charCount = 0;
while (!feof(fin))
{
currChar = fgetc(fin);
if (feof(fin))
break;
if (currChar != lastChar || count == 10) {
if (count > 1) {
fputc('$', fout);
fputc('0'+count-1, fout);
charCount += 2;
}
count = 1;
lastChar = currChar;
fputc(currChar, fout);
charCount++;
} else {
count++;
}
}
if (count > 1) { // Controllo se devo scrivere le ripetizioni dell'ultimo carattere
fputc('$', fout);
fputc('0'+count-1, fout);
charCount += 2;
}
return charCount;
}
int decomprimi(FILE *fin, FILE *fout) {
char lastChar, currChar;
int count, i;
int charCount = 0;
while (!feof(fin))
{
currChar = fgetc(fin);
if (feof(fin))
break;
if (currChar == '$') {
count = fgetc(fin) - '0';
for (i = 0; i < count; i++)
fputc(lastChar, fout);
charCount += count;
} else {
lastChar = currChar;
fputc(currChar, fout);
charCount++;
}
}
return charCount;
}
int main()
{
FILE *fp_read, *fp_write;
char operation;
puts("Inserisci 'c' per comprimere o qualsiasi altro carattere per decomprimere: ");
scanf(" %c", &operation);
if (operation == 'c') {
fp_read = fopen("./sorgente.txt", "r");
fp_write = fopen("./compresso.txt", "w");
} else {
fp_read = fopen("./compresso.txt", "r");
fp_write = fopen("./decompresso.txt", "w");
}
if (fp_read == NULL)
{
printf("Error opening input file\n");
return 1;
}
if (fp_write == NULL)
{
printf("Error opening output file\n");
return 2;
}
if (operation == 'c') {
comprimi(fp_read, fp_write);
} else {
decomprimi(fp_read, fp_write);
}
fclose(fp_read);
fclose(fp_write);
return 0;
}