Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programare
#2
#include <iostream.h>
#include <conio.h>

class rational
{ int p,q;
void reduce();
public:
rational(int a=0, int b=1); //constructor - conversia int=>rational
rational(double); // constructor - conversia double => rational
rational operator+(rational&);
friend rational operator-(rational&, rational&);
rational operator*(rational&);
friend rational operator/(rational&, rational&);
rational operator++();
friend rational operator--(rational&);
int operator>(rational&);
friend int operator<(rational&, rational&);
operator int(); //conversie rational=>int
operator double(); //conversie rational=>double
friend ostream& operator<<(ostream&, rational&); // afisarea unui obiect rational
friend istream& operator>>(istream&, rational&);// citirea de la tastatura a unui rational
};

void rational::reduce()
{ int min,i, a, b;
a = p>0 ? p : -p;
b = q>0 ? q : -q;
min=(a<b)?a:b;
for(i=2;i<=min;i++)
while ((a%i==0)&&(b%i==0))
{ a/=i;
b/=i;
min/=i;
}
p = p>0 ? a : -a;
q = q>0 ? b : -b;
}

rational::rational(int a, int b)
{ if (b)
{ p=a;
q=b;
reduce();
}
else
{ cout<<"\nValoare ilegala pentru q, se atribuie implicit 1!";
p=a;
q=1;
}
}


rational::rational(double val)
{ p=val*100;
q=100;
reduce();
}

rational rational::operator+(rational& r)
{
rational aux;
aux.p=p*r.q + q*r.p;
aux.q=q*r.q;
aux.reduce();
return aux;
}

rational operator-(rational &a, rational &b)
{ rational temp;
temp.p=a.p*b.q-a.q*b.p;
temp.q=a.q*b.q;
temp.reduce();
return temp;
}

rational rational::operator*(rational& r)
{ rational aux;
aux.p = p*r.p;
aux.q = q*r.q;
aux.reduce();
return aux;
}

rational operator/(rational& r1, rational& r2)
{ rational aux;
aux.p = r1.p * r2.q;
aux.q = r1.q * r2.p;
aux.reduce();
return aux;
}

rational rational::operator++()
{ p+=q;
return *this;
}

rational operator--(rational& r)
{ r.p-=r.q;
return r;
}

int rational::operator>(rational& r)
{ return (p*r.q > q*r.q);
}

int operator<(rational& r1, rational& r2)
{ return (r1.p * r2.q < r1.q * r2.p);
}

rational::operator int()
{ return p/q;
}

rational::operator double()
{ return (double)p / q;
}

ostream& operator<<(ostream& str, rational& r)
{ str << r.p << "/" << r.q;
return str;
}

istream& operator>>(istream& str, rational& r)
{ cout <<"\np=";
str >> r.p;
do
{
cout << "q=";
str >> r.q;
if (r.q==0) cout<<"\nValoare ilegala pentru q !";
} while (r.q==0);
r.reduce();
return str;
}

void main ()
{ rational a(15,30), b(12,16),c(2,0),d;
cout << "\na= " << a << endl;
cout << "\nb= " << b << endl;
cout << "\nc= " << c << endl;
cout << "\nd= " << d << endl;
getch();
cout<<"\nSe citeste rationalul c:";
cin >> c;
cout << "\nc= " << c << endl;
getch();
d=a+b+c;
cout << "d=a+b+c = " << d << endl;
d=a-b;
cout << "d=a-b = " << d << endl;
d=a*b;
cout << "d=a*b = " << d << endl;
d=a/b;
cout << "d=a/b = " << d << endl;
d=++a;
cout << "d=++a = " << d << endl;
d=--a;
cout << "d=--a = " << d << endl;
if (a > b)
cout<<"("<< a << ") > ("<< b << ")";
if (a < b)
cout<<"("<< a << ") < ("<< b << ")";
d = a + (rational)10 ;
cout << "\nd= " << d << endl;
d = a + (rational)1.5 ;
cout << "\nd= " << d << endl;
double r = (double)a * 5.5;
cout << "\nr = "<< r;
getch();
}

Problema anul 1 la examen. :X
----------------Worldofgothic----------------
---------PLAY
replyReply


Messages In This Thread
Programare - by Zamolxis1 - 10-14-2011, 07:49 PM
RE: Programare - by Szekemri - 10-14-2011, 07:53 PM
RE: Programare - by g3orge13 - 10-14-2011, 09:05 PM
RE: Programare - by Szekemri - 10-14-2011, 09:21 PM
RE: Programare - by Altair xD - 10-14-2011, 09:16 PM
RE: Programare - by g3orge13 - 10-14-2011, 09:18 PM
RE: Programare - by Altair xD - 10-14-2011, 09:21 PM
RE: Programare - by Altair xD - 10-14-2011, 09:23 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 05:42 PM
RE: Programare - by Szekemri - 10-15-2011, 05:56 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 06:46 PM
RE: Programare - by Szekemri - 10-15-2011, 07:04 PM
RE: Programare - by Altair xD - 10-15-2011, 07:49 PM
RE: Programare - by Szekemri - 10-15-2011, 08:04 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:01 PM
RE: Programare - by Szekemri - 10-15-2011, 08:09 PM
RE: Programare - by Altair xD - 10-15-2011, 08:06 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:10 PM
RE: Programare - by Szekemri - 10-15-2011, 08:12 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:17 PM
RE: Programare - by Szekemri - 10-15-2011, 08:24 PM
RE: Programare - by Altair xD - 10-15-2011, 08:25 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:28 PM
RE: Programare - by Szekemri - 10-15-2011, 08:33 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:37 PM
RE: Programare - by Szekemri - 10-15-2011, 08:39 PM
RE: Programare - by Zamolxis1 - 10-15-2011, 08:40 PM
RE: Programare - by Altair xD - 10-16-2011, 03:22 PM
RE: Programare - by Szekemri - 10-16-2011, 04:05 PM
RE: Programare - by Zamolxis1 - 10-17-2011, 04:40 PM
RE: Programare - by Altair xD - 10-17-2011, 04:41 PM
RE: Programare - by Zamolxis1 - 10-17-2011, 04:42 PM
RE: Programare - by Szekemri - 10-18-2011, 07:47 PM
RE: Programare - by Zamolxis1 - 10-18-2011, 07:51 PM
RE: Programare - by Altair xD - 10-30-2011, 06:25 PM
RE: Programare - by Szekemri - 10-30-2011, 07:16 PM
RE: Programare - by Szekemri - 10-30-2011, 07:01 PM
RE: Programare - by Altair xD - 10-30-2011, 07:05 PM
RE: Programare - by Altair xD - 10-30-2011, 08:34 PM
RE: Programare - by Hegan - 10-31-2011, 09:00 PM
RE: Programare - by Szekemri - 10-31-2011, 10:54 PM
RE: Programare - by Szekemri - 11-30-2011, 05:42 PM
RE: Programare - by Zamolxis1 - 11-30-2011, 08:41 PM
RE: Programare - by Dastan - 12-05-2011, 10:06 PM
RE: Programare - by g3orge13 - 12-06-2011, 12:21 AM
RE: Programare - by Dastan - 12-06-2011, 04:55 PM
RE: Programare - by Zamolxis1 - 12-06-2011, 05:00 PM
RE: Programare - by Dastan - 12-06-2011, 10:38 PM
RE: Programare - by Zamolxis1 - 12-07-2011, 05:39 PM
RE: Programare - by Dastan - 12-07-2011, 05:55 PM
RE: Programare - by Zamolxis1 - 12-07-2011, 08:38 PM
RE: Programare - by Szekemri - 12-07-2011, 11:27 PM
RE: Programare - by Zamolxis1 - 12-08-2011, 05:05 PM
RE: Programare - by Altair xD - 12-12-2011, 04:47 PM
RE: Programare - by Zamolxis1 - 02-09-2012, 08:15 PM
RE: Programare - by Szekemri - 02-10-2012, 02:38 PM
RE: Programare - by Szekemri - 02-09-2012, 08:21 PM
RE: Programare - by Zamolxis1 - 02-09-2012, 08:24 PM
RE: Programare - by Zamolxis1 - 02-10-2012, 06:57 PM
RE: Programare - by Szekemri - 02-11-2012, 11:39 AM
RE: Programare - by Zamolxis1 - 02-11-2012, 08:21 PM
RE: Programare - by Zamolxis1 - 02-14-2012, 09:11 PM
RE: Programare - by Marius Titulescu - 03-21-2017, 12:05 PM

Forum Jump:


Users browsing this thread: 3 Guest(s)