makrusak

Дроби для Насти

Dec 23rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. //Написано жесть, почти в каждой строчке ошибка.
  2. //Поправил drobi - теперь работает, вроде. Fraction не работает, лучше замени его тем, с предыдущей задачи.
  3. //ПРотестируй обязательно.
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. using namespace std;
  8. const int mod=1000*1000*1000;
  9.  
  10. class fraction
  11. {
  12. protected:
  13.         int fir, sec;
  14. public:
  15.         void init (int a=0, int b=0)
  16.         {
  17.                 fir=a; sec=b;
  18.         }
  19.  
  20.   friend istream& operator>> (istream& stream, fraction& d){
  21.                 char str[50]; int a=0, b=0, znak=1;
  22.                 stream >> str; int i=0;
  23.                 if (str[0]=='-') {znak*=(-1); i++;}
  24.                 while (str[i]!='.') {
  25.                         a=a*10+(int)str[i]-'0';
  26.                         i++;
  27.                 }
  28.                 a*=znak;
  29.                 i++;
  30.                 while (str[i]) {
  31.                         b=b*10+(int)str[i]-'0'; i++;
  32.                 }
  33.                 while (b<mod/10) {
  34.                         b*=10;
  35.                 }
  36.                 if (a<0) {a--; b=mod-b;}
  37.                 d.init (a, b);
  38.     return stream;
  39.   }
  40.  
  41.   friend ostream& operator<< (ostream& stream, const fraction& d) {
  42.     int sec = d.sec;
  43.     int fir = d.fir;
  44.     if (fir<0 && sec!=0) {
  45.       fir++;
  46.       sec = mod-sec;
  47.     }
  48.     while (sec%10==0) sec/=10;
  49.     stream << fir << "." << sec;
  50.     return stream;
  51.   }
  52.  
  53.         fraction (int a, int b){init (a,b);}
  54.         fraction () {}
  55.         fraction (const fraction & obj):fir(obj.fir), sec(obj.sec){}
  56.  
  57.         fraction & operator = (const fraction & q)
  58.         {
  59.                 fir=q.fir; sec=q.sec; return *this;
  60.         }
  61.         virtual fraction operator+ (const fraction & oth)
  62.         {
  63.                 int a, b;
  64.  
  65.                 b=(sec+oth.sec)%mod;
  66.                 while (b<mod/10) {
  67.                         b*=10;
  68.                 }
  69.                 a=fir+oth.fir+(sec+oth.sec)/mod;
  70.                 fraction z(a, b);
  71.                 return z;
  72.         }
  73.         friend int red (int num, int den);
  74. };
  75.  
  76. int red (int a, int b) //reduction
  77. {
  78.   while (a!=0 && b!=0) {
  79.     if (a>b) a = a%b;
  80.     else b = b%a;
  81.   }
  82.   return a+b;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. class drobi: public fraction
  90. {
  91.     int thir;
  92. public:
  93.     drobi (): fraction() { thir = 1;}
  94.     drobi(int x, int y, int z): fraction(x, y), thir(z) {};
  95.     drobi(const drobi& obj) {*this = *&obj;}
  96.     drobi operator+ (const drobi& obj) const
  97. {
  98.     drobi temp;
  99.     temp.fir = fir+obj.fir;
  100.     temp.sec = sec*obj.thir+thir*obj.sec;
  101.     temp.thir = thir*obj.thir;
  102.     while (temp.sec >= temp.thir) {
  103.         temp.fir++;
  104.         temp.sec -= temp.thir;
  105.     }
  106.     int reduct = red(temp.sec, temp.thir);
  107.     temp.sec /= reduct; temp.thir /= reduct;
  108.     return temp;
  109. }
  110.  
  111.     friend istream& operator>> (istream& in , drobi& obj)
  112.     {
  113.         in >> obj.fir >> obj.sec >> obj.thir;
  114.     return in;
  115.     }
  116.     friend ostream& operator<< (ostream& out, const drobi& obj)
  117.     {
  118.         out << obj.fir << " " << obj.sec << " " << obj.thir << endl;
  119.     return out;
  120.     }
  121.  
  122. };
  123.  
  124.  
  125.  
  126. int main ()
  127. {
  128.     fraction ab(1,006), bb(2,936); 
  129.     cout << "sum_fr " << ab + bb << endl;
  130.    
  131.     drobi ad(-1, 1, 2), bd(5,0,7);
  132.     cout << "sum_dr " << ad + bd << endl;
  133.  
  134.     fraction *arr[4];
  135.     fraction sum_fr(0, 0); drobi sum_dr(0, 0, 1); int fr_num = 0, dr_num = 0;
  136.     arr[0] = new fraction(2,5);
  137.     arr[1] = new drobi(1,9,10);
  138.     arr[2] = new drobi(2,1,4);
  139.     arr[3] = new fraction(-9,1);
  140.     drobi *cur;
  141.  
  142.     for (int i = 0; i < 4; ++i) {
  143.         cur = dynamic_cast<drobi*>(arr[i]);
  144.         if (!cur) {
  145.             sum_fr = sum_fr + *arr[i];
  146.             ++fr_num;
  147.         }
  148.         else {
  149.             sum_dr = sum_dr + *cur;
  150.             ++dr_num;
  151.         }
  152.     }
  153.     cout << sum_fr << " " << sum_dr;
  154.     cout << fr_num << " " << dr_num;
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment