Advertisement
Guest User

Untitled

a guest
Jan 16th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class type> type mcm(type*,type);
  6. template <class type> type MinInArr(type*,type);
  7. // template <class type> type MaxInArr(type*,type);
  8. template <class type> type* AddizionaFrazioni(type**,type);
  9.  
  10. int main()
  11. {
  12.     cout << "Start.." << endl;
  13.  
  14.     int frazioni[2][2] = { {1,4}, {2,4} };
  15.     int *risultato;
  16.  
  17.     risultato = AddizionaFrazioni(&frazioni,2);
  18.  
  19.     cout << risultato[0] << "%" << risultato[1] << endl;
  20.  
  21.     return 0;
  22. }
  23. template <class type> type* AddizionaFrazioni(type** frazioni, type nfrazioni)
  24. {
  25.     type denominatore = 0;
  26.     type *denominatori = new type[nfrazioni];
  27.     type numeratore = 0;
  28.  
  29.     for(type i = 0; i < nfrazioni; i++)
  30.     {
  31.         denominatori[i] = frazioni[i][0];
  32.     }
  33.  
  34.     denominatore = mcm(denominatori,nfrazioni);
  35.  
  36.     delete [] denominatori;
  37.  
  38.     for(int i = 0; i < nfrazioni; i++)
  39.     {
  40.         numeratore += denominatore/frazioni[i][1]*frazioni[i][0];
  41.     }
  42.  
  43.     type risultato = new type[2];
  44.     risultato[0] = numeratore;
  45.     risultato[1] = denominatore;
  46.  
  47.     return risultato;
  48. }
  49.  
  50. template <class type> type mcm(type *numeri, type len)
  51. {
  52.     type mcm = MinInArr(numeri,len);
  53.     type i = 0, err = 0;
  54.     while(1)
  55.     {
  56.         err = 0;
  57.         for(i = 0; i < len; ++i)
  58.         {
  59.             if( (mcm % numeri[i]) != 0 )
  60.             {
  61.                 ++err;
  62.                 break;
  63.             }
  64.         }
  65.         if (err == 0)
  66.         {
  67.             return mcm;
  68.         }
  69.         ++mcm;
  70.     }
  71. }
  72.  
  73. template <class type> type MinInArr(type *numeri, type len)
  74. {
  75.     type min = NULL;
  76.     for(type i = 0; i < len; ++i)
  77.     {
  78.         if( (min == NULL) || (min > numeri[i]) )
  79.         {
  80.             min = numeri[i];
  81.         }
  82.     }
  83.     return min;
  84. }
  85.  
  86. /*
  87. template <class type> type MaxInArr(type *numeri, type len)
  88. {
  89.     type max = NULL;
  90.     for(type i = 0; i < len; ++i)
  91.     {
  92.         if( (max == NULL) || (max < numeri[i]) )
  93.         {
  94.             max = numeri[i];
  95.         }
  96.     }
  97.     return max;
  98. }
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement