kleine Optimierung
This commit is contained in:
parent
b3823c5204
commit
9f787b339e
3 changed files with 38 additions and 36 deletions
|
@ -5,7 +5,7 @@
|
|||
|
||||
int main()
|
||||
{
|
||||
long long depth = 1;
|
||||
short depth = 1;
|
||||
std::string input;
|
||||
|
||||
std::cout << "Gib die Tiefe ein: ";
|
||||
|
@ -13,43 +13,43 @@ int main()
|
|||
{
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> depth;
|
||||
if(depth<2)std::cout << "ERROR: Die Zahl muss gr\x94\xE1 \ber als 1 sein. Gib die Tiefe erneut ein: ";
|
||||
}while(depth<2);
|
||||
if(depth<2 || depth==SHRT_MAX)std::cout << "ERROR: Die Zahl ist zu gro\xE1 oder klein. Gib die Tiefe erneut ein: ";
|
||||
}while(depth<2 || depth==SHRT_MAX);
|
||||
|
||||
|
||||
for(long long idx = 0; idx<depth*depth; std::cout << "*") if(idx++%depth==0) std::cout << "\n";
|
||||
for(short idx = 0; idx<depth*depth; std::cout << "*") if(idx++%depth==0) std::cout << "\n";
|
||||
|
||||
|
||||
std::cout << "\n";
|
||||
|
||||
|
||||
for(long long idx = 0; idx<depth; idx++)
|
||||
for(short idx = 0; idx<depth; idx++)
|
||||
{
|
||||
std::cout << "\n";
|
||||
for(long long idx2 = 0; idx2<idx; idx2++) std::cout << " ";
|
||||
for(long long idx2 = 0; idx2<depth*2-1-2*idx; idx2++) std::cout << "*";
|
||||
for(short idx2 = 0; idx2<idx; idx2++) std::cout << " ";
|
||||
for(short idx2 = 0; idx2<depth*2-1-2*idx; idx2++) std::cout << "*";
|
||||
}
|
||||
|
||||
|
||||
std::cout << "\n\n";
|
||||
|
||||
|
||||
for(long long idx = 0; idx<depth-1; std::cout << "\n")
|
||||
for(short idx = 0; idx<depth-1; std::cout << "\n")
|
||||
{
|
||||
for(long long idx2 = 0; idx2<depth-idx-1; idx2++) std::cout << " ";
|
||||
for(short idx2 = 0; idx2<depth-idx-1; idx2++) std::cout << " ";
|
||||
std::cout << "*";
|
||||
for(long long idx2 = 0; idx2<2*idx-1; idx2++) std::cout << " ";
|
||||
for(short idx2 = 0; idx2<2*idx-1; idx2++) std::cout << " ";
|
||||
if(idx++>0) std::cout << "*";
|
||||
}
|
||||
for(long long idx = 0; idx<2*depth-1; idx++) std::cout << "*";
|
||||
for(long long idx = 0; idx<depth-2; idx++)
|
||||
for(short idx = 0; idx<2*depth-1; idx++) std::cout << "*";
|
||||
for(short idx = 0; idx<depth-2; idx++)
|
||||
{
|
||||
std::cout << "\n*";
|
||||
for(long long idx2 = 0; idx2<2*depth-3; idx2++) std::cout << " ";
|
||||
for(short idx2 = 0; idx2<2*depth-3; idx2++) std::cout << " ";
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << "\n";
|
||||
for(long long idx = 0; idx<2*depth-1; idx++) std::cout << "*";
|
||||
for(short idx = 0; idx<2*depth-1; idx++) std::cout << "*";
|
||||
|
||||
std::cout << "\n\n\nProgrammende!";
|
||||
getch();
|
||||
|
|
|
@ -5,39 +5,38 @@
|
|||
#include<string>
|
||||
#include<sstream>
|
||||
|
||||
//Zusatzaufgabe: Berechne die minimal notwendige Anzahl an Versuchen
|
||||
|
||||
int main()
|
||||
{
|
||||
bool again = true;
|
||||
std::string input;
|
||||
|
||||
while(again)
|
||||
do
|
||||
{
|
||||
srand(time(NULL));
|
||||
again = false;
|
||||
short randNumber = rand()%100+1, tries = 0, number = 0;
|
||||
std::string input;
|
||||
|
||||
short randNumber = rand()%100+1, tries = 0, number;
|
||||
|
||||
std::cout << "Errate die richtige Zahl zwischen 1 und 100: ";
|
||||
|
||||
while(number!=randNumber)
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
tries++;
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> number;
|
||||
if(number<1 || number>100) std::cout << "Die eingegebene Zahl liegt nicht im Wertebereich. Gib eine Zahl zwischen 1 und 100 ein: ";
|
||||
}while(number<1 || number>100);
|
||||
tries++;
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> number;
|
||||
|
||||
if(number<randNumber) std::cout << "\nDie eingegebene Zahl ist zu klein! Versuche es erneut: ";
|
||||
if(number<1 || number>100) std::cerr << "Error: Die eingegebene Zahl liegt nicht im Wertebereich. Gib eine Zahl zwischen 1 und 100 ein: ";
|
||||
else if(number<randNumber) std::cout << "\nDie eingegebene Zahl ist zu klein! Versuche es erneut: ";
|
||||
else if(number>randNumber) std::cout << "\nDie eingegebene Zahl ist zu gro\xE1! Versuche es erneut: ";
|
||||
}
|
||||
}while(number!=randNumber || number<1 || number>100);
|
||||
|
||||
//Zusatzaufgabe: Berechne die minimal notwendige Anzahl an Versuchen
|
||||
std::cout << "\nTreffer! Du hast " << tries << " Versuche gebraucht.\nM\x94 \bchtest du ein 2. mal spielen? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input == "y" || input == "Y") again = true;
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
do
|
||||
{
|
||||
getline(std::cin, input);
|
||||
if(input != "y" && input != "Y" && input != "n" && input !="N") std::cerr << "Error: Die Eingabe ist ung\x81ltig. M\x94 \bchtest du ein 2. mal spielen? y/n ";
|
||||
else std::cout << "\n\n";
|
||||
}while(input != "y" && input != "Y" && input != "n" && input !="N");
|
||||
}while(input == "y" || input == "Y");
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,10 @@ int main ()
|
|||
|
||||
if(start<end) for(int idx = start; idx<end; idx++) std::cout << idx << ", ";
|
||||
else for(int idx = start; idx>end; idx--) std::cout << idx << ", ";
|
||||
std::cout << end << "\n\nDie Summe der natuerlichen Zahlen von " << start << " bis " << end << " betraegt: " << (end*(end+1)-start*(start-1))/2;
|
||||
|
||||
std::cout << end << "\n\nDie Summe der natuerlichen Zahlen von " << start << " bis " << end << " betraegt: ";
|
||||
if(start<end) std::cout << (end*(end+1)-start*(start-1))/2;
|
||||
else std::cout << (end*(end-1)-start*(start+1))/2;
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
|
|
Reference in a new issue