Dear all - for a school project I have to make a calculator which does the basic + - * / % operations and, if the user does not enter "x" (x stopps the program and prints out the result) the calculator should function like any other, so, input for example 5+3, prints out the result, and then the user can do other operation on that, for example -1, then the result is printed, and so on, until the user hits X.
there are of course other requirements, like if there is division with rest, user is prompted to choose whether to round up the result or end the calculation... and something like "division with 0 is not allowed"
current "skill" status - we've done switch/case, loops and functions.
i've been playing around with do-while loop but i cannot make anything happen; so far i only have the basic switch-case thingy and i know, the whole thing looks terrifying. any help would be greatly appreciated!
What I have tried:
#include <stdio.h>
#include <stdlib.h>
int calculate(int num1, char op, int num2)
{
int gueltig = 0; int ungueltig = 0; int answer = 0;
int rest = 0; switch (op)
{
case '+':
answer = num1 + num2;
++gueltig;
printf("Ergebnis: %d.\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig); break;
case '-':
if (num1 > num2) {
answer = num1 - num2;
++gueltig;
printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
} else {
printf("Operation ist ungueltig!\nErgebnis darf nicht negativ sein!\nErgebnis: %d", num1);
}
break;
case '*':
answer = num1 * num2;
++gueltig;
printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
break;
case '/':
if (num2 == 0) {
++ungueltig;
printf("Operation ist ungueltig!\nKeine Divisionen durch 0!\nErgebnis: %d", num1);
} else if (num2 != 0) {
if (num1 % num2 == 0) {
answer = num1 / num2;
++gueltig;
printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
} else if (num1 % num2 !=0) {
answer = num1 / num2;
rest = num1 % num2;
printf("Operation ist ungueltig! Ergebnis ist %d mit %d Rest. Soll gerundet(r), abgeschnitten(c) oder verworfen(x) werden?", answer, rest);
}
}
}
}
int main()
{
int num1, num2;
char op;
do
{
scanf("%d %c %d", &num1, &op, &num2);
int result = calculate(num1, op, num2);
} while ( op != 'x');