Skip to content
Open
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions solutions/cpp/beer-song/1/beer_song.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "beer_song.h"
#include <sstream>

using namespace std;

namespace beer_song {

string verse(int number) {
ostringstream ss;

switch (number) {
case 0:
ss << "No more bottles of beer on the wall, no more bottles of beer.\n"
<< "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
break;

case 1:
ss << "1 bottle of beer on the wall, 1 bottle of beer.\n"
<< "Take it down and pass it around, no more bottles of beer on the wall.\n";
break;

case 2:
ss << "2 bottles of beer on the wall, 2 bottles of beer.\n"
<< "Take one down and pass it around, 1 bottle of beer on the wall.\n";
break;

default:
ss << number << " bottles of beer on the wall, " << number << " bottles of beer.\n"
<< "Take one down and pass it around, " << (number - 1) << " bottles of beer on the wall.\n";
break;
}

return ss.str();
}

string sing(int start, int end) {
ostringstream ss;

// La canción siempre va en cuenta regresiva
for (int i = start; i >= end; --i) {
ss << verse(i);
// Salto de linea entre versos
if (i > end) {
ss << "\n";
}
}

return ss.str();
}

}
12 changes: 12 additions & 0 deletions solutions/cpp/beer-song/1/beer_song.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <string>

namespace beer_song {

// Genera un único verso específico (de 0 a 99)
std::string verse(int number);

// Genera una serie de versos desde un punto de inicio hasta un punto de fin
std::string sing(int start, int end = 0);

}