Programmiersprache C++
Posted by peterle on 1-3-2010 12:04 am
Seit ein paar Wochen lerne ich die Programmiersprache C++ und habe meinen Spass dabei. Ich freue mich schon auf die objektorientierte Programmierung, mal schauen wie ich mit Klassen zurecht komme. So lange beschäftige ich mich noch mit den rekursiven Funktionen. Zum Pflichtprogramm gehört natürlich auch ein "Hello World!".
// hello world program in C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
return 0;
}
Im Moment schreibe ich ein kleines Rechenprogramm für meinen Neffen, etwas Mathe kann er schon und er tippt gerne auf der Computertastatur herum.
// Rechenspiel
#include
#include
using namespace std;
int Calculation(int One, int Two, int Op)
{
int Sum = 0;
switch (Op) {
case 1:
Sum = One + Two;
break;
case 2:
Sum = One - Two;
break;
case 3:
Sum = One * Two;
break;
case 4:
Sum = One / Two;
break;
}
return Sum;
}
void Sign(int Op)
{
switch (Op) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
}
}
int main()
{
string Playername;
cout << "Willkommen zum Kopfrechenspiel. Bitte geben Sie den Spielernamen ein: ";
getline (cin, Playername);
srand (0);
int Counter = 0;
bool Further;
int Fieldone, Fieldtwo, Arithmetic_operator;
int Sum = 0, Guess = 0; Counter = 0;
do {
Further = false;
Fieldone = rand() % 10 + 1;
Fieldtwo = rand() % 10 + 1;
Arithmetic_operator = rand() % 4 + 1;
Sum = Calculation(Fieldone, Fieldtwo, Arithmetic_operator);
cout << "Wie lautet das Ergebnis aus: " << Fieldone;
Sign(Arithmetic_operator);
cout << Fieldtwo << " ";
cin >> Guess;
cout << "E[HE[2J";
if (Guess==Sum) {
Further = true;
Counter++;
cout << "Richtig. Gut gemacht " << Playername << "! ";
}
else {
cout << "Falsch. ";
Further = false;
}
} while (Further);
cout << "Das war " << Counter << "xmal richtig. Und das letzte mal leider falsch. Du bist raus! ";
return 0;
}