Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programare
#1
Acest thread il vom folosi pentru a vorbi numai si numai de programare...de ex daca mai este cineva l mate info sau in situatia lui Terente(vorbind de facultate)ne vom dezbate pe teme gen algoritmi s.a. ...asta pt cei care vorSmile...sa speram ca va fii un thread folositorWink
"Errare humanum est sed perseverare diabolicum."
"Gutta cavat lapidem non vi, sed saepe cadendo."
"Inter arma silent leges."

replyReply
#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
#3
Da' ce programe folosiţi? La mine la şcoală e Borland de c++.
replyReply
#4
Eu is la liceu de informatica si vreau sa termin o facultate de informatica.Din pacate,nu sunt tocmai un geniu si am de exersat Sad,n-am probleme de facut acasa etc.
replyReply
#5
(10-14-2011, 09:16 PM)Altair Wrote: Eu is la liceu de informatica si vreau sa termin o facultate de informatica.Din pacate,nu sunt tocmai un geniu si am de exersat Sad,n-am probleme de facut acasa etc.

Same here...Tongue
replyReply
#6
Chestia ii ca mie chiar imi place informatica(nu insinuez ca tie nu),dar nu stiu de unde sa gasesc niste probleme de rezolvat.
replyReply
#7
(10-14-2011, 09:05 PM)g3orge13 Wrote: Da' ce programe folosiţi? La mine la şcoală e Borland de c++.

Eu am folosit pana acum: c/c++, Pascal, java si anu' asta urmeaza c Sharp.
(10-14-2011, 09:21 PM)Altair Wrote: Chestia ii ca mie chiar imi place informatica(nu insinuez ca tie nu),dar nu stiu de unde sa gasesc niste probleme de rezolvat.

WHAT ABOUT HERE
----------------Worldofgothic----------------
---------PLAY
replyReply
#8
Eu folosesc MinGW Developer Studio. Grin
replyReply
#9
ce ai scris tu acolo e c++...imi place c++ si Pascal...imi place pascal fiindca vara trecuta am facut programele pe delphiWink...si cred ca e acelasi limbajHaha...dar terente imi explici si mie te rog ce ai scris acolo...ca pe noi liceenii ne depasesteSmile
si am o problema cu c++...nu stiu cand ii la cout mesaj si cand mesaju defineste un termen bagat la int...de ex cand cout<<mesaj<<x; sau cand cout<<mesaj;...app manualu nostru are 70 de pagini(nu-i de mirare ca e greu de invatat din el) si tocmai mi-am comandat un manual de a9a scris de Tudor Sorin...am auzit ca tipu e super bun...si ala are 250 de pagini(am gasit cartile lui pe un forum din intamplare cand incercam sa pricep c++Grin
"Errare humanum est sed perseverare diabolicum."
"Gutta cavat lapidem non vi, sed saepe cadendo."
"Inter arma silent leges."

replyReply
#10
(10-15-2011, 05:42 PM)Zamolxis1 Wrote: ce ai scris tu acolo e c++...imi place c++ si Pascal...imi place pascal fiindca vara trecuta am facut programele pe delphiWink...si cred ca e acelasi limbajHaha...dar terente imi explici si mie te rog ce ai scris acolo...ca pe noi liceenii ne depasesteSmile
si am o problema cu c++...nu stiu cand ii la cout mesaj si cand mesaju defineste un termen bagat la int...de ex cand cout<<mesaj<<x; sau cand cout<<mesaj;...app manualu nostru are 70 de pagini(nu-i de mirare ca e greu de invatat din el) si tocmai mi-am comandat un manual de a9a scris de Tudor Sorin...am auzit ca tipu e super bun...si ala are 250 de pagini(am gasit cartile lui pe un forum din intamplare cand incercam sa pricep c++Grin

Programul de mai sus include cam tot ce inveti in anul 1 de fac despre clase and stuff. In mare e o supradefinire de operatori. Smile

Nu inteleg exact ce vroiai sa zici, dar cout<<"mesaju"<<int-u sau ce vrei tu; -asa ar trebui sa arate. Scrie codu' mai bine.



----------------Worldofgothic----------------
---------PLAY
replyReply


Forum Jump:


Users browsing this thread: 2 Guest(s)