BeerSong из книги Objective-C Programing: The Big Nerd Ranch Guide 2ed - C (СИ)
Формулировка задачи:
#include <stdio.h>
void singSongFor(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n\n");
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n",
numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n\n",
oneFewer);
singSongFor(oneFewer); // This function calls itself!
// Print a message just before the function ends
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
}
}
int main(int argc, const char * argv[])
{
singSongFor(4);
return 0;
}4 bottles of beer on the wall. 4 bottles of beer. Take one down, pass it around, 3 bottles of beer on the wall. 3 bottles of beer on the wall. 3 bottles of beer. Take one down, pass it around, 2 bottles of beer on the wall. 2 bottles of beer on the wall. 2 bottles of beer. Take one down, pass it around, 1 bottles of beer on the wall. 1 bottles of beer on the wall. 1 bottles of beer. Take one down, pass it around, 0 bottles of beer on the wall. There are simply no more bottles of beer on the wall. Put a bottle in the recycling, 1 empty bottles in the bin. Put a bottle in the recycling, 2 empty bottles in the bin. Put a bottle in the recycling, 3 empty bottles in the bin. Put a bottle in the recycling, 4 empty bottles in the bin. Program ended with exit code: 0
Я НЕ МОГУ ПОНЯТЬ ПОЧЕМУ СООБЩЕНИЕ ПРО Recycling ПЕЧАТАЕТСЯ В КОНЦЕ, ДА ЕЩЕ И ЧЕТЫРЕ РАЗА??? Исходя из моей логики когда numberOfBottles = 0, программа повторяя себя выводит: There are simply no more bottles of beer on the wall. и дальше просто возвращается не оброщая внимания на "else". Единственное объяснение которое я нашел для себя, это то, что когда программа возвращает каждый stack, то она каким-то оброзом запускает else statement с кодом который ранее не был запущен, при этом пропуская все остальное....??? O_O Но, мне все равно не понятно почему так? ПОЧЕМУ ОНА ВЫДАЕТ Put a bottle in the recycling, 1 empty bottles in the bin. В САМОМ КОНЦЕ И АЖ ЧЕТЫРЕ РАЗА? Объясните на пальцах пожалуйста! Спасибо!!!Решение задачи: «BeerSong из книги Objective-C Programing: The Big Nerd Ranch Guide 2ed»
void singSongFor0(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n\n");
}
}
void singSongFor1(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n\n");
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n",
numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n\n",
oneFewer);
singSongFor0(0);
// Print a message just before the function ends
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
}
}
void singSongFor2(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n\n");
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n",
numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n\n",
oneFewer);
singSongFor1(1);
// Print a message just before the function ends
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
}
}
int main(int argc, const char * argv[])
{
singSongFor2(2);
return 0;
}