Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Написано жесть, почти в каждой строчке ошибка.
- //Поправил drobi - теперь работает, вроде. Fraction не работает, лучше замени его тем, с предыдущей задачи.
- //ПРотестируй обязательно.
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- const int mod=1000*1000*1000;
- class fraction
- {
- protected:
- int fir, sec;
- public:
- void init (int a=0, int b=0)
- {
- fir=a; sec=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;
- }
- while (sec%10==0) sec/=10;
- stream << fir << "." << 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;
- }
- virtual fraction operator+ (const fraction & oth)
- {
- int a, b;
- b=(sec+oth.sec)%mod;
- while (b<mod/10) {
- b*=10;
- }
- a=fir+oth.fir+(sec+oth.sec)/mod;
- fraction z(a, b);
- return z;
- }
- friend int red (int num, int den);
- };
- int red (int a, int b) //reduction
- {
- while (a!=0 && b!=0) {
- if (a>b) a = a%b;
- else b = b%a;
- }
- return a+b;
- }
- class drobi: public fraction
- {
- int thir;
- public:
- drobi (): fraction() { thir = 1;}
- drobi(int x, int y, int z): fraction(x, y), thir(z) {};
- drobi(const drobi& obj) {*this = *&obj;}
- drobi operator+ (const drobi& obj) const
- {
- drobi temp;
- temp.fir = fir+obj.fir;
- temp.sec = sec*obj.thir+thir*obj.sec;
- temp.thir = thir*obj.thir;
- while (temp.sec >= temp.thir) {
- temp.fir++;
- temp.sec -= temp.thir;
- }
- int reduct = red(temp.sec, temp.thir);
- temp.sec /= reduct; temp.thir /= reduct;
- return temp;
- }
- friend istream& operator>> (istream& in , drobi& obj)
- {
- in >> obj.fir >> obj.sec >> obj.thir;
- return in;
- }
- friend ostream& operator<< (ostream& out, const drobi& obj)
- {
- out << obj.fir << " " << obj.sec << " " << obj.thir << endl;
- return out;
- }
- };
- int main ()
- {
- fraction ab(1,006), bb(2,936);
- cout << "sum_fr " << ab + bb << endl;
- drobi ad(-1, 1, 2), bd(5,0,7);
- cout << "sum_dr " << ad + bd << endl;
- fraction *arr[4];
- fraction sum_fr(0, 0); drobi sum_dr(0, 0, 1); int fr_num = 0, dr_num = 0;
- arr[0] = new fraction(2,5);
- arr[1] = new drobi(1,9,10);
- arr[2] = new drobi(2,1,4);
- arr[3] = new fraction(-9,1);
- drobi *cur;
- for (int i = 0; i < 4; ++i) {
- cur = dynamic_cast<drobi*>(arr[i]);
- if (!cur) {
- sum_fr = sum_fr + *arr[i];
- ++fr_num;
- }
- else {
- sum_dr = sum_dr + *cur;
- ++dr_num;
- }
- }
- cout << sum_fr << " " << sum_dr;
- cout << fr_num << " " << dr_num;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment