//------------------------------------------------- // Jordi Binefa i Martínez // dubtes@excite.com // http://members.juara.com/asi //------------------------------------------------- //Write the program SWITCH2.CPP to implement a four //functions single calculator. The program should //ask for the numbers and the function (+,-,* or /) //and should show the operation and the solution. //You may include the errors handling for the //"by zero division". //Escriu el programa SWITCH2.CPP que implementa una //calculadora simple de quatre funcions. El programa //té que demanar l’operand i l’operador, i presentar //tant el teclejat com el resultat. Has d’incloure la //revissió d’errors a l’introduïr l’operand i l’intent //de dividir per zero. #include #define YES 1 #define NO 0 #define MAX_STRING 30 void askValues(double *b, double *c, char *bb, char *cc){ cout << "Tell me the value of " << bb << " : "; cin >> *b; cout << "Tell me the value of " << cc << " : "; cin >> *c; } char operation(double a, double b, double *r){ int possible; char resposta; do{ possible = YES; cout << "\n Tell me the operation to do : "; cin >> resposta; switch(resposta){ case '+' : *r = a + b; break; case '-' : *r = a - b; break; case '*' : *r = a * b; break; case '/' : if(b) *r = a / b; else{ cout << "\nYou cannot divide by zero.\n"; possible = NO; } break; default : cout << "\nMay be +, -, * or /\n"; possible = NO; } }while(!possible); return resposta; } void main(){ double x,y,res; char first_num[MAX_STRING] = "first number"; char second_num [MAX_STRING] = "second number" ; askValues(&x,&y,first_num,second_num); cout << "\n\nThe solution is " << x << " " << operation(x,y,&res) << " "; cout << y << " = " << res << "\n\n\n"; }