{"id":177061,"date":"2013-04-17T19:56:04","date_gmt":"2013-04-17T15:56:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=177061"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=177061","title":{"rendered":"<span class=\"post_title\">\u041a\u043b\u0430\u0441\u0441 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043d\u0430 C++<\/span>"},"content":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u0430 C++ \u2014 \u043a\u043b\u0430\u0441\u0441\u0430 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u0411\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0443\u043d\u0430\u0440\u043d\u044b\u0445 \u0438 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0430\u043a \u0447\u043b\u0435\u043d\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 istream \u0438 ostream, \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 (friend) \u0444\u0443\u043d\u043a\u0446\u0438\u0439. <br \/>  \u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u043b\u0430\u0441\u0441\u0430\u0445, \u0432 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0435 \u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445 \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  \u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 \u0441\u0440\u0435\u0434\u0435 NetBeans 7.3 (Ubuntu 12.10). <\/p>\n<h4>\u041d\u0430\u0447\u0430\u043b\u043e<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c 3 \u0444\u0430\u0439\u043b\u0430 \u0432 \u0441\u0440\u0435\u0434\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 \u0447\u0430\u0441\u0442\u0438. <\/p>\n<ul>\n<li>main.cpp \u2014 \u0442\u0435\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/li>\n<li>realize.cpp \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430<\/li>\n<li>rational.h \u2014 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430<\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>rational.h<\/h4>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u041d\u0430\u0437\u043e\u0432\u0435\u043c \u0435\u0433\u043e <b>rat<\/b>.<\/p>\n<pre><code class=\"cpp\">#ifndef RATIONAL_H  #define\tRATIONAL_H  \/\/ \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430  class rat {  int ch; \/\/ \u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c int zn; \/\/ \u0437\u043d\u0430\u043c\u0435\u043d\u0430\u0442\u0435\u043b\u044c friend istream &operator&gt;&gt;(istream&, rat&); \/\/ \u0432\u0432\u043e\u0434 friend ostream &operator&lt;&lt;(ostream&, rat&); \/\/\u0432\u044b\u0432\u043e\u0434 int nod(int, int);  \/\/ \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043e\u0431\u0449\u0438\u0439 \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044c int abs(int); \/\/ \u043c\u043e\u0434\u0443\u043b\u044c \u0447\u0438\u0441\u043b\u0430 public: rat(int=0, int=1); \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 void norm(); \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0438\u0441\u043b\u0430  rat operator*(const rat&) const; \/\/ \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 (\u0434\u0440\u043e\u0431\u043d\u044b\u0445) \u0447\u0438\u0441\u0435\u043b rat operator+(const rat&) const; \/\/ \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435  \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 (\u0434\u0440\u043e\u0431\u043d\u044b\u0445) \u0447\u0438\u0441\u0435\u043b rat operator-(const rat&) const; \/\/ \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 (\u0434\u0440\u043e\u0431\u043d\u044b\u0445) \u0447\u0438\u0441\u0435\u043b rat operator\/(const rat&) const; \/\/ \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 (\u0434\u0440\u043e\u0431\u043d\u044b\u0445) \u0447\u0438\u0441\u0435\u043b  }; #endif <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 ch \u0438 zn \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c \u0438 \u0437\u043d\u0430\u043c\u0435\u043d\u0430\u0442\u0435\u043b\u044c \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430. \u0424\u0443\u043d\u043a\u0446\u0438\u0438 nod() \u0438 abs() \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 private, \u0447\u0442\u043e\u0431\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0432\u043d\u0435 \u043a\u043b\u0430\u0441\u0441\u0430, \u0438\u0431\u043e \u043e\u043d\u0438 \u043d\u0430\u043c \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b. <\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u0438 istream (&gt;&gt;) \u0438 ostream (&lt;&lt;) \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u2014 \u043a\u0430\u043a \u0447\u043b\u0435\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0430. <br \/>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 +,-,*,\/ \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u043e \u043d\u0435 &gt;&gt; \u0438 &lt;&lt;. <\/p>\n<p>  \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438\u0445 \u043a\u0430\u043a \u0447\u043b\u0435\u043d\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442\u043e\u0433\u0434\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0438\u043f\u0430 rat, \u043d\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0442\u0438\u043f istream \u0438\u043b\u0438 ostream. <\/p>\n<h4>main.cpp<\/h4>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0435\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 rat.<\/p>\n<pre><code class=\"cpp\">#include &lt;iostream&gt;  #include &quot;rational.h&quot; using namespace std; int main() { rat a, b; \/\/  \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0442\u0438\u043f\u0430 rat   cin &gt;&gt;a;  \/\/ \u0432\u0432\u043e\u0434 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 cin &gt;&gt; b; \/\/ \u0432\u0432\u043e\u0434 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 cout &lt;&lt; a+b &lt;&lt; endl; \/\/ \u0441\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b return 0; } <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b \u043d\u0430\u0432\u0435\u0440\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0442\u0435\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043e. \u041c\u044b \u043d\u0435 \u0441\u0442\u0430\u043b\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u0430, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438. \u042d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432 \u041e\u041e\u041f. <\/p>\n<h4>realize.cpp<\/h4>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u044e\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0430\u0439\u043b\u0430 realize.cpp<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\"> #include &lt;iostream&gt; #include &quot;rational.h&quot; using namespace std; int rat::abs(int x) { if(x&lt;0) return (-1)*x; return x; } int rat::nod(int x, int y) { if(x==0 || y==0) return 1; while(x!=y) if(x&gt;y) x=x-y; else y=y-x; return x; } rat::rat(int x, int y) { if(!y || !x) {ch=0;zn=1;} ch=x; zn=y; this-&gt;norm(); }  void rat::norm() {  int sign=1;  if(ch*zn&lt; 0) sign=(-1); ch=abs(ch); zn=abs(zn); int b=nod(ch, zn); if(ch==0 || zn==0) zn=1; if(b==1) return; ch=(ch\/b)*sign; zn=zn\/b;  } rat rat::operator*(const rat& rvalue) const { rat temp; temp.ch=this-&gt;ch * rvalue.ch;  temp.zn=this-&gt;zn * rvalue.zn;  temp.norm(); return temp; } rat rat::operator+(const rat& rvalue) const { rat temp; temp.ch=rvalue.zn*this-&gt;ch + rvalue.ch*this-&gt;zn; temp.zn=this-&gt;zn * rvalue.zn;  temp.norm(); return temp; } rat rat::operator-(const rat& rvalue) const { rat temp; temp.ch=rvalue.zn*this-&gt;ch - rvalue.ch*this-&gt;zn; temp.zn=this-&gt;zn * rvalue.zn;  temp.norm(); return temp; } rat rat::operator\/(const rat& rvalue) const { rat temp; temp.ch=this-&gt;ch* rvalue.zn; temp.hay=this-&gt;zn * rvalue.ch;  temp.norm(); return temp; }  istream &operator&gt;&gt;(istream &stream_in, rat &rvalue)  { cout &lt;&lt; &quot;Enter rational value (a\/b): &quot;; stream_in &gt;&gt; rvalue.ch; stream_in.ignore(); stream_in &gt;&gt; rvalue.zn; cout &lt;&lt; endl; return stream_in;  }  ostream &operator&lt;&lt;(ostream &stream_out, rat &rvalue)  { stream_out &lt;&lt; &quot;Rational number: &quot; &lt;&lt; rvalue.ch &lt;&lt; &quot;\/&quot; &lt;&lt; rvalue.zn &lt;&lt;endl; return stream_out; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043a\u043e\u0434 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c.<\/p>\n<h6>\u041c\u043e\u0434\u0443\u043b\u044c, \u041d\u041e\u0414 \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/h6>\n<pre><code class=\"cpp\">\/\/ abs \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 x \u0434\u043b\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0438 -x \u0434\u043b\u044f \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445  int rat::abs(int x) { if(x&lt;0) return (-1)*x; return x; }  \/\/ nod() \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043e\u0431\u0449\u0438\u0439 \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044c x \u0438 y int rat::nod(int x, int y) { if(x==0 || y==0) return 1; while(x!=y) if(x&gt;y) x=x-y; else y=y-x; return x; } \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0440\u0430\u0446. \u0447\u0438\u0441\u043b\u043e \/\/ \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 0\/1 rat::rat(int x, int y) { if(!y || !x) {ch=0;zn=1;} ch=x; zn=y; this-&gt;norm(); } <\/code><\/pre>\n<p>  \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e norm() \u0434\u043b\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0434\u0440\u043e\u0431\u0438.<\/p>\n<pre><code class=\"cpp\">void rat::norm() {  int sign=1;  if(ch*zn &lt; 0) sign=(-1); ch=abs(ch); zn=abs(zn); int b=nod(ch, zn); if(ch==0 || zn==0) hay=1; if(b==1) return; ch=(ch\/b)*sign; zn=zn\/b;  } <\/code><\/pre>\n<p>  \u0418\u0434\u0435\u044f \u043f\u0440\u043e\u0441\u0442\u0430 \u2014 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0437\u043d\u0430\u043a \u0434\u0440\u043e\u0431\u0438, \u0434\u0435\u043b\u0438\u043c \u0437\u043d\u0430\u043c\u0435\u043d\u0430\u0442\u0435\u043b\u044c \u0438 \u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c \u043d\u0430 \u041d\u041e\u0414, \u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c \u043d\u0430 \u0437\u043d\u0430\u043a \u0434\u0440\u043e\u0431\u0438.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f (\u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 +).<\/p>\n<pre><code class=\"cpp\">rat rat::operator+(const rat& rvalue) const { rat temp; temp.ch=rvalue.zn*this-&gt;ch + rvalue.ch*this-&gt;zn; temp.hay=this-&gt;zn * rvalue.zn;  temp.norm(); return temp; } <\/code><\/pre>\n<p>  \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f operator+() \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0442\u0438\u043f\u0430 rat, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 rat. <\/p>\n<p>  \u041d\u043e \u0433\u0434\u0435 \u0436\u0435 \u0432\u0442\u043e\u0440\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u043d\u0434 \u0434\u043b\u044f \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f? <br \/>  \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0410 + \u0411, \u043b\u0435\u0432\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u043d\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c (*this), \u0430 \u0411 \u2014 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 operator+().<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u043c \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u0430\u043a friend, \u0442\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442, \u0438\u0431\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e operator+() \u0438 \u043f\u0435\u0440\u0432\u044b\u0439, \u0438 \u0432\u0442\u043e\u0440\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u043d\u0434. <\/p>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438 &gt;&gt; \u0438 &lt;&lt;. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439 \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c &gt;&gt; (\u0432\u0432\u043e\u0434).<\/p>\n<pre><code class=\"cpp\">istream &operator&gt;&gt;(istream &stream_in, rat &rvalue) { cout &lt;&lt; &quot;Enter rational value (a\/b): &quot;; stream_in &gt;&gt; rvalue.ch; stream_in.ignore(); stream_in &gt;&gt; rvalue.zn; cout &lt;&lt; endl; return stream_in; } <\/code><\/pre>\n<p>  \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e operator&gt;&gt;() \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430. \u041f\u0435\u0440\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 istream (\u043f\u043e\u0442\u043e\u043a \u0432\u0432\u043e\u0434\u0430), \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u043e\u043f\u0435\u0440\u0430\u043d\u0434 \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u0430 rat.<br \/>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438, \u0430 \u043d\u0435 \u043f\u0440\u044f\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u0442\u043e\u043a\u0430. \t\t\t \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/177061\/\"> http:\/\/habrahabr.ru\/post\/177061\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u0430 C++ \u2014 \u043a\u043b\u0430\u0441\u0441\u0430 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u0411\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0443\u043d\u0430\u0440\u043d\u044b\u0445 \u0438 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0430\u043a \u0447\u043b\u0435\u043d\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 istream \u0438 ostream, \u043a\u0430\u043a \u0434\u0440\u0443\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 (friend) \u0444\u0443\u043d\u043a\u0446\u0438\u0439. <br \/>  \u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u043b\u0430\u0441\u0441\u0430\u0445, \u0432 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0435 \u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445 \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  \u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 \u0441\u0440\u0435\u0434\u0435 NetBeans 7.3 (Ubuntu 12.10). <\/p>\n<h4>\u041d\u0430\u0447\u0430\u043b\u043e<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c 3 \u0444\u0430\u0439\u043b\u0430 \u0432 \u0441\u0440\u0435\u0434\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 \u0447\u0430\u0441\u0442\u0438. <\/p>\n<ul>\n<li>main.cpp \u2014 \u0442\u0435\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/li>\n<li>realize.cpp \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430<\/li>\n<li>rational.h \u2014 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430<\/li>\n<\/ul>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-177061","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/177061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=177061"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/177061\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=177061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=177061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=177061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}