Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- const int mod=1000*1000*1000;
- class fraction
- {
- protected:
- int fir, sec;
- int add0(int q) const {
- if (q==0) return 0;
- while (q<mod/10) q*=10;
- return q;
- }
- int remove0(int q) const {
- if (q==0) return 0;
- while (q%10==0) q/=10;
- return q;
- }
- public:
- void init (int a=0, int b=0)
- {
- fir=a; sec=add0(b);
- }
- friend istream& operator>> (istream& stream, fraction& d){
- char str[50]; int a=0, b=0, znak=1;
- stream >> str; int i=0;
- if (str[0]=='-') {znak*=(-1); i++;}
- while (str[i]!='.') {
- a=a*10+(int)str[i]-'0';
- i++;
- }
- a*=znak;
- i++;
- while (str[i]) {
- b=b*10+(int)str[i]-'0'; i++;
- }
- while (b<mod/10) {
- b*=10;
- }
- if (a<0) {a--; b=mod-b;}
- d.init (a, b);
- return stream;
- }
- friend ostream& operator<< (ostream& stream, const fraction& d) {
- int sec = d.sec;
- int fir = d.fir;
- if (fir<0 && sec!=0) {
- fir++;
- sec = mod-sec;
- }
- stream << fir << "." << d.remove0(sec);
- return stream;
- }
- fraction (int a, int b){init (a, b);}
- fraction () {}
- fraction (const fraction & obj):fir(obj.fir), sec(obj.sec){}
- fraction & operator = (const fraction & q)
- {
- fir=q.fir; sec=q.sec; return *this;
- }
- fraction operator+ (const fraction & oth)
- {
- int a, b;
- b=(sec+oth.sec)%mod;
- a = fir+oth.fir+(sec+oth.sec)/mod;
- fraction z(a, b);
- return z;
- }
- fraction operator * (const fraction & oth) {
- // ([x] + {x})*([y] + {y})
- // [x]*[y] + [x]*{y} + [y]*{x} + {x}*{y}
- int resf = 0, ress = 0;
- resf += fir * oth.fir;
- long long prom = (long long)fir*oth.sec;
- prom += (long long)oth.fir*sec;
- if (prom<0) {
- long long promwas = prom;
- prom = ((long long)mod*(mod+1)+prom)%mod;
- resf -= (prom-promwas)/mod;
- ress += prom;
- }
- else {
- resf += prom/mod;
- ress += prom%mod;
- }
- prom = (long long)sec * oth.sec;
- ress += prom/mod;
- resf+=ress/mod;
- ress%=mod;
- return fraction(resf, ress);
- }
- fraction opposite() const {
- int afir = 0, asec = 0;
- if (sec==0) {
- afir = -fir;
- }
- else {
- afir = -(fir+1);
- asec = mod-sec;
- }
- return fraction(afir, asec);
- }
- fraction find_sum(const fraction &to, const fraction &step) const {
- fraction sum(0, 0);
- fraction cp = (*this);
- while (cp<=to) {
- sum += cp;
- cp += step;
- }
- return sum;
- }
- fraction operator - (const fraction &oth)
- {
- return (*this + oth.opposite());
- }
- fraction & operator += (const fraction & oth)
- {
- *this = *this + oth;
- return *this;
- }
- bool operator < (const fraction & oth) const
- {
- if (fir < oth.fir) return true;
- else if (fir>oth.fir) return false;
- else if (fir==oth.fir)
- {
- if (sec<oth.sec) return true;
- else return false;
- }
- }
- bool operator <= (const fraction & oth) const
- {
- if (fir < oth.fir) return true;
- else if (fir>oth.fir) return false;
- else if (fir==oth.fir)
- {
- if (sec<=oth.sec) return true;
- else return false;
- }
- }
- };
- void RunTests() {
- // Test #1
- }
- int main ()
- {
- RunTests();
- fraction a,b;
- cin >> a >> b;
- cout << a*b << "\n";
- fraction step;
- cin >> step;
- cout << a.find_sum(b, step) << "\n";
- /*
- for (int i=0;i<n;i++) cin >> a[i];
- sort(a, a+n);
- for (int i=0;i<n;i++) cout << a[i] << "\n";
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment