File indexing completed on 2024-04-14 03:40:27

0001 /*
0002     Ratio.h  -  source code of class Ratio
0003     SPDX-FileCopyrightText: 2001-2004 Sebastian Stein <seb.kde@hpfsc.de>
0004     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0005     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef RATIO_H
0011 #define RATIO_H
0012 
0013 #include <QTextStream>
0014 
0015 /** Represents a ratio
0016  *  This class represents 1 ratio. There are several functions provided to
0017  *  modify the numerator and denominator. It is also possible to calculate with
0018  *  objects of the class ratio. Overloaded operation functions are provided for
0019  *  this task.
0020  *  \author Sebastian Stein */
0021 class Ratio
0022 {
0023 public:
0024     /** constructor */
0025     explicit Ratio(int pnumerator = 0, int pdenominator = 1);
0026 
0027     /** constructor with reduce option */
0028     Ratio(int pnumerator, int pdenominator, bool reduce_fraction);
0029 
0030     /** destructor */
0031     ~Ratio();
0032 
0033     /** returns the ratio as QTextStream object */
0034     QTextStream & display(QTextStream & str) const;
0035 
0036     /** returns the numerator */
0037     int numerator() const;
0038 
0039     /** returns the denominator */
0040     int denominator() const;
0041 
0042     /** set numerator and reduce the ratio */
0043     void setNumerator(int pnumerator = 0, bool reduce = true);
0044 
0045     /** set denominator and reduce the ratio */
0046     void setDenominator(int pdenominator = 1, bool reduce = true);
0047 
0048     /** set both numerator and denominator;
0049      *  reducing will be done after numerator and denominator were set!
0050      */
0051     void setRatio(int pnumerator, int pdenominator, bool reduce = true);
0052 
0053     /** set new ratio using mixed numbers;
0054      *  reducing will be done after numerator and denominator were set!
0055      */
0056     void setRatio(int pinteger, int pnumerator, int pdenominator, bool reduce = true);
0057 
0058     /** operator overloading for: c = object + summand */
0059     Ratio operator+ (const Ratio &addend);
0060 
0061     /** operator overloading for: c = object - subtrahend */
0062     Ratio operator- (Ratio subtrahend);
0063 
0064     /** operator overloading for: c = object * factor */
0065     Ratio operator*(const Ratio &factor);
0066 
0067     /** operator overloading for: c = object / divisor */
0068     Ratio operator/ (Ratio divisor);
0069 
0070     /** set numerator with dummy and denominator = 1 */
0071     Ratio operator= (int dummy);
0072 
0073     /** compares the current ratio with a given one */
0074     bool operator== (const Ratio &right);
0075 
0076     /** compares the current ratio with a given one */
0077     bool operator< (const Ratio &right);
0078 
0079     /** compares the current ratio with a given one */
0080     bool operator> (const Ratio &right);
0081 
0082     /** exchange numerator and denominator */
0083     void reziproc();
0084 
0085     /** reduce the ratio */
0086     void reduce();
0087 
0088 private:
0089     /** numerator */
0090     int m_numerator;
0091 
0092     /** denominator */
0093     int m_denominator;
0094 
0095     /** change sign of the ratio */
0096     void change_sign();
0097 }
0098 ;
0099 
0100 /* ------ some prototyps of non class functions ------ */
0101 
0102 /** it is possible to code: cout << ratio_object << endl; */
0103 QTextStream & operator<< (QTextStream & str, const Ratio & pratio);
0104 
0105 #endif