La calcolatrice – Funzioni in C++

CALCOLATRICE

TEORIA:

Cap01_Procedure_Funzioni
PROBLEMA:
Scrivere un programma che simuli una calcolatrice, avente le operazioni di somma, sottrazione, moltiplicazione, divisione, inverso di un numero, potenza e radice quadrata.
ANALISI:
Il programma si servirà di una funzione per ogni operazione richiesta. All’inizio leggerà il primo numero, dopodiché inizierà un ciclo while che avrà termine quando l’utente digiterà =. All’interno di questo ciclo il programma richiederà l’operazione da compiere e, con uno switch, richiamerà la funzione corrispondente mettendo il risultato nella variabile ris.
FUNZIONI UTILIZZATE:
• double somma(double, double): calcola la somma tra due numeri
• double sott(double, double): calcola la differenza tra due numeri
• double prodotto(double, double): calcola il prodotto tra due numeri
• double divisione(double, double): calcola il quoziente tra due numeri
• double inverso(double): calcola l’inverso di un numero
• double radice(double): calcola la radice quadrata di un numero
• double potenza (double n1, double n2): la potenza di un numero elevato ad un altro numero
FUNZIONI INTEGRABILI:
Prevedere l’aggiunta di altre funzioni per rendere la calcolatrice del tipo scientifica.
STEP BY STEP:
Si suggerisce, prima di risolvere questo problema, di realizzare una versione demo di calcolatrice in cui viene richiesto di leggere due numeri da tastiera e di eseguire l’operazione di moltiplicazione.

 

SWITCH CASE IN C AND C++
Switch case statements are a substitute for long if statements that compare a variable to several “integral” values (“integral” values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
switch ( ) {
case this-value:
Code to execute if == this-value
break;
case that-value:
Code to execute if == that-value
break;

default:
Code to execute if does not equal the value following any of the cases
break;
}

The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions. Sadly, it isn’t legal to use case like this:
int a = 10;
int b = 10;
int c = 20;

switch ( a ) {
case b:
// Code
break;
case c:
// Code
break;
default:
// Code
break;
}
The default case is optional, but it is wise to include it as it handles any unexpected cases. Switch statements serves as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user.

Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.
#include

using namespace std;

void playgame()
{
cout << “Play game called”;
}
void loadgame()
{
cout << “Load game called”;
}
void playmultiplayer()
{
cout << “Play multiplayer game called”;
}

int main()
{
int input;

cout<<“1. Play game\n”;
cout<<“2. Load game\n”;
cout<<“3. Play multiplayer\n”;
cout<<“4. Exit\n”;
cout<<“Selection: “; cin>> input;
switch ( input ) {
case 1: // Note the colon, not a semicolon
playgame();
break;
case 2: // Note the colon, not a semicolon
loadgame();
break;
case 3: // Note the colon, not a semicolon
playmultiplayer();
break;
case 4: // Note the colon, not a semicolon
cout<<“Thank you for playing!\n”;
break;
default: // Note the colon, not a semicolon
cout<<“Error, bad input, quitting\n”;
break;
}
cin.get();
}
This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code.

 

LOOP: DO … WHILE

DO..WHILE loops are useful for things that want to loop at least once. The structure is
do {
} while ( condition );
Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says “Loop while the condition is true, and execute this block of code”, a do..while loop says “Execute this block of code, and loop while the condition is true”.

Example:
#include

using namespace std;

int main()
{
int x;

x = 0;
do {
// “Hello, world!” is printed at least one time
// even though the condition is false
cout<<“Hello, world!\n”;
} while ( x != 0 );
cin.get();
}
Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.