Advertisement
Guest User

Untitled

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