makrusak

Help1

Dec 23rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <algorithm>
  4. using namespace std;
  5. const int mod=1000*1000*1000;
  6.  
  7. class fraction
  8. {
  9. protected:
  10.     int fir, sec;
  11. public:
  12.     void init (int a=0, int b=0)
  13.     {
  14.         fir=a; sec=b;
  15.     }
  16.  
  17.   friend istream& operator>> (istream& stream, fraction& d){
  18.         char str[50]; int a=0, b=0, znak=1;
  19.         stream >> str; int i=0;
  20.         if (str[0]=='-') {znak*=(-1); i++;}
  21.         while (str[i]!='.') {
  22.             a=a*10+(int)str[i]-'0';
  23.             i++;
  24.         }
  25.         a*=znak;
  26.         i++;
  27.         while (str[i]) {
  28.             b=b*10+(int)str[i]-'0'; i++;
  29.         }
  30.         while (b<mod/10) {
  31.             b*=10;
  32.         }
  33.         if (a<0) {a--; b=mod-b;}
  34.         d.init (a, b);
  35.     return stream;
  36.   }
  37.  
  38.   friend ostream& operator<< (ostream& stream, const fraction& d) {
  39.     int sec = d.sec;
  40.     int fir = d.fir;
  41.     if (fir<0 && sec!=0) {
  42.       fir++;
  43.       sec = mod-sec;
  44.     }
  45.     while (sec%10==0) sec/=10;
  46.     stream << fir << "." << sec;
  47.     return stream;
  48.   }
  49.  
  50.     fraction (int a, int b){init (a,b);}
  51.     fraction () {}
  52.     fraction (const fraction & obj):fir(obj.fir), sec(obj.sec){}
  53.  
  54.     fraction & operator = (const fraction & q)
  55.     {
  56.         fir=q.fir; sec=q.sec; return *this;
  57.     }
  58.     fraction operator+ (const fraction & oth)
  59.     {
  60.         int a, b;
  61.  
  62.         b=(sec+oth.sec)%mod;
  63.         while (b<mod/10) {
  64.             b*=10;
  65.         }
  66.         a=fir+oth.fir+(sec+oth.sec)/mod;
  67.         fraction z(a, b);
  68.         return z;
  69.     }
  70.  
  71.   fraction operator * (const fraction & oth) {
  72.     // ([x] + {x})*([y] + {y})
  73.     // [x]*[y] + [x]*{y} + [y]*{x} + {x}*{y}
  74.     int resf = 0, ress = 0;
  75.     resf += fir * oth.fir;
  76.     long long prom = (long long)fir*oth.sec;
  77.     prom += (long long)oth.fir*sec;
  78.     resf += prom/mod;
  79.     ress += prom%mod;
  80.     prom = (long long)sec * oth.sec;
  81.     ress += prom/mod;
  82.     return fraction(resf+ress/mod, ress%mod);
  83.   }
  84.  
  85.     fraction operator - (const fraction &oth)
  86.     {
  87.         int a, b;
  88.         int oth_fir=oth.fir; int oth_sec=oth.sec;
  89.         if (oth_fir<0) {oth_fir++; oth_fir*=(-1);oth_sec=mod-oth_sec;}
  90.         else {oth_fir*=(-1); oth_fir--; oth_sec=mod-oth_sec;}
  91.  
  92.         b=(sec+oth_sec)%mod;
  93.         while (b<mod/10) {
  94.             b*=10;
  95.         }
  96.         a=fir+oth_fir+(sec+oth_sec)/mod;
  97.         fraction z(a, b);
  98.         return z;
  99.     }
  100.     fraction & operator += (const fraction & oth)
  101.     {
  102.     fir += oth.fir;
  103.     sec += oth.sec;
  104.         return *this;
  105.     }
  106.     bool operator < (const fraction & oth) const
  107.     {
  108.         if (fir < oth.fir) return true;
  109.         else if (fir>oth.fir) return false;
  110.         else if (fir==oth.fir)
  111.         {
  112.             if (sec<oth.sec) return true;
  113.             else return false;
  114.         }
  115.     }
  116.  
  117.   bool operator <= (const fraction & oth) const
  118.   {
  119.     if (this < oth || (fir==oth.fir && sec==oth.sec) ) return true;
  120.     return false;
  121.   }
  122.  
  123.  
  124. };
  125.  
  126. void RunTests() {
  127.   // Test #1
  128. }
  129.  
  130. int main ()
  131. {
  132.   RunTests();
  133.   fraction a[200];
  134.   int n;
  135.   cin >> n;
  136.   for (int i=0;i<n;i++) cin >> a[i];
  137.   sort(a, a+n);
  138.   for (int i=0;i<n;i++) cout << a[i] << "\n";
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment