Опытный
 
Профиль
Группа: Участник
Сообщений: 740
Регистрация: 12.1.2008
Где: Germany
Репутация: 1 Всего: 1
|
djamshud - Че - т сложновато написано. Но попробую разобрать, неохота уже сегодня половину задачи переделывать :( А фашистский приходится использовать так как учусь здесь.  Хочется как можно лучше подправить и все. Вот такая у меня структура если целиком - может кому пригодиться, все считает четко.  Код | #include <iostream> #include "RationaleZahl.h"
using namespace std;
void test1() { // Test der Arithmetik cout << "Testprogramm 1:" << endl; RationaleZahl a = {'+',4,7}; // +4/7 RationaleZahl b = {'-',1,4}; // -1/4 RationaleZahl c = {'+',5,6}; // +5/6 cout << b << '+' << b << '=' << b+b << endl; cout << b << '+' << c << '='<< b+c << endl; cout << c << '+' << b << '='<< c+b << endl; cout << b << '-' << c << '='<< b-c << endl; cout << "a*c = "<< a*c << endl; cout << "b/a = "<< b/a << endl; }
void test2() { // Test unbekannte Ausnahme // cout <<"\n\n"<< "Testprogramm 2:" // << " int-Ausnahme. (Sollte als unbekannte Ausnahme gefangen werden)" // <<"\n\n"<< endl; throw int(10); }
void test3() { cout << "Testprogramm 3!!:" << endl; RationaleZahl a = {'+', 1, 3}; // +1/3 RationaleZahl b = {'+', 1, 4}; // +1/4 RationaleZahl c = {'+', 2, 7}; // +2/7 RationaleZahl d = {'+', 3, 5}; // +3/5 RationaleZahl e = {'-', 1, 3}; // -1/3 RationaleZahl f = {'-', 1, 4}; // -1/4 RationaleZahl g = {'-', 2, 7}; // -2/7 RationaleZahl h = {'-', 3, 5}; // -3/5 cout << a << '+' << b << '=' << a + b << endl; cout << c << '+' << d << '=' << c + d << endl; cout << f << '+' << a << '=' << f + a << endl; cout << e << '+' << b << '=' << e + b << endl; cout << g << '+' << d << '=' << g + d << endl; cout << h << '+' << c << '=' << h + c << endl; cout << f << '+' << e << '=' << f + e << endl; cout << e << '+' << f << '=' << e + f << endl; cout << h << '+' << c << '=' << h + c << endl; cout << g << '+' << h << '=' << g + h << endl; cout << b << '+' << e << '=' << b + e << endl; cout << a << '+' << f << '=' << a + f << endl; cout << c << '+' << h << '=' << c + h << endl; cout << d << '+' << g << '=' << d + g << endl; }
void test4() { // Test ... cout << "Testprogramm 4" << endl; }
void test5() { cout << "Testprogramm 5:" << endl; RationaleZahl a = {'+', 1, 3}; // +1/3 RationaleZahl b = {'+', 1, 4}; // +1/4 RationaleZahl c = {'+', 4, 9}; // +4/9 RationaleZahl d = {'+', 3, 10}; // +3/10 RationaleZahl e = {'-', 1, 3}; // -1/3 RationaleZahl f = {'-', 1, 4}; // -1/4 RationaleZahl g = {'-', 4, 9}; // -4/9 RationaleZahl h = {'-', 3, 10}; // -3/10 cout << a << '-' << b << '=' << a - b << endl; cout << c << '-' << d << '=' << c - d << endl; cout << b << '-' << a << '=' << b - a << endl; cout << d << '-' << c << '=' << d - c << endl; cout << h << '-' << b << '=' << h - b << endl; cout << f << '-' << d << '=' << f - d << endl; cout << h << '-' << g << '=' << h - g << endl; cout << g << '-' << h << '=' << g - h << endl; }
void test6() { // Test ... cout << "Testprogramm 6" << endl; } void test7() { // Test ... cout << "Testprogramm 7" << endl; RationaleZahl a = {'+', 1, 3}; // +1/3 RationaleZahl b = {'*', 1, 4}; // +1/4 RationaleZahl c = {'+', 4, 9}; // +4/9 RationaleZahl d = {'+', 3, 10}; // +3/10 RationaleZahl e = {'-', 1, 3}; // -1/3 RationaleZahl f = {'-', 1, 4}; // -1/4 RationaleZahl g = {'-', 4, 9}; // -4/9 RationaleZahl h = {'-', 3, 10}; // -3/10 cout << a << '*' << b << '=' << a * b << endl; cout << c << '/' << d << '=' << c / d << endl; cout << b << '*' << a << '=' << b * a << endl; cout << d << '/' << c << '=' << d / c << endl; cout << h << '*' << b << '=' << h * b << endl; cout << f << '/' << d << '=' << f / d << endl; cout << h << '*' << g << '=' << h * g << endl; cout << g << '/' << h << '=' << g / h << endl; } void test8() { // Test ... cout << "Testprogramm 8" << endl; RationaleZahl a = {'+', 1, 0}; // +1/0 RationaleZahl b = {'-', 0, 3}; // -0/3 RationaleZahl c = {'+', 0, 0}; // +0/0 RationaleZahl d = {'-', 4, 0}; // -4/0 //cout << a << '/' << c << '=' << a / c << endl; cout << b << '/' << c << '=' << b / c << endl; cout << c << '*' << d << '=' << c * d << endl; cout << a << '*' << c << '=' << a * c << endl; cout << c << '*' << c << '=' << c * c << endl; cout << c << '+' << c << '=' << c + c << endl;
}
int main() { void (*testfkts[])(void) = {test1, test2, test3, test4, test5, test6, test7, test8}; for (int i=0;i<sizeof(testfkts)/sizeof(void (*)(void));i++) { try { (*testfkts[i])(); } catch (UnendlichDurchUnendlich) { cout << "--> Ausnahme: Unendlich durch Unendlich" << endl; } catch (NullDurchNull) { cout << "--> Ausnahme: 0 durch 0" << endl; } catch (NullMalUnendlich) { cout << "--> Ausnahme: 0 mal unendlich" << endl; } catch (MinusUnendlichUndPlusUnendlich) { cout << "--> Ausnahme: -unendlich + +unendlich" << endl; } catch (KeineZahl) { cout << "--> Ausnahme: Keine Zahl" << endl; } catch (FalschesVorzeichen) { cout << "--> Ausnahme: FalschesVorzeichen" << endl; } catch (...) { cout << "--> Ausnahme: Unbekannte Ausnahme" << endl; } } }
|
Код | #include <iostream>
using namespace std;
struct UnendlichDurchUnendlich{}; struct NullDurchNull{}; struct NullMalUnendlich{}; struct MinusUnendlichUndPlusUnendlich{}; struct KeineZahl{}; struct FalschesVorzeichen{}; struct UeberlaufImZaehler{}; // Freiwillig struct UeberlaufImNenner{}; // Freiwillig struct UeberlaufImZaehlerUndNenner{}; // Freiwillig
// // Vereinbarung des Benutzerdefinierten Datentyps "Rationale Zahl" // struct RationaleZahl { char Vorzeichen; unsigned int Zaehler, Nenner; };
RationaleZahl &operator +(const RationaleZahl &l, const RationaleZahl &r); RationaleZahl &operator -(const RationaleZahl &l, const RationaleZahl &r); RationaleZahl &operator *(const RationaleZahl &l, const RationaleZahl &r); RationaleZahl &operator /(const RationaleZahl &l, const RationaleZahl &r);
ostream &operator << (ostream & o, const RationaleZahl &r); // benoetigt <iostream>
void event (char, const RationaleZahl&, const RationaleZahl&); void reduce(RationaleZahl&); unsigned int ggT(unsigned int, unsigned int); RationaleZahl& fallZeichen(RationaleZahl&, const RationaleZahl&, const RationaleZahl&,int);
|
Код | #include <iostream> #include "RationaleZahl.h"
using namespace std;
RationaleZahl & operator +(const RationaleZahl &l, const RationaleZahl &r) {
event('+', l, r);
static RationaleZahl result; double x1 = static_cast<double> (l.Zaehler) / l.Nenner, x2 = static_cast<double> (r.Zaehler) / r.Nenner;
if (l.Vorzeichen == r.Vorzeichen) { result = fallZeichen(result, l, r, 1); } else if ((l.Vorzeichen == '+') && (r.Vorzeichen == '-')) { if (x1 < x2) result = fallZeichen(result, l, r, 2); else result = fallZeichen(result, l, r, 3);
} else if ((l.Vorzeichen == '-') && (r.Vorzeichen == '+')) { if (x1 < x2) result = fallZeichen(result, l, r, 2); else result = fallZeichen(result, l, r, 3); }
reduce(result); return result; }
//-----------------------------------------------------------------------
RationaleZahl & operator -(const RationaleZahl &l, const RationaleZahl &r) {
event('-', l, r);
static RationaleZahl result; double x3 = static_cast<double> (l.Zaehler) / l.Nenner, x4 = static_cast<double> (r.Zaehler) / r.Nenner;
if ((l.Vorzeichen == '-') && (r.Vorzeichen == '-')) { if (x3 < x4) result = fallZeichen(result, l, r, 4); else result = fallZeichen(result, l, r, 3); } else if ((l.Vorzeichen == '+') && (r.Vorzeichen == '-')) result = fallZeichen(result, l, r, 1); else if ((l.Vorzeichen == '+') && (r.Vorzeichen == '+')) if (x3 < x4) result = fallZeichen(result, l, r, 5); else result = fallZeichen(result, l, r, 3); else if ((l.Vorzeichen == '-') && (r.Vorzeichen == '+')) result = fallZeichen(result, l, r, 1);
reduce(result); return result; }
//-----------------------------------------------------------------------
RationaleZahl & operator *(const RationaleZahl &l, const RationaleZahl &r) {
event('*', l, r);
static RationaleZahl result;
if ((l.Zaehler == 0 || l.Nenner == 0)&&(r.Zaehler == 0 || r.Nenner == 0)) throw KeineZahl();
if ((l.Vorzeichen == '-' && r.Vorzeichen == '-') || (l.Vorzeichen == '+' && r.Vorzeichen == '+')) result.Vorzeichen = '+'; else result.Vorzeichen = '-';
result.Nenner = l.Nenner * r.Nenner; result.Zaehler = l.Zaehler * r.Zaehler;
reduce(result); return result; }
//-----------------------------------------------------------------------
RationaleZahl & operator /(const RationaleZahl &l, const RationaleZahl &r) {
event('*', l, r);
static RationaleZahl result;
if (l.Zaehler == 0 && r.Zaehler == 0) throw NullDurchNull(); if (l.Zaehler != 0 && l.Nenner == 0 && r.Zaehler != 0 && r.Nenner == 0) throw UnendlichDurchUnendlich();
if ((l.Vorzeichen == '-' && r.Vorzeichen == '-') || (l.Vorzeichen == '+' && r.Vorzeichen == '+')) result.Vorzeichen = '+'; else result.Vorzeichen = '-';
result.Nenner = l.Nenner * r.Zaehler; result.Zaehler = l.Zaehler * r.Nenner;
reduce(result); return result; }
//-----------------------------------------------------------------------
ostream & operator <<(ostream & o, const RationaleZahl &r) {
if (r.Zaehler == 0) o << 0 << ')'; else if (r.Nenner == 0) o << r.Vorzeichen << "Unendlich"; else o << '(' << r.Vorzeichen << r.Zaehler << '/' << r.Nenner << ')';
return o; }
|
Код | #include "RationaleZahl.h"
RationaleZahl& fallZeichen(RationaleZahl &result, const RationaleZahl &l, const RationaleZahl &r, int boo) {
unsigned int n1, n2;
n1 = l.Nenner / ggT(l.Nenner, r.Nenner); n2 = r.Nenner / ggT(l.Nenner, r.Nenner); result.Nenner = n1 * n2 * ggT(l.Nenner, r.Nenner);
switch (boo) { case 1: result.Zaehler = l.Zaehler * n2 + r.Zaehler*n1; result.Vorzeichen = l.Vorzeichen; return result; case 2: result.Zaehler = r.Zaehler * n1 - l.Zaehler * n2; result.Vorzeichen = r.Vorzeichen; return result; case 3: result.Zaehler = l.Zaehler * n2 - r.Zaehler * n1; result.Vorzeichen = l.Vorzeichen; return result; case 4: result.Zaehler = r.Zaehler * n1 - l.Zaehler * n2; result.Vorzeichen = '+'; return result; case 5: result.Zaehler = r.Zaehler * n1 - l.Zaehler * n2; result.Vorzeichen = '-'; return result; } }
//-----------------------------------------------------------------------
void reduce(RationaleZahl &res) {
unsigned int ggT_result;
ggT_result = ggT(res.Zaehler, res.Nenner);
res.Zaehler = res.Zaehler / ggT_result; res.Nenner = res.Nenner / ggT_result; }
//-----------------------------------------------------------------------
unsigned int ggT(unsigned int z, unsigned int n) {
unsigned int r, d;
if (n != 0) { r = z % n; d = ggT(n, r); return d; } else return z; }
//-----------------------------------------------------------------------
void event(char symbol, const RationaleZahl &l, const RationaleZahl &r) {
if (l.Zaehler == 0 && l.Nenner == 0) throw NullDurchNull(); if (r.Zaehler == 0 && r.Nenner == 0) throw NullDurchNull();
if (l.Vorzeichen != '-' && l.Vorzeichen != '+') throw FalschesVorzeichen(); if (r.Vorzeichen != '-' && r.Vorzeichen != '+') throw FalschesVorzeichen();
if (symbol == '-' || symbol == '+') if (l.Nenner == 0 || r.Nenner == 0) throw MinusUnendlichUndPlusUnendlich();
if (symbol == 0) if ((l.Zaehler == 0 && r.Nenner == 0) || (l.Nenner == 0 && r.Zaehler == 0)) throw NullMalUnendlich();
if (symbol == '/') { if (l.Zaehler == 0 && r.Zaehler == 0) throw NullDurchNull(); if (l.Zaehler != 0 && l.Nenner == 0 && r.Zaehler != 0 && r.Nenner == 0) throw UnendlichDurchUnendlich(); }
if (symbol == '*') { if ((l.Zaehler == 0 || l.Nenner == 0)&&(r.Zaehler == 0 || r.Nenner == 0)) throw KeineZahl(); } }
|
|