Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • 20041679/prog-1-lab-assigments
1 result
Show changes
aaabca
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
char c1;
char c2;
int diff = 0;
while (fscanf(f1, "%c", &c1) != EOF &&
fscanf(f2, "%c", &c2) != EOF &&
!diff) {
if (c1 != c2) {
diff = 1;
printf("file 1: %c, file2: %c\n", c1, c2);
}
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
abcdefghijk
abbdefgjklk
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
#define FOUT_STR "out.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
FILE *fout;
if ((fout = fopen(FOUT_STR, "w"))) {
char c1;
char c2;
while (fscanf(f1, "%c", &c1) != EOF &&
fscanf(f2, "%c", &c2) != EOF) {
if (c1 != c2) {
printf("file 1: %c, file2: %c\n", c1, c2);
fprintf(fout, "%c%c\n", c1, c2);
}
}
fclose(fout);
} else {
ret = 1;
err(FOUT_STR ": ");
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
cb
hj
ik
jl
aaa bbb ccc ddd eee
aaa b ccc dd eeee
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
char s1[128];
char s2[128];
while (fscanf(f1, "%s", s1) != EOF &&
fscanf(f2, "%s", s2) != EOF) {
if (strcmp(s1, s2) != 0) {
puts(s2);
}
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
#include <stdio.h>
size_t slen(char *s) {
size_t i = 0;
while (s[i] != '\0') { ++i; }
return i;
}
void strrev(char *src, char *dst) {
size_t len = slen(src);
for (size_t i = 0; i < len; ++i) {
dst[i] = src[len - 1 - i];
}
src[len] = '\0';
}
int main(void) {
char s[] = "Informatica";
char s_rev[sizeof(s)];
strrev(s, s_rev);
printf("%s", s_rev);
}
size_t lunghezza(char *s) {
size_t i = 0;
while (s[i] != '\0') { ++i; }
return i;
}
int palindroma(char *s) {
int is_palindrome = 1;
size_t len = lunghezza(s);
for (size_t i = 0; i < len / 2 && is_palindrome; ++i) {
if (s[i] != s[len - 1 - i]) { is_palindrome = 0; }
}
return is_palindrome;
}
void cswap(char *c1, char *c2) {
char temp = *c1;
*c1 = *c2;
*c2 = temp;
}
void inverti(char *s) {
size_t len = lunghezza(s);
for (size_t i = 0; i < len / 2; ++i) {
cswap(&s[i], &s[len - 1 - i]);
}
}
#include <stdio.h>
void print_repeat_char(char c, unsigned n) {
for (unsigned i = 0; i < n; ++i) {
printf("%c", c);
}
}
int main(void) {
puts("Lato del quadrato");
unsigned side;
scanf("%u", &side);
for (unsigned row = 1; row <= side; ++row) {
unsigned stars_amount = row;
print_repeat_char('*', stars_amount);
unsigned plus_amount = side - stars_amount;
print_repeat_char('+', plus_amount);
puts("");
}
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#define END -1000
#define ASC_STR "crescente"
#define DESC_STR "decrescente"
#define NEITHER_STR "ne' " ASC_STR "ne' " DESC_STR
const char *sequence_monotony_str(bool asc, bool desc) {
const char *sequence_kind_str;
printf("la sequenza e' ");
if (asc) {
assert(!desc);
sequence_kind_str = ASC_STR;
} else if (desc) {
assert(!asc);
sequence_kind_str = DESC_STR;
} else {
sequence_kind_str = NEITHER_STR;
}
return sequence_kind_str;
}
int main(void) {
int sum = 0;
int prev, num;
bool asc = true, desc = true;
unsigned num_counter = 0;
scanf("%d", &prev);
sum += prev;
if (prev != END) {
num_counter = 1;
while (scanf("%d", &num), num != END) {
++num_counter;
sum += num;
asc &= num > prev;
desc &= num < prev;
prev = num;
}
}
if (num_counter > 1) {
puts(sequence_monotony_str(asc, desc));
} else {
puts("la sequenza non e' abbastanza lunga per essere definita crescente o decrescente");
}
printf("somma: %d\n", sum);
return 0;
}
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
# define LEN(x) (sizeof(x) / sizeof(*x))
enum Product { COFFE = 0, TEA = 1, CHOCOLATE = 2, CAPPUCCINO = 3, LAST_PRODUCT };
const char *const products_str[] = { "caffe'", "the", "cioccolata", "cappuccino", };
const int products_price[] = { 50, 40, 60, 70 };
const int coins[] = { 1, 5, 10, 20, 50 };
void print_numbered_strings(const char *const products[], size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%zu. %s\n", i, products[i]) ;
}
}
int input_int() {
int ret;
scanf("%d", &ret);
return ret;
}
int input_int_range(int lower, int upper) {
int ret;
while ((ret = input_int()) < lower || ret > upper) {
puts("Valore non ammesso");
}
return ret;
}
bool is_in_array(int needle, const int haystack[], size_t len) {
bool found = false;
// could use memmem(3) instead
for (size_t i = 0; i < len && !found; i++) {
if (haystack[i] == needle) { found = true; }
}
return found;
}
int input_int_filter(const int whitelist[], size_t len) {
int ret;
while (ret = input_int(), !is_in_array(ret, whitelist, len)) {
puts("Valore non ammesso");
}
return ret;
}
void calculate_change(int total, int *count_5, int *count_1) {
*count_5 = total / 5;
total -= 5 * *count_change;
*count_1 = total;
}
int main(void) {
assert(LEN(products_str) == LEN(products_price));
assert(LEN(products_str) == LAST_PRODUCT);
int selected_product;
int credit = 0;
int inserted_coin;
int count_change_5 = 0, count_change_1 = 0;
puts("Seleziona il prodotto");
print_numbered_strings(products_str, LEN(products_str));
selected_product = input_int_range(0, LAST_PRODUCT);
printf("Costo: %d centesimi\n", products_price[selected_product]);
puts("Inserisci monete");
while (credit < products_price[selected_product]) {
inserted_coin = input_int_filter(coins, LEN(coins));
credit += inserted_coin;
}
calculate_change(
credit - products_price[selected_product],
&count_change_5, &count_change_1
);
printf("Resto: %d monete da 5, %d monete da 1\n", count_change_5, count_change_1);
return 0;
}
#include <stdio.h>
#include <math.h>
#define PERC_CRASHED (+0.12)
#define PERC_NOT_CRASHED (-0.04)
int main(void) {
double new_total_amount = 0;
double old_total_amount = 0;
do {
puts("Inserire numero di auto");
unsigned int n_cars;
scanf("%u", &n_cars);
double old_client_amount = 0;
double new_client_amount = 0;
for (unsigned int i = 0; i < n_cars; i++) {
double old_car_amount;
double new_car_amount;
printf("Inserire importo dell'auto %u\n", i + 1);
do {
scanf("%lf", &old_car_amount);
} while (old_car_amount < 0);
old_client_amount += old_car_amount;
puts("Inserire numero di incidenti dell'anno scorso");
int n_crashes;
do {
scanf("%u", &n_crashes);
} while (n_crashes < 0);
if (n_crashes > 0) {
new_car_amount = old_car_amount + PERC_CRASHED * old_car_amount;
} else {
new_car_amount = old_car_amount - PERC_NOT_CRASHED * old_car_amount;
}
new_client_amount += new_car_amount;
}
printf("Importo da pagare per il cliente: %lf\n", new_client_amount);
old_total_amount += old_client_amount;
new_total_amount += new_client_amount;
puts("Inserire nuovo cliente? y/n");
} while (getchar() == 'y');
printf("Totale degli importi: %lf, variazione rispetto al precedente: %lf\n",
new_total_amount, fabs(new_total_amount - old_total_amount));
}