Skip to content
Snippets Groups Projects
Commit bd905a97 authored by 20041679's avatar 20041679
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
#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));
}
#include <stdio.h>
void input_int_array(int arr[], size_t len) {
for (size_t i = 0; i < len; ++i) {
scanf("%d", arr[i]);
}
}
#include <stdio.h>
void print_int_array(int arr[], size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%d", arr[i]);
if (i < len - 1) { printf(", "); }
}
puts("");
}
#include <assert.h>
void add_int_array(int A[], int dimA, int B[], int dimB, int C[], int dimC) {
assert(dimA == dimB);
assert(dimB == dimC);
for (int i = 0; i < dimA; ++i) {
C[i] = A[i] + B[i];
}
}
int sum_int_array(int arr[], int len) {
int ret = 0;
for (int i = 0; i < len; i++) {
ret += arr[i];
}
return ret;
}
int f(int A[], int dimA, int B[], int dimB) {
int sumi = 0;
for (int i = 0; i < dimA; ++i) { sumi += A[i] * sum_int_array(B, dimB); }
return sumi;
}
#include <stdio.h>
void minmax(int array[], int len, int *min, int *max) {
int i = 0;
*min = array[i];
*max = array[i];
for (i = 1; i < len; i++) {
if (array[i] < *min) { *min = array[i]; }
if (array[i] > *max) { *max = array[i]; }
}
}
#include <stdio.h>
int max(int array[], int len, int *n_max_occurr) {
int max;
*n_max_occurr = 0;
int i = 0;
max = array[i];
(*n_max_occurr)++;
for (i = 1; i < len; i++) {
if (array[i] == max) { (*n_max_occurr)++; }
else if (array[i] > max) {
max = array[i];
(*n_max_occurr) = 1;
}
}
return max;
}
#include <stdio.h>
int maxcount(int array[], int len) {
int i = 0;
int max_i = i;
for (i = 1; i < len; i++) {
if (array[i] > array[max_i]) { max_i = i; }
}
int ret = array[max_i];
array[max_i] = -1;
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>
#include <string.h>
#define BUF_SIZE 4096
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
int main(void) {
char str1[BUF_SIZE];
char str2[BUF_SIZE];
fgets(str1, ARR_SIZE(str1), stdin);
str_strip_trailing(str1);
fgets(str2, ARR_SIZE(str2), stdin);
str_strip_trailing(str2);
if (strcmp(str1, str2) == 0) {
puts("Le due stringhe sono uguali");
} else if (strstr(str1, str2) != NULL) {
puts("La seconda stringa e' contenuta nella prima");
} else if (strstr(str2, str1) != NULL) {
puts("La prima stringa e' contenuta nella seconda");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 4096
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
const char vowels[] = "aeiouAEIOU";
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
void change_forbidden_chars(char *s, const char *remove, char new) {
char *match = s;
while ((match = strpbrk(match, remove)) != NULL) {
*match = new;
}
}
int main(void) {
char str1[BUF_SIZE];
char str2[BUF_SIZE];
char str12[BUF_SIZE * 2 - 1];
fgets(str1, ARR_SIZE(str1), stdin);
str_strip_trailing(str1);
fgets(str2, ARR_SIZE(str2), stdin);
str_strip_trailing(str2);
strcpy(str12, str1);
strcat(str12, str2);
change_forbidden_chars(str12, vowels, '*');
puts(str12);
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BUF_SIZE 100
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
const char vowels[] = {'a', 'e', 'i', 'o', 'u' };
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
void count_vowels(char *s, unsigned int counts[]) {
while (*s) {
int match = 0;
size_t i;
for (i = 0; i < ARR_SIZE(vowels) && !match; ++i) {
if (vowels[i] == tolower(*s)) { match = 1; }
}
if (match) { ++counts[i - 1]; }
++s;
}
}
unsigned int arr_sum(unsigned int arr[], size_t len) {
unsigned int ret = 0;
for (size_t i = 0; i < len; ++i) { ret += arr[i]; }
return ret;
}
int main(void) {
char str[BUF_SIZE];
fgets(str, ARR_SIZE(str), stdin);
str_strip_trailing(str);
unsigned int vowels_counters[5] = {0};
count_vowels(str, vowels_counters);
unsigned int n_vowels = arr_sum(vowels_counters, ARR_SIZE(vowels_counters));
size_t len = strlen(str);
unsigned int n_consonants = len - n_vowels;
printf("vocali: %u, consonanti: %u\n", n_vowels, n_consonants);
puts("frequenze vocali:");
for (size_t i = 0; i < 5; ++i) {
printf("%c: %lf\n", vowels[i], ((double)vowels_counters[i] / len) * 100);
}
}
File added
#include <stdio.h>
#include <assert.h>
#define MARKER -1
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
int populate(int arr[], size_t size) {
int insert;
size_t i = 0;
while (i < size && ((scanf("%d", &insert)), insert != MARKER)) {
arr[i] = insert;
++i;
}
int ret;
if (i < size) {
arr[i] = MARKER;
ret = 1;
}
else { ret = -1; }
return ret;
}
void print(int arr[]) {
size_t i = 0;
if (arr[i] != MARKER) {
while (arr[i + 1] != MARKER) {
printf("%d*", arr[i]);
++i;
}
printf("%d", arr[i]);
}
}
int *find(int where[], int what) {
size_t i = 0;
int found = 0;
while (where[i] != MARKER && !found) {
if (where[i] == what) { found = 1; }
else { ++i; }
}
int *ret;
if (found) { ret = &where[i]; }
else { ret = NULL; }
return ret;
}
void remove_first(int where[], int what) {
assert(what != MARKER);
int *pos = find(where, what);
if (pos != NULL) {
while ((*pos) != MARKER) {
*pos = *(pos + 1);
++pos;
}
}
}
void remove_all(int where[], int what) {
size_t shift_width = 0;
size_t i = 0;
while (where[i + shift_width] != MARKER) {
if (where[i] == what) {
++shift_width;
} else {
++i;
}
where[i] = where[i + shift_width];
}
where[i] = MARKER;
}
int main(void) {
int array[10];
if (populate(array, ARR_SIZE(array))) {
remove_all(array, 5);
print(array);
}
}
#include <stdio.h>
#define MARK -1
#define LEN(x) (sizeof(x) / sizeof(*x))
void copy(int to[], int from[], int n, int *ret) {
int i = 0;
while (from[i] != MARK && i < n - 1) {
to[i] = from[i];
++i;
}
to[i] = MARK;
*ret = from[i] == MARK ? 1 : -1;
}
void cmp(int a1[], int a2[], int *ret) {
int i = 0;
while (a1[i] != MARK && a2[i] != MARK && a1[i] == a2[i]) { ++i; }
*ret = a1[i] == a2[i] ? 1 : -1;
}
int count_greater(int a1[], int a2[]) {
int i = 0;
int ret = 0;
while (a1[i] != MARK && a2[i] != MARK) {
if (a1[i] > a2[i]) { ++ret; }
++i;
}
while (a2[i] != MARK) {
++ret;
++i;
}
return ret;
}
int copy_even_positions(int to[], int from[], int n) {
int i = 0;
while (from[i * 2] && from[(i * 2) + 1] != MARK && i < n - 1) {
to[i] = from[i * 2];
++i;
}
to[i] = MARK;
return i;
}
int sum(int a1[], int a2[], int n, int sum[]) {
int i = 0;
while (a1[i] != MARK && a2[i] != MARK && i < n - 1) {
sum[i] = a1[i] + a2[i];
++i;
}
while (a1[i] != MARK && i < n - 1) {
sum[i] = a1[i];
++i;
}
while (a2[i] != MARK && i < n - 1) {
sum[i] = a2[i];
++i;
}
sum[i] = MARK;
return a1[i] == MARK || a2[i] == MARK ? 1 : -1;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment