Almetare  1.15
Alle meine Taschenrechner - Eine C++-Bibliothek zur Entwicklung von Taschenrechnern
fraction.h
gehe zur Dokumentation dieser Datei
1 //******************************************************************************
2 // Copyright (c) 2002-2004 by Friedemann Seebass, Germany.
3 // Dieses Programm ist freie Software. Siehe GNU GPL Notiz in Datei main.cpp.
4 // This program is free software. See the GNU GPL notice in file main.cpp.
5 // Projekt: Almetare - Alle meine Taschenrechner
6 //******************************************************************************
9 //******************************************************************************
10 // Wann Wer Was
11 // ---------- -------- ---------------------------------------------------------
12 // 08.03.2003 fse neue Operatoren <= und >=
13 // 20.04.2002 fse erzeugt
14 //******************************************************************************
15 
16 #ifndef FRACTION_H
17 #define FRACTION_H
18 
19 #ifndef __cplusplus
20 #error fraction.h is only for C++!
21 #endif
22 
23 //******************************************************************************
24 
25 #include <iostream>
26 #include <string>
27 
28 using namespace std;
29 
30 //******************************************************************************
31 // Klasse ZeroDevide:
32 //******************************************************************************
33 
57 // fse, 09.06.02
58 
60 {
61 };
62 
63 //******************************************************************************
64 // Klasse Fraction:
65 //******************************************************************************
66 
81 
82 //fse, 18.04.02
83 
84 class Fraction
85 {
86  long mNumr;
87  long mDnom;
88  static bool mIsShortened;
89 public:
90  Fraction() : mNumr(0), mDnom(1) {};
91  Fraction(long n, long d = 1) throw (ZeroDivide);
92  Fraction(const Fraction& f) : mNumr(f.mNumr), mDnom(f.mDnom) {}
93  long getNumr() const { return mNumr; }
94  long getDnom() const { return mDnom; }
95  long gcd() const;
96  long lcm() const;
97  void shorten();
98  Fraction rcp() const throw (ZeroDivide) { Fraction f(mDnom, mNumr); return f; }
99  double getVal() const { return static_cast<double>(mNumr)/static_cast<double>(mDnom); }
100  void getMixedFraction(long& whole, Fraction& f) const;
101  string getMixedFraction() const;
102  string getFraction() const;
103  bool isOverflow() const { return abs(mNumr) > 10000 || abs(mDnom) > 10000; }
104  static void setShortened(bool is) { mIsShortened = is; }
105  // Operatoren:
106  Fraction& operator*=(const Fraction& f);
107  Fraction& operator/=(const Fraction& f) throw (ZeroDivide);
108  Fraction& operator+=(const Fraction& f);
109  Fraction& operator-=(const Fraction& f);
110  friend Fraction operator* (const Fraction& f1, const Fraction& f2) { Fraction ret(f1); ret *= f2; return ret; }
111  friend Fraction operator/ (const Fraction& f1, const Fraction& f2) throw (ZeroDivide) { Fraction ret(f1); ret /= f2; return ret; }
112  friend Fraction operator+ (const Fraction& f1, const Fraction& f2) { Fraction ret(f1); ret += f2; return ret; }
113  friend Fraction operator- (const Fraction& f1, const Fraction& f2) { Fraction ret(f1); ret -= f2; return ret; }
114  friend bool operator==(const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom == f1.mDnom*f2.mNumr; }
115  friend bool operator!=(const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom != f1.mDnom*f2.mNumr; }
116  friend bool operator< (const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom < f1.mDnom*f2.mNumr; }
117  friend bool operator<=(const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom <= f1.mDnom*f2.mNumr; }
118  friend bool operator> (const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom > f1.mDnom*f2.mNumr; }
119  friend bool operator>=(const Fraction& f1, const Fraction& f2) { return f1.mNumr*f2.mDnom >= f1.mDnom*f2.mNumr; }
120  friend ostream& operator<<(ostream& os, const Fraction& f);
121 };
122 
123 //******************************************************************************
124 
125 #endif // !FRACTION_H
ostream & operator<<(ostream &os, const Error &e)
Ausgabeoperator, wandelt enum ErrId in Strings.
Definition: calculator.cpp:68
long mNumr
Zaehler (numerator)
Definition: fraction.h:86
Klasse zum Rechnen mit Bruechen.
Definition: fraction.h:84
Klasse zur Ausnahmebehandlung "Division durch 0".
Definition: fraction.h:59
long mDnom
Nenner (denominator)
Definition: fraction.h:87
bool operator!=(const Number &n1, const Number &n2)
Pruefung auf Ungleichheit.
Definition: number.cpp:299
static bool mIsShortened
gibt an, ob Bruch gekuerzt werden soll
Definition: fraction.h:88
bool operator<=(const Number &n1, const Number &n2)
Operator "kleiner als oder gleich".
Definition: number.cpp:338
bool operator>(const Number &n1, const Number &n2)
Operator "groesser als".
Definition: number.cpp:354
bool operator==(const Number &n1, const Number &n2)
Vergleichsoperator.
Definition: number.cpp:283
bool operator<(const Number &n1, const Number &n2)
Operator "kleiner als".
Definition: number.cpp:322
bool operator>=(const Number &n1, const Number &n2)
Operator "groesser als oder gleich".
Definition: number.cpp:370