Public paste
Undefined
By: modcalc.cpp | Date: Nov 28 2009 14:53 | Format: C++ | Expires: never | Size: 4.46 KB | Hits: 813

  1. //////////////////////////////////////////////////////////////////////////
  2. //                                                                                                                                              //
  3. // Voelz, Michael                                    Matr.-Nr.: 345451  //
  4. //                                                                                                                                              //
  5. //                                                                                                                                              //
  6. // modCalc                                                                                                              //
  7. // =========                                                                                                                    //
  8. //                                                                                                                                              //
  9. //////////////////////////////////////////////////////////////////////////
  10. //                                                                                                                                              //
  11. // Datum: 19.11.2006                                                                                                    //
  12. //                                                                                                                                              //
  13. //                                                                                                                                              //
  14. //                                                                                                                                              //
  15. //                                                                                                                                              //
  16. // Beschreibung des Programms:                                                                                  //
  17. // ===========================                                                                                  //
  18. //                                                                                                                                          //
  19. // Funktion:                                                                                                                    //
  20. // Fordert zur Eingabe eines Operators und eines Operanden auf und              //
  21. // gibt dann das ergebnis aus. Division durch 0 ist nicht erlaubt.              //
  22. // Beenden mit 'e'. L�schen mit 'c'.                                                                     //
  23. //                                                                                                                                          //
  24. // Aufbau:                                                              //
  25. // enth�lt eine do-while Schleife in der die Berechnung und die         //
  26. // Abfrage stattfindet. Am Anfang ein if um auf Eingabe von 'e'                 //
  27. // oder 'c' zu �berpr�fen.                                                                                                //
  28. //                                                                                                                                          //
  29. // Benutzung:                                                                                                           //
  30. // einfach nur starten und den Anweisungen folgen                       //
  31. //                                                                                                                                              //
  32. //                                                                                                                                              //
  33. //                                                                                                                                              //
  34. //////////////////////////////////////////////////////////////////////////
  35.  
  36. #include <iostream>
  37. #include "mathfunctions.h" // headerfile
  38. using namespace std;
  39.  
  40. void main()
  41. {
  42.         char Operator;
  43.         double operand, ergebnis = 0;
  44.        
  45.         druckEingabeHilfe(); // eingabehilfe ausgeben
  46.         cout << ergebnis;
  47.        
  48.         do
  49.         {
  50.                 cin >> Operator; // abfrage f&#65533;r +, -, *, /, ...
  51.                 switch(Operator)
  52.                         {
  53.                                 case '*':       cin >> operand; // abfrage des operanden
  54.                                                         ergebnis = ergebnis * operand;
  55.                                                         cout << ergebnis;
  56.                                                         break;
  57.                                 case '/':       cin >> operand; // abfrage des operanden
  58.                                                         if(operand == 0) // wenn x/0
  59.                                                         {
  60.                                                                 cout << "Division durch '0' ist nicht zulaessig!" << endl << ergebnis;
  61.                                                         }
  62.                                                         else
  63.                                                         {
  64.                                                                 ergebnis = ergebnis / operand;
  65.                                                                 cout << ergebnis;
  66.                                                         }
  67.                                                         break;
  68.                                 case '-':       cin >> operand; // abfrage des operanden
  69.                                                         ergebnis = ergebnis - operand;
  70.                                                         cout << ergebnis;
  71.                                                         break;
  72.                                 case '+':       cin >> operand; // abfrage des operanden
  73.                                                         ergebnis = ergebnis + operand;
  74.                                                         cout << ergebnis;
  75.                                                         break;
  76.                                 case '!':       if (ganzZahl(ergebnis)) // ganze zahl?
  77.                                                         {
  78.                                                                 if (ergebnis < 0) // es gibt nur Fakult&#65533;ten > 0
  79.                                                                 {
  80.                                                                         cout << "Fakultaet ist nur fuer natuerliche Zahlen > 0 definiert!" << endl << ergebnis;
  81.                                                                 }
  82.                                                                 else
  83.                                                                 {
  84.                                                                 ergebnis = fakultaet(ergebnis);
  85.                                                                 cout << ergebnis;
  86.                                                                 }
  87.                                                         }
  88.                                                         else // wenn keine ganze zahl
  89.                                                         {
  90.                                                                 cout << "Keine ganze Zahl!" << endl << ergebnis;
  91.                                                         }
  92.                                                         break;
  93.                                 case '^':       cin >> operand; // abfrage des operanden
  94.                                                         if (ganzZahl(operand)) // ganze zahl?
  95.                                                         {
  96.                                                                 if (ergebnis == 0 && operand < 0) // wenn basis = 0 und exponent < 0
  97.                                                                 {
  98.                                                                         cout << "Basis = 0 und Exponent < 0 geht leider nicht!" << endl << ergebnis;
  99.                                                                 }
  100.                                                                 else
  101.                                                                 {
  102.                                                                         if (operand == 0) // wenn basis = 0
  103.                                                                         {
  104.                                                                                 ergebnis = 0.;
  105.                                                                                 cout << ergebnis;
  106.                                                                         }
  107.                                                                         else
  108.                                                                         {
  109.                                                                         ergebnis = potenz(ergebnis, int(operand)); // potenzrechnung
  110.                                                                         cout << ergebnis;
  111.                                                                         }
  112.                                                                 }
  113.                                                         }
  114.                                                         else
  115.                                                         {
  116.                                                                 cout << "Keine ganze Zahl!" << endl << ergebnis; // keine ganze zahl
  117.                                                         }
  118.                                                         break;
  119.                                 case 'c':       ergebnis = 0.; // ergebnis zur&#65533;cksetzen
  120.                                                         cout << ergebnis;
  121.                                                         break;
  122.                                 case 'e':       break; // beenden
  123.                                 default:        cout << " ->" << Operator << ": falsche Operation" << endl << ergebnis; // bei falscher eingabe
  124.                                                         break;
  125.                         }
  126.         }while(Operator != 'e');
  127. }
  128.  
  129. void druckEingabeHilfe() //Unterprogramm
  130. {
  131.         for (int i=1; i<=(breite/2); i++) // breite/2 mal *
  132.         {
  133.                 cout << "*";
  134.         }
  135.         cout << title; // in die mitte der titel
  136.         for (int i=1; i<=(breite/2); i++) // breite/2 mal *
  137.         {
  138.                 cout << "*";
  139.         }
  140.         cout << endl;
  141.         cout << "Bitte geben sie die Anweisungen in der Form von '+1' an." << endl;
  142.         cout << "Beenden mit 'e'. Ergebnis loeschen mit 'c'." << endl;
  143.         for (int i=1; i<=(breite + int(strlen(title))); i++)
  144.         {
  145.                 cout << "*";
  146.         }
  147.         cout << endl;
  148. }