Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- typedef enum { false = 0, true = !false } bool;
- int log(int n, int b) {
- return !(n / b < 1) ? 1 + log(n / b, b) : 1;
- }
- int pow(int b, int e) {
- return e < 1 ? 1 : b * pow(b, e - 1);
- }
- //Esercizio 6-----------------------------------------------------
- #define tronca(n,o) n -= (n / o) * o //elimina la prima cifra di un intero n
- #define cifre(n) log(n, 10)
- #define offset(i) pow(10, i)
- bool checkDigit(int n, unsigned char d) {
- for (int i = cifre(n), o; i > 0; tronca(n, o)) {
- o = offset(--i);
- if (d == (n / o))
- return true;
- }
- return false;
- }
- inline bool ex6(int x, int y, unsigned char d) {
- return checkDigit(x, d) && checkDigit(y, d);
- }
- #undef tronca
- #undef cifre
- #undef offset
- //Esercizio 7/8---------------------------------------------------
- #define offset sizeof(char)
- #define itoc(i) i + '0' //int to char - IO meaning
- #define pt_str(l) (char*)malloc(offset * l); //ptstr(lenght of string) - string pointer
- #define store_in_str(p, i, c) *(p + offset * i) = c //store_in_str(string pointer, index, char)
- #define print_out_str(p,i) putchar(*(p + offset * i)) //print_out_str(string pointer, index)
- void ricorsione(int n, unsigned char b) {
- if (n / b)
- ricorsione(n / b, b);
- putchar(itoc(n % b));
- }
- void dton(int n, unsigned char b, bool ricorsivo) {
- if (ricorsivo)
- ricorsione(n, b);
- else {
- int dim = log(n, b);
- char *c = pt_str(dim);
- for (int i = dim; i > 0; n /= b)
- store_in_str(c, --i, itoc(n % b));
- for (int i = 0; i < dim; i++)
- print_out_str(c, i);
- free(c);
- }
- }
- #undef offset
- #undef itoc
- #undef pt_str
- #undef store_in_str
- #undef print_out_str
- //Esercizio 9--------------------------------------------------------
- int Euclide(int a, int b){
- int r;
- while (b){
- r = a % b;
- a = b;
- b = r;
- }
- return a;
- }
- void ex9(int a, int b) {
- int mcd = Euclide(a, b);
- printf("mcd: %d\nmcm: %d", mcd, a * b / mcd);
- }
- //Esercizio 10-------------------------------------------------------
- void ex10() {
- bool fp; //flag parola
- char c;
- unsigned contc, contp, contr;
- fp = c = contc = contp = contr = 0;
- while (c != 'X') {
- c = getchar();
- if (c == '\n') contr++;
- if (c == '\n' || c == ' ')
- fp = true;
- else{
- contc++;
- if (fp) contp++;
- fp = false;
- }
- }
- printf("\nNumero di caratteri: %u\nNumero di parole: %u\nNumero di righe: %u\n", contc, contp + 1, contr + 1);
- }
- //-------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement