Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculator #306

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions exercises/calculator.cpp
Original file line number Diff line number Diff line change
@@ -10,3 +10,44 @@
Multiplication: 8
Division: 2
*/

#include <iostream>
using namespace std;


int Sum(int a,int b){
return a+b;
}

int Difference(int a,int b){
return a-b;
}

int Multiplication(int a,int b){
return a*b;
}

int Division(int a,int b){
return a/b;
}


int main(){
cout<<"-------------Welcome to calculator---------------------"<<endl;
cout<<"Chose your operation \n Press 1 to Sum \n Press 2 to Difference \n Press 3 to
Multiplicate \n Press 4 to Division"<<endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In questa riga hai inserito un 'a capo' e quindi il compilatore si lamenta perchè non trova il "fine istruzione". Se vuoi scrivere codice multiriga, quindi rimanendo nella stessa istruzione (in questo caso cout devi inserire un \ a fine riga per indicare al compilatore "Il codice prosegue sotto, quindi non è un errore"

Esempio: (nota il \ alla fine della riga)

cout << "Chose your operation \n Press 1 to Sum \n Press 2 to Difference \n Press 3 to \
        Multiplicate \n Press 4 to Division "<<endl;

int x;
cin >> x;
cout<<"Insert first number"<<endl;
int a,b;
cin >> a;
cout<<"Insert second number"<<endl;
cin >> b;

if(x==1)return Sum(a,b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potresti usare uno switch case per migliorare la leggibilità del codice.

if(x==2)return Difference(a,b);
if(x==3)return Multiplication(a,b);
if(x==4)return Division(a,b);

return 0;
}
55 changes: 55 additions & 0 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
@@ -27,3 +27,58 @@
M 3 vs 3 => blue win
O 2 vs 1 => red win
*/

#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;


int attacker_Dices[3];
int defender_Dices[3];

void compare(){
sort(attacker_Dices, attacker_Dices+ 3, greater<int>());
sort(defender_Dices, defender_Dices + 3, greater<int>());
for(int i=0; i<3; i++){
if(defender_Dices[i]>=attacker_Dices[i])
{
cout<<"N"<<attacker_Dices[i]<<" vs "<<defender_Dices[i]<<
" ==> Blue Win"<<endl;
}
else
{
cout<<"N"<<attacker_Dices[i]<<" vs "<<defender_Dices[i]<<
" ==> Red Win"<<endl;
}
}

}

int main(){
srand(rand()%556745+1);


cout<<"Red Dices:\n";
for(int i=0; i<3; i++){
attacker_Dices[i]= rand() % 5+1;
cout<<attacker_Dices[i]<<endl;


}


cout<<"Blue Dices:\n";
for(int i=0; i<3; i++){
defender_Dices[i]= rand() % 5+1;
cout<<defender_Dices[i]<<endl;

}

compare();





}