[Work] ultra-practical C ++ class scores

introduction

We say that the essence of a programming language that package, and object-oriented languages victory cause of process-oriented language that has better encapsulation, and C ++ is such a multi-paradigm language, common and complex do not have to work at every a heavy knock in the source file, just as we do not need to own handwriting printf () and scanf (), as one of their own self-built headers can be a great help for the program, I'm going to recommend myself today original score category (in fact C ++ STL library has class scores, but low performance, and the use of extremely inconvenient, and my class this score has many advantages not available in the former).

Note: This is the original article, reproduced then be sure to explain the original source.

Code

The following code is stored in a file *** h, and adding in the source file in the same directory:. #Include "fraction" to use the fraction class (see method below).

  1  // - Arch Kai original
   2  // fraction class implements 0.8.5 ~~~
   . 3  @ fraction: Score 
   . 4  @ numerator: molecule
   . 5  @ denominator: the denominator
   6  // Please input end with a carriage return fraction
   7  / / input portion as contains a space, the space behind the part is ignored 
   . 8  // fraction may be abbreviated as FAC, the computer can recognize 
  . 9  
10 #include <the iostream>
 . 11 #include <the cmath>
 12 is  
13 is  #ifndef __fraction_h_ 
 14  #define __fraction_h_
 15  
16  the using  namespace STD;
 . 17  
18 is namespace std
 19 { 
 20 
 21 //分数类 
 22 class fraction
 23 {
 24 public:
 25     static long long MCF(long long a, long long b);
 26     
 27     friend ostream& operator<< (ostream& os,const fraction& the_fraction);
 28     friend istream& operator>> (istream& is,fraction& the_fraction);
 29     
 30     friend fraction operator+  (const fraction& the_fraction_a,const fraction& the_fraction_b);
 31     friend fraction operator-  (const fraction& the_fraction_a,const fraction& the_fraction_b);
 32     friend fraction operator*  (const fraction& the_fraction_a,const fraction& the_fraction_b);
 33     friend fraction operator/  (const fraction& the_fraction_a,const fraction& the_fraction_b);
 34     friend fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b);
 35     friend fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b);
 36     friend fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b);
 37     friend fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b);
 38     friend fraction operator-  (const fraction& the_fraction);
 39     friend fraction operator++ (fraction& the_fraction); 
 40     friend fraction operator++ (fraction& the_fraction,int);
 41     friend fraction operator-- (fraction& the_fraction);
 42     friend fraction operator-- (fraction& the_fraction,int);
 43     
 44     friend bool operator>  (fraction& the_fraction_a,const fraction& the_fraction_b);
 45     friend bool operator<  (fraction& the_fraction_a,const fraction& the_fraction_b);
 46     friend bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b);
 47     friend bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b);
 48     friend bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b);
 49     friend bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b);
 50     
 51     fraction();
 52     fraction(long long the_numerator);
 53     fraction(long long the_numerator,long long the_denominator);
 54     fraction(fraction the_fraction_a,fraction the_fraction_b);
 55     
 56     double decimal();
 57     long long getnum();
 58     long long getden();
 59     void setnum(long long num);
 60     void setden(long long den);
 61     
 62 private: 
 63     long long numerator;
 64     long long denominator; 
 65     
 66 };
 67 
 68 
 69 
 70 long long fraction::MCF(long long a, long long b)
 71 {
 72     return a==0? b:MCF(b%a,a);
 73 }
 74 
 75 
 76 
 77 ostream& operator<< (ostream& os,const fraction& the_fraction)
 78 {
 79     bool book=the_fraction.numerator>=0&&the_fraction.denominator>=0||the_fraction.numerator<0&&the_fraction.denominator<0;
 80     if(book==false)
 81         os<<"(-";
 82     if(the_fraction.denominator!=1)
 83         os<<abs(the_fraction.numerator)<<"/"<<abs(the_fraction.denominator);
 84     else
 85         os<<abs(the_fraction.numerator);
 86     if(book==false)
 87         os<<")";
 88     return os;
 89 }
 90 
 91 istream& operator>> (istream& is,fraction& the_fraction)
 92 {
 93     char input[100];
 94     char ch;
 95     bool mid=false;
 96     bool is_pt=true;
 97     the_fraction.numerator=the_fraction.denominator=0;
 98     cin>>input;
 99     for(int i=0;;i++)
100     {    
101         ch=input[i];
102         if(ch=='\0')
103             break;
104         if(ch=='-')
105         {
106             is_pt=!is_pt;
107             continue;
108         }
109         if(ch=='/')
110         {
111             mid=true;
112             continue;
113         }
114         if(mid==false)
115             the_fraction.numerator=the_fraction.numerator*10+(ch-'0');
116         else
117             the_fraction.denominator=the_fraction.denominator*10+(ch-'0');
118     }
119     if(mid==false)
120         the_fraction.denominator=1;
121     if(the_fraction.denominator==0)
122     {
123         cout<<"False,the denominator == 0!!!";
124         return is;
125     }    
126     long long mcf=fraction::MCF(the_fraction.numerator,the_fraction.denominator);
127     the_fraction.numerator/=mcf;
128     the_fraction.denominator/=mcf;
129     if(!is_pt)
130         the_fraction.numerator=-the_fraction.numerator;
131     return is;
132 }
133 
134 
135 
136 fraction operator+ (const fraction& the_fraction_a,const fraction& the_fraction_b)
137 {
138     long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator+the_fraction_b.numerator*the_fraction_a.denominator;
139     long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator;
140     return fraction(the_numerator,the_denominator);
141 }
142 
143 fraction operator- (const fraction& the_fraction_a,const fraction& the_fraction_b)
144 {
145     long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator-the_fraction_b.numerator*the_fraction_a.denominator;
146     long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator;
147     return fraction(the_numerator,the_denominator);
148 }
149 fraction operator* (const fraction& the_fraction_a,const fraction& the_fraction_b)
150 {
151     long long the_numerator=the_fraction_a.numerator*the_fraction_b.numerator;
152     long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator;
153     long long mcf=fraction::MCF(the_numerator,the_denominator);
154     the_numerator/=mcf;
155     the_denominator/=mcf;
156     fraction the_fraction(the_numerator,the_denominator);
157     return the_fraction;
158 }
159 
160 fraction operator/(const fraction& the_fraction_a,const fraction& the_fraction_b)
161 {
162     return the_fraction_a*fraction(the_fraction_b.denominator,the_fraction_b.numerator);
163 }
164 
165 fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b)
166 {
167     return the_fraction_a=the_fraction_a+the_fraction_b;
168 }
169 
170 fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b)
171 {
172     return the_fraction_a=the_fraction_a-the_fraction_b;
173 }
174 
175 fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b)
176 {
177     return the_fraction_a=the_fraction_a*the_fraction_b;
178 }
179 
180 fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b)
181 {
182     return the_fraction_a=the_fraction_a/the_fraction_b;
183 }
184 
185 fraction operator-  (const fraction& the_fraction)
186 {
187     return 0-the_fraction;
188 }
189 
190 fraction operator++ (fraction& the_fraction)
191 {
192     the_fraction=the_fraction+1;
193     return the_fraction;
194 }
195 fraction operator++ (fraction& the_fraction,int)
196 {
197     the_fraction=the_fraction+1;
198     return the_fraction-1;
199 }
200 fraction operator-- (fraction& the_fraction)
201 {
202     the_fraction=the_fraction-1;
203     return the_fraction;
204 }
205 fraction operator-- (fraction& the_fraction,int)
206 {
207     the_fraction=the_fraction-1;
208     return the_fraction+1;
209 }
210 
211 
212 
213 bool operator>  (fraction& the_fraction_a,const fraction& the_fraction_b)
214 {
215     return the_fraction_a.numerator*the_fraction_b.denominator>the_fraction_b.numerator*the_fraction_a.denominator;
216 }
217 bool operator<  (fraction& the_fraction_a,const fraction& the_fraction_b)
218 {
219     return the_fraction_a.numerator*the_fraction_b.denominator<the_fraction_b.numerator*the_fraction_a.denominator;
220 }
221 bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b)
222 {
223     return the_fraction_a.numerator*the_fraction_b.denominator>=the_fraction_b.numerator*the_fraction_a.denominator;
224 }
225 bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b)
226 {
227     return the_fraction_a.numerator*the_fraction_b.denominator<=the_fraction_b.numerator*the_fraction_a.denominator;
228 }
229 bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b)
230 {
231     return the_fraction_a.numerator*the_fraction_b.denominator==the_fraction_b.numerator*the_fraction_a.denominator;
232 }
233 bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b)
234 {
235     return the_fraction_a.numerator*the_fraction_b.denominator!=the_fraction_b.numerator*the_fraction_a.denominator;
236 }
237 
238 
239 
240 fraction::fraction()
241 {
242     numerator=0;
243     denominator=1;
244 }
245 
246 fraction::fraction(long long the_numerator)
247 {
248     numerator=the_numerator;
249     denominator=1;
250 }
251 
252 fraction::fraction(long long the_numerator,long long the_denominator)
253 {    
254     long long mcf=fraction::MCF(the_numerator,the_denominator);
255     numerator=the_numerator/mcf;
256     denominator=the_denominator/mcf;
257 }
258 
259 fraction::fraction(fraction the_fraction_a,fraction the_fraction_b)
260 {
261     long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator;
262     long long the_denominator=the_fraction_a.denominator*the_fraction_b.numerator;
263     long long mcf=fraction::MCF(the_numerator,the_denominator);
264     numerator=the_numerator/mcf;
265     denominator=the_denominator/mcf;
266 }
267 
268 double fraction::decimal()
269 {
270     return 1.0*numerator/denominator;
271 }
272 
273 long long fraction::getnum()
274 {
275     return numerator;
276 }
277 long long fraction::getden()
278 {
279     return denominator;
280 }
281 void fraction::setnum(long long num)
282 {
283     numerator=num;
284     long long mcf=fraction::MCF(numerator,denominator);
285     numerator/=mcf;
286     denominator/=mcf;
287 }
288 void fraction::setden(long long den)
289 {
290     if(den!=0)
291     {
292         denominator=den;
293         long long mcf=fraction::MCF(numerator,denominator);
294         numerator/=mcf;
295         denominator/=mcf;
296     }
297     else
298     {
299         cout<<"False,the denominator == 0!!!";
300     }
301 }
302 
303 typedef fraction fac;
304 
305 }
306 
307 #endif

 

Instructions

table of Contents

  • Brief introduction
  • A unary operator overloading
  • Binary operator overloading
  • Constructor
  • Member function
  • Data members
  • Precautions

Brief introduction

    An overloaded operators almost all classes of scores.

A unary operator overloading

friend fraction operator-  (const fraction& the_fraction);

friend fraction operator++ (fraction& the_fraction);

friend fraction operator++ (fraction& the_fraction,int);

friend fraction operator-- (fraction& the_fraction);

friend fraction operator-- (fraction& the_fraction,int);

These friend function overloading the pre and post-increment decrement operators, negation and operators, such as a is a fraction, represents the inverse of a, -a, a ++ / a-- represents postincrement / diminishing, ++ a / - a representation front increment / decrement.

Binary operator overloading

friend ostream& operator<< (ostream& os,const fraction& the_fraction);

friend istream& operator>> (istream& is,fraction& the_fraction);

The input and output function overloading the operator, so that it can be used cin / cout objects, such as a class object is a fractional value equal to -4 / 3, cout << a, will be displayed on the screen (-4/3 ), cin >> a read plus an integer plus a fraction of a line integer, such as 42/43, to enter after the end of the transport, must not end with a space, the former can be any integer plus a fraction of a negative number has been entered ( If the input -4 / -3, the storage system by an integer, i.e., 4/3).

friend fraction operator+  (const fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator-  (const fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator*  (const fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator/  (const fraction& the_fraction_a,const fraction& the_fraction_b);

This overloads the four operators + - * / four arithmetic operations, so that it can be used for fraction (if not the most simple fraction calculation result, the system will automatically reduce fractions).

friend fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b);

friend fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b);

These four function overloading + =, - =, * =, / = compound operator, so that it can be used for the score, the expression returns the value of its right.

friend bool operator>  (fraction& the_fraction_a,const fraction& the_fraction_b);

friend bool operator<  (fraction& the_fraction_a,const fraction& the_fraction_b);

friend bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b);

friend bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b);

friend bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b);

friend bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b);

The six function overloading the relational operators, so that it can be used to score.

Constructor

fraction();

This is the default constructor to initialize the fraction 0/1.

fraction(long long the_numerator);

This conversion is a constructor may be a long long type of data into a fraction, but also can display the call, such as: fraction a (10); 10/1 creates a score value.

fraction(long long the_numerator,long long the_denominator);

fraction(fraction the_fraction_a,fraction the_fraction_b);

This is the most important structural function, as the quotient of the two values ​​of the parameter score.

Such as: fraction a (150,100) creates a fractional value of 3/2.

Such as: fraction a (fraction (2,3), fraction (3,2)) creates a fractional value of 1/1.

Member function

double decimal();

Returns the numerator by the denominator of double value.

long long getnum();

Return Value molecule.

long long getden();

Returns the value of the denominator.

void substituted (long long num);

It accepts a parameter, and assigns molecule (automatically reduced).

void setden(long long den);

Receiving a non-zero parameter, and assigns the denominator (automatically reduced) (If the parameter is 0, the denominator will not be assigned, and outputs an error message in the standard output stream: "False, the denominator == 0 !! ! ").

Data members

Molecule: Long Long numerator;

Denominator: Long Long denominator;

Static member function

static long long MCF(long long a, long long b);

This function takes two long long, and returns a value equal to the value of the long long, their greatest common factor. The fraction :: MCF (2,4) is 2.

Precautions

  When using a fraction fraction of all the relevant code can be abbreviated as fac, both fully equivalent.

  The header file class scores with the use of its source code files are saved in the same directory, header files to compile it, and then compile the source code file, or there will be a compilation error, the first compiled file is changed or if transferred to another after a directory must also be recompiled, or there will be a compile error! ! !

 

postscript

  This is the original article without permission is forbidden! ! ! (Now I can explain, but to indicate the source)

Guess you like

Origin www.cnblogs.com/gongkai/p/10806080.html