feat: Initial commit
This commit is contained in:
43
Laboratorio 1/EsempioIO.c
Normal file
43
Laboratorio 1/EsempioIO.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
FILE *fp_read, *fp_write;
|
||||
char file_char, choice;
|
||||
if ((fp_read = fopen("../Guide.txt", "r")) == NULL)
|
||||
{
|
||||
printf("Error opening file\n");
|
||||
return 1;
|
||||
}
|
||||
if ((fp_write = fopen("../Output.txt", "w")) == NULL)
|
||||
{
|
||||
printf("Error opening file\n");
|
||||
return 2;
|
||||
}
|
||||
printf("Print on console (C) or on file (F):");
|
||||
choice = getchar();
|
||||
while (!feof(fp_read))
|
||||
{
|
||||
file_char = fgetc(fp_read);
|
||||
if (!feof(fp_read))
|
||||
{
|
||||
switch (choice)
|
||||
{
|
||||
case 'C':
|
||||
printf("\nChar printed on the console: %c",
|
||||
file_char);
|
||||
break;
|
||||
case 'F':
|
||||
fputc(file_char, fp_write);
|
||||
printf("\nChar saved on file: ");
|
||||
putchar(file_char);
|
||||
break;
|
||||
default:
|
||||
printf("Wrong choice\n");
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(fp_read);
|
||||
fclose(fp_write);
|
||||
return 0;
|
||||
}
|
||||
4
Laboratorio 1/Operations.txt
Normal file
4
Laboratorio 1/Operations.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
+ 15.225 30.51
|
||||
- 42.1 10.01
|
||||
* 0.62 2.4
|
||||
/ 5.0 2.5
|
||||
4
Laboratorio 1/Results.txt
Normal file
4
Laboratorio 1/Results.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
+ 45.74
|
||||
- 32.09
|
||||
* 1.49
|
||||
/ 2.00
|
||||
12
Laboratorio 1/TestAmbiente.c
Normal file
12
Laboratorio 1/TestAmbiente.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int x, y;
|
||||
float z;
|
||||
printf("Insert an integer number:");
|
||||
scanf("%d", &x);
|
||||
y = 3;
|
||||
z = (float)(x) / y;
|
||||
printf("%d/%d=%.3f\n", x, y, z);
|
||||
return 0;
|
||||
}
|
||||
40
Laboratorio 1/calcolaaree.c
Normal file
40
Laboratorio 1/calcolaaree.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#define P 3.14
|
||||
|
||||
int main()
|
||||
{
|
||||
char fig;
|
||||
char dato;
|
||||
float area;
|
||||
float num;
|
||||
scanf("%c %c%f", &fig, &dato, &num);
|
||||
|
||||
if (fig == 'Q')
|
||||
{
|
||||
if (dato == 'D')
|
||||
{
|
||||
area = num * num / 2;
|
||||
}
|
||||
else if (dato == 'L')
|
||||
{
|
||||
area = num * num;
|
||||
}
|
||||
|
||||
printf("Area quadrato = %f\n", area);
|
||||
}
|
||||
else if (fig == 'C')
|
||||
{
|
||||
if (dato == 'D')
|
||||
{
|
||||
area = P * num * num / 4;
|
||||
}
|
||||
else if (dato == 'R')
|
||||
{
|
||||
area = P * num * num;
|
||||
}
|
||||
|
||||
printf("Area cerchio = %f\n", area);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
43
Laboratorio 1/calcolatrice.c
Normal file
43
Laboratorio 1/calcolatrice.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
char operation;
|
||||
float op1, op2, res;
|
||||
|
||||
operation = getchar();
|
||||
if (scanf("%f %f", &op1, &op2) != 2) {
|
||||
printf("Error: Invalid input\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
switch (operation)
|
||||
{
|
||||
case '+':
|
||||
res = op1 + op2;
|
||||
break;
|
||||
|
||||
case '-':
|
||||
res = op1 - op2;
|
||||
break;
|
||||
|
||||
case '*':
|
||||
res = op1 * op2;
|
||||
break;
|
||||
|
||||
case '/':
|
||||
if (op2 == 0) {
|
||||
printf("Error: divide by zero\n");
|
||||
return 2;
|
||||
}
|
||||
res = op1 / op2;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Error: invalid operation\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("%c %f\n", operation, res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
61
Laboratorio 1/calcolatricefile.c
Normal file
61
Laboratorio 1/calcolatricefile.c
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
|
||||
FILE *fp_read, *fp_write;
|
||||
char operation;
|
||||
float op1, op2, res;
|
||||
|
||||
if ((fp_read = fopen("./Operations.txt", "r")) == NULL)
|
||||
{
|
||||
printf("Error opening file\n");
|
||||
return 1;
|
||||
}
|
||||
if ((fp_write = fopen("./Results.txt", "w")) == NULL)
|
||||
{
|
||||
printf("Error opening file\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
while (!feof(fp_read))
|
||||
{
|
||||
fscanf(fp_read, " %c %f %f", &operation, &op1, &op2);
|
||||
if (!feof(fp_read))
|
||||
{
|
||||
switch (operation)
|
||||
{
|
||||
case '+':
|
||||
res = op1 + op2;
|
||||
break;
|
||||
|
||||
case '-':
|
||||
res = op1 - op2;
|
||||
break;
|
||||
|
||||
case '*':
|
||||
res = op1 * op2;
|
||||
break;
|
||||
|
||||
case '/':
|
||||
if (op2 == 0)
|
||||
{
|
||||
printf("Error: divide by zero\n");
|
||||
return 3;
|
||||
}
|
||||
res = op1 / op2;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Error: invalid operation\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(fp_write, "%c %.2f\n", operation, res);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp_read);
|
||||
fclose(fp_write);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user