/*
* Filename : Taschenrechner.exe
* Author : Dennis Eckert
* Date : 25.11.2018
* Description : einfacher Taschenrechner mit vier Grundrechenarten und Modulo-Operator
*/
#include <iostream>
#include <string.h>
using namespace std;
void input(double& op1, double& op2, char& opp);
void addition(double& op1, double& op2, double& res);
int main()
{
bool addRemainder = false;
double leftOperator = 0.0;
double rightOperator = 0.0;
double result = 0.0;
int remainder = 0;
char symbolOperator = '\0';
do
{
input(leftOperator, rightOperator, symbolOperator);
if (symbolOperator == '+')
{
addition(leftOperator, rightOperator, result);
}
else if (symbolOperator == '-')
{
result = leftOperator - rightOperator;
}
else if (symbolOperator == '*')
{
result = leftOperator * rightOperator;
}
else if (symbolOperator == '/')
{
if (rightOperator != 0.0)
{
char input = '\0';
do //Anfang Ganzzahldivision
{
cout << "Ganzzahldivision durchfuehren?(j/n)";
cin >> input;
if (input == 'j')
{
if (static_cast<int>(leftOperator) - leftOperator == 0.0 && static_cast<int>(rightOperator) - rightOperator == 0.0)
{
result = leftOperator / rightOperator;
result = static_cast<int>(result);
remainder = static_cast<int>(leftOperator) % static_cast<int>(rightOperator);
addRemainder = true;
break;
}
else
{
cout << "Fuer die Ganzzahldivision muessen beide Operatoren ganzzahlig sein!" << endl;
continue;
}
}
else if (input == 'n')
{
result = leftOperator / rightOperator;
break;
}
else
{
cout << "Ungueltige Eingabe!" << endl;;
continue;
}
}while(true); //Ende Ganzzahldivision
}
else //Bezieht sich auf if(rightOperator != 0.0)
{
cout << "Es darf nicht durch null geteilt werden!" << endl;
continue;
}
}
else if (symbolOperator == '%')
{
if (static_cast<int>(leftOperator) - leftOperator == 0.0 && static_cast<int>(rightOperator) - rightOperator == 0.0 && static_cast<int>(rightOperator) > 0)
{
result = static_cast<int>(leftOperator) % static_cast<int>(rightOperator);
}
else
{
cout << "Fuer die Modulo-Operation muessen beide Operatoren ganzzahlig und der rechte groesser null sein!" << endl;
continue;
}
}
else
{
cout << "Es wurde kein gueltiges Operatorsymbol eingegeben!" << endl;
continue;
}
cout << leftOperator << " " << symbolOperator << " " << rightOperator << " = " << result;
if (addRemainder)
{
cout << " Rest : " << remainder << endl;
addRemainder = false;
}
else
{
cout << endl;
}
}while(true);
return 0; //Wird bei Aufgabenstellung, dass so viele Rechnungen wie man will ausgeführt werden können, sowieso nicht ausgeführt.
}
void input(double& op1, double& op2, char& opp){
cout << "Einfacher Taschenrechner" << endl;
cout << "Verfuegbare Operationen : +, -, /, *, %" << endl << endl;
cin >> op1 >> opp >> op2;
}
void addition(double& leftOperator, double& rightOperator, double& result){
result = leftOperator + rightOperator;
}