1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
#include <cmath>
using namespace std;
// Function declaration
void skrivInfo();
void lasEttTal (int &tal1, int &tal2);
char lasOp();
void ordna (int &tal1, int &tal2);
int berakna (int tal1, int tal2, char op);
void skrivResultat (int summa);
// Main program
int main()
{
int tal1, tal2, summa;
char op;
skrivInfo();
lasEttTal (tal1, tal2);
op = lasOp();
ordna (tal1, tal2);
summa = berakna (tal1, tal2, op);
skrivResultat(summa);
return 0;
}
// Func. def.
// Hello
void skrivInfo()
{
cout<<"Hej det här programmet räknar addition, multiplikation eller kvadratsumman av två tal."<<endl;
}
//Func. lasEttTal that lets the user to write two numbers
void lasEttTal (int &tal1, int &tal2)
{
cout<<"Skriv in två heltal: ";
cin>>tal1>>tal2;
}
//Funk. lasOp that lets the user to choose which operation
char lasOp()
{
char op;
char t1='a';
char t2='m';
char t3='k';
cout<<"Välj vilken beräkning som skall göras: "<<t1<<" addera, "<<t2<<" multiplicera, eller "<<t3<<" kvadratsumman av två tal: ";
cin>>op;
while (!((op==t1)||(op==t2)||(op==t3))) //if none of these (wrog choice)
{
cout<<"Fel inmatning"<<endl;
cout<<"Välj vilken beräkning som skall göras "<<t1<<" addera, "<<t2<<" multiplicera, eller "<<t3<<" kvadratsumman av tal";
cin>>op;
}
return op;
};
//Func. to make them in order of magnitude
void ordna (int &tal1, int &tal2)
{
if(tal1>tal2)
{
int temp = tal1;
tal1=tal2;
tal2=temp;
}
}
// Func. to calculate according to choises in LasEttTal and in lasOp
int berakna (int tal1, int tal2, char op)
{
int summa=0; //nollställa
cout<<tal1<<endl;
cout<<tal2<<endl;
cout<<op<<endl;
if(op=='a')
{
for(int tal1; tal1<=tal2; tal1++)
{
summa +=tal1; //addition
cin>>summa;
cout<<summa<<endl;
}
}
if(op=='m')
{
for(int tal1; tal1<=tal2; tal1++)
summa*=tal1; //multiplication
cin>>summa;
cout<<summa<<endl;
}
if(op=='k')
{
for(int tal1; tal1<=tal2; tal1++);
summa=tal1*tal1 + tal2*tal2; // Square-sum
cin>>summa;
cout<<summa<<endl;
}
return summa;
}
// Func. write the result
void skrivResultat(int summa)
{
cout<<"Resultatet av beräkningen är: "<<summa<<endl;
}
|