makrusak

Пытаюсь помочь вредине :)

Dec 23rd, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 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.  
  12.   int add0(int q) const {
  13.     if (q==0) return 0;
  14.     while (q<mod/10) q*=10;
  15.     return q;
  16.   }
  17.  
  18.   int remove0(int q) const {
  19.     if (q==0) return 0;
  20.     while (q%10==0) q/=10;
  21.     return q;
  22.   }
  23.  
  24. public:
  25.     void init (int a=0, int b=0)
  26.     {
  27.         fir=a; sec=add0(b);
  28.     }
  29.  
  30.   friend istream& operator>> (istream& stream, fraction& d){
  31.         char str[50]; int a=0, b=0, znak=1;
  32.         stream >> str; int i=0;
  33.         if (str[0]=='-') {znak*=(-1); i++;}
  34.         while (str[i]!='.') {
  35.             a=a*10+(int)str[i]-'0';
  36.             i++;
  37.         }
  38.         a*=znak;
  39.         i++;
  40.         while (str[i]) {
  41.             b=b*10+(int)str[i]-'0'; i++;
  42.         }
  43.         while (b<mod/10) {
  44.             b*=10;
  45.         }
  46.         if (a<0) {a--; b=mod-b;}
  47.         d.init (a, b);
  48.     return stream;
  49.   }
  50.  
  51.   friend ostream& operator<< (ostream& stream, const fraction& d) {
  52.     int sec = d.sec;
  53.     int fir = d.fir;
  54.     if (fir<0 && sec!=0) {
  55.       fir++;
  56.       sec = mod-sec;
  57.     }
  58.     stream << fir << "." << d.remove0(sec);
  59.     return stream;
  60.   }
  61.  
  62.     fraction (int a, int b){init (a, b);}
  63.     fraction () {}
  64.     fraction (const fraction & obj):fir(obj.fir), sec(obj.sec){}
  65.  
  66.     fraction & operator = (const fraction & q)
  67.     {
  68.         fir=q.fir; sec=q.sec; return *this;
  69.     }
  70.  
  71.     fraction operator+ (const fraction & oth)
  72.     {
  73.         int a, b;
  74.  
  75.         b=(sec+oth.sec)%mod;
  76.     a = fir+oth.fir+(sec+oth.sec)/mod;
  77.  
  78.         fraction z(a, b);
  79.         return z;
  80.     }
  81.  
  82.   fraction operator * (const fraction & oth) {
  83.     // ([x] + {x})*([y] + {y})
  84.     // [x]*[y] + [x]*{y} + [y]*{x} + {x}*{y}
  85.     int resf = 0, ress = 0;
  86.     resf += fir * oth.fir;
  87.     long long prom = (long long)fir*oth.sec;
  88.     prom += (long long)oth.fir*sec;
  89.     if (prom<0) {
  90.       long long promwas = prom;
  91.       prom = ((long long)mod*(mod+1)+prom)%mod;
  92.       resf -= (prom-promwas)/mod;
  93.       ress += prom;
  94.     }
  95.     else {
  96.       resf += prom/mod;
  97.       ress += prom%mod;
  98.     }
  99.     prom = (long long)sec * oth.sec;
  100.     ress += prom/mod;
  101.     resf+=ress/mod;
  102.     ress%=mod;
  103.  
  104.     return fraction(resf, ress);
  105.   }
  106.  
  107.   fraction opposite() const {
  108.     int afir = 0, asec = 0;
  109.     if (sec==0) {
  110.       afir = -fir;
  111.     }
  112.     else {
  113.       afir = -(fir+1);
  114.       asec = mod-sec;
  115.     }
  116.     return fraction(afir, asec);
  117.   }
  118.  
  119.   fraction find_sum(const fraction &to, const fraction &step) const {
  120.     fraction sum(0, 0);
  121.     fraction cp = (*this);
  122.     while (cp<=to) {
  123.       sum += cp;
  124.       cp += step;
  125.     }
  126.     return sum;
  127.   }
  128.  
  129.     fraction operator - (const fraction &oth)
  130.     {
  131.         return (*this + oth.opposite());
  132.     }
  133.  
  134.     fraction & operator += (const fraction & oth)
  135.     {
  136.     *this = *this + oth;
  137.         return *this;
  138.     }
  139.  
  140.     bool operator < (const fraction & oth) const
  141.     {
  142.         if (fir < oth.fir) return true;
  143.         else if (fir>oth.fir) return false;
  144.         else if (fir==oth.fir)
  145.         {
  146.             if (sec<oth.sec) return true;
  147.             else return false;
  148.         }
  149.     }
  150.  
  151.     bool operator <= (const fraction & oth) const
  152.     {
  153.         if (fir < oth.fir) return true;
  154.         else if (fir>oth.fir) return false;
  155.         else if (fir==oth.fir)
  156.         {
  157.             if (sec<=oth.sec) return true;
  158.             else return false;
  159.         }
  160.     }
  161.  
  162. };
  163.  
  164. void RunTests() {
  165.   // Test #1
  166. }
  167.  
  168. int main ()
  169. {
  170.   RunTests();
  171.   fraction a,b;
  172.   cin >> a >> b;
  173.   cout << a*b << "\n";
  174.   fraction step;
  175.   cin >> step;
  176.   cout << a.find_sum(b, step) << "\n";
  177.     /*
  178.   for (int i=0;i<n;i++) cin >> a[i];
  179.   sort(a, a+n);
  180.   for (int i=0;i<n;i++) cout << a[i] << "\n";
  181.   */
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment