Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef enum { false = 0, true = !false } bool;
  4.  
  5. int log(int n, int b) {
  6.     return !(n / b < 1) ? 1 + log(n / b, b) : 1;
  7. }
  8. int pow(int b, int e) {
  9.     return e < 1 ? 1 : b * pow(b, e - 1);
  10. }
  11.  
  12. //Esercizio 6-----------------------------------------------------
  13.  
  14. #define tronca(n,o) n -= (n / o) * o                        //elimina la prima cifra di un intero n
  15. #define cifre(n) log(n, 10)
  16. #define offset(i) pow(10, i)
  17.  
  18. bool checkDigit(int n, unsigned char d) {
  19.     for (int i = cifre(n), o; i > 0; tronca(n, o)) {
  20.         o = offset(--i);
  21.         if (d == (n / o))
  22.             return true;
  23.     }
  24.     return false;
  25. }
  26.  
  27. inline bool ex6(int x, int y, unsigned char d) {
  28.     return checkDigit(x, d) && checkDigit(y, d);
  29. }
  30.  
  31. #undef tronca
  32. #undef cifre
  33. #undef offset
  34.  
  35. //Esercizio 7/8---------------------------------------------------
  36.  
  37. #define offset sizeof(char)
  38. #define itoc(i) i + '0'                                     //int to char - IO meaning
  39. #define pt_str(l) (char*)malloc(offset * l);                //ptstr(lenght of string) - string pointer
  40. #define store_in_str(p, i, c) *(p + offset * i) = c         //store_in_str(string pointer, index, char)
  41. #define print_out_str(p,i) putchar(*(p + offset * i))       //print_out_str(string pointer, index)
  42.  
  43. void ricorsione(int n, unsigned char b) {
  44.     if (n / b)
  45.         ricorsione(n / b, b);
  46.     putchar(itoc(n % b));
  47. }
  48.  
  49. void dton(int n, unsigned char b, bool ricorsivo) {
  50.     if (ricorsivo)
  51.         ricorsione(n, b);
  52.     else {
  53.         int dim = log(n, b);
  54.         char *c = pt_str(dim);
  55.         for (int i = dim; i > 0; n /= b)
  56.             store_in_str(c, --i, itoc(n % b));
  57.         for (int i = 0; i < dim; i++)
  58.             print_out_str(c, i);
  59.         free(c);
  60.     }
  61. }
  62.  
  63. #undef offset
  64. #undef itoc
  65. #undef pt_str
  66. #undef store_in_str
  67. #undef print_out_str
  68.  
  69. //Esercizio 9--------------------------------------------------------
  70.  
  71. int Euclide(int a, int b){
  72.     int r;
  73.     while (b){
  74.         r = a % b;
  75.         a = b;
  76.         b = r;
  77.     }
  78.     return a;
  79. }
  80.  
  81. void ex9(int a, int b) {
  82.     int mcd = Euclide(a, b);
  83.     printf("mcd: %d\nmcm: %d", mcd, a * b / mcd);
  84. }
  85.  
  86. //Esercizio 10-------------------------------------------------------
  87.  
  88. void ex10() {
  89.     bool fp;                                            //flag parola
  90.     char c;
  91.     unsigned contc, contp, contr;
  92.     fp = c = contc = contp = contr = 0;
  93.     while (c != 'X') {
  94.         c = getchar();
  95.         if (c == '\n') contr++;
  96.         if (c == '\n' || c == ' ')
  97.             fp = true;
  98.         else{
  99.             contc++;
  100.             if (fp) contp++;
  101.             fp = false;
  102.         }
  103.     }
  104.     printf("\nNumero di caratteri: %u\nNumero di parole: %u\nNumero di righe: %u\n", contc, contp + 1, contr + 1);
  105. }
  106.  
  107. //-------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement