Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. /* Includes */
  2.  
  3. // Objects
  4. #include <iostream>
  5. #include <string>
  6.  
  7. // Headers
  8.  
  9. #include <conio.h>
  10. #include <time.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <windows.h>
  14.  
  15. // Using
  16.  
  17. using namespace std;
  18.  
  19. /* Code */
  20.  
  21. class Calculator
  22. {
  23. private:
  24.     char sign;
  25.     double num1, num2, result;
  26.     int functionID, temp;
  27.     long seconds;
  28.  
  29.     void sleep()
  30.     {
  31.         clock_t start = clock();
  32.         while (clock() - start < seconds);
  33.     }
  34.  
  35.     void gotoXY(int x, int y)
  36.     {
  37.         COORD cur = {x, y};
  38.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
  39.     }
  40.  
  41.     void printHeader()
  42.     {
  43.         cout << "************************************" << endl;
  44.         cout << "* Taschenrechner - Grundfunktionen *" << endl;
  45.         cout << "************************************" << endl << endl;
  46.     }
  47.  
  48.     void clearScreen()
  49.     {
  50.         system("cls");
  51.     }
  52.  
  53.     void clearSTDIN()
  54.     {
  55.         fflush(stdin);
  56.     }
  57.  
  58.     void getNumbers()
  59.     {
  60.         cout << "Bitte Zahl 1 eingeben: ";
  61.         cin >> num1;
  62.         cout << "Bitte Zahl 2 eingeben: ";
  63.         cin >> num2;
  64.         cout << endl;
  65.         this->clearSTDIN();
  66.     }
  67.  
  68.     void getFunctionID()
  69.     {
  70.         cout << "Bitte Funktion auswaehlen:" << endl << endl;
  71.         cout << "1 - Addition" << endl;
  72.         cout << "2 - Subtraktion" << endl;
  73.         cout << "3 - Multiplikation" << endl;
  74.         cout << "4 - Division" << endl << endl;
  75.         cout << "Funktion: ";
  76.         cin >> functionID;
  77.     }
  78.  
  79.     bool getResult()
  80.     {
  81.         if ((functionID > 0) && (functionID < 5))
  82.         {
  83.             if (functionID == 1)
  84.             {
  85.                 result = num1 + num2;
  86.             }
  87.             else if (functionID == 2)
  88.             {
  89.                 result = num1 - num2;
  90.             }
  91.             else if (functionID == 3)
  92.             {
  93.                 result = num1 * num2;
  94.             }
  95.             else if (functionID == 4)
  96.             {
  97.                 result = num1 / num2;
  98.             }
  99.             return true;
  100.         }
  101.         return false;
  102.     }
  103.    
  104.     void printResult()
  105.     {
  106.         cout << endl << "Ergebnis: " << result << endl << endl;
  107.         cout << "Return zum Abbrechen, alle anderen Tasten zum Wiederholen.";
  108.         sign = (char)_getch();
  109.     }
  110.  
  111.     void cancel()
  112.     {
  113.         cout << endl << "Nur Funktionen (1 - 4) sind gueltig, Neustart in 5 Sekunden." << endl;
  114.         for (temp = 4; temp >= 0; temp--)
  115.         {
  116.             this->sleep();
  117.             this->gotoXY(49, 19);
  118.             cout << temp;
  119.             this->gotoXY(60, 19);
  120.         }
  121.         sign = '0';
  122.     }
  123.  
  124. public:
  125.     Calculator(int s)
  126.     {
  127.         seconds = s * 1000;
  128.     }
  129.  
  130.     ~Calculator()
  131.     {
  132.     }
  133.  
  134.     void mainLoop()
  135.     {
  136.         do
  137.         {
  138.             this->clearScreen();
  139.             this->clearSTDIN();
  140.             this->printHeader();
  141.             this->getNumbers();
  142.             this->getFunctionID();
  143.             if (this->getResult())
  144.             {
  145.                 this->printResult();
  146.             }
  147.             this->cancel();
  148.         }
  149.         while (sign != 13);
  150.     }
  151. };
  152.  
  153. int main()
  154. {
  155.     Calculator calculator(1);
  156.     calculator.mainLoop();
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement