Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes */
- // Objects
- #include <iostream>
- #include <string>
- // Headers
- #include <conio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <math.h>
- #include <windows.h>
- // Using
- using namespace std;
- /* Code */
- class Calculator
- {
- private:
- char sign;
- double num1, num2, result;
- int functionID, temp;
- long seconds;
- void sleep()
- {
- clock_t start = clock();
- while (clock() - start < seconds);
- }
- void gotoXY(int x, int y)
- {
- COORD cur = {x, y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
- }
- void printHeader()
- {
- cout << "************************************" << endl;
- cout << "* Taschenrechner - Grundfunktionen *" << endl;
- cout << "************************************" << endl << endl;
- }
- void clearScreen()
- {
- system("cls");
- }
- void clearSTDIN()
- {
- fflush(stdin);
- }
- void getNumbers()
- {
- cout << "Bitte Zahl 1 eingeben: ";
- cin >> num1;
- cout << "Bitte Zahl 2 eingeben: ";
- cin >> num2;
- cout << endl;
- this->clearSTDIN();
- }
- void getFunctionID()
- {
- cout << "Bitte Funktion auswaehlen:" << endl << endl;
- cout << "1 - Addition" << endl;
- cout << "2 - Subtraktion" << endl;
- cout << "3 - Multiplikation" << endl;
- cout << "4 - Division" << endl << endl;
- cout << "Funktion: ";
- cin >> functionID;
- }
- bool getResult()
- {
- if ((functionID > 0) && (functionID < 5))
- {
- if (functionID == 1)
- {
- result = num1 + num2;
- }
- else if (functionID == 2)
- {
- result = num1 - num2;
- }
- else if (functionID == 3)
- {
- result = num1 * num2;
- }
- else if (functionID == 4)
- {
- result = num1 / num2;
- }
- return true;
- }
- return false;
- }
- void printResult()
- {
- cout << endl << "Ergebnis: " << result << endl << endl;
- cout << "Return zum Abbrechen, alle anderen Tasten zum Wiederholen.";
- sign = (char)_getch();
- }
- void cancel()
- {
- cout << endl << "Nur Funktionen (1 - 4) sind gueltig, Neustart in 5 Sekunden." << endl;
- for (temp = 4; temp >= 0; temp--)
- {
- this->sleep();
- this->gotoXY(49, 19);
- cout << temp;
- this->gotoXY(60, 19);
- }
- sign = '0';
- }
- public:
- Calculator(int s)
- {
- seconds = s * 1000;
- }
- ~Calculator()
- {
- }
- void mainLoop()
- {
- do
- {
- this->clearScreen();
- this->clearSTDIN();
- this->printHeader();
- this->getNumbers();
- this->getFunctionID();
- if (this->getResult())
- {
- this->printResult();
- }
- this->cancel();
- }
- while (sign != 13);
- }
- };
- int main()
- {
- Calculator calculator(1);
- calculator.mainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement