From aaa33fdb9a883de63a701fd8eccf8836db4cb5aa Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 21 Jun 2026 20:48:53 +0000 Subject: [PATCH] [Sync Iteration] cpp/food-chain/1 --- solutions/cpp/food-chain/1/food_chain.cpp | 58 +++++++++++++++++++++++ solutions/cpp/food-chain/1/food_chain.h | 21 ++++++++ 2 files changed, 79 insertions(+) create mode 100644 solutions/cpp/food-chain/1/food_chain.cpp create mode 100644 solutions/cpp/food-chain/1/food_chain.h diff --git a/solutions/cpp/food-chain/1/food_chain.cpp b/solutions/cpp/food-chain/1/food_chain.cpp new file mode 100644 index 0000000..0f289a4 --- /dev/null +++ b/solutions/cpp/food-chain/1/food_chain.cpp @@ -0,0 +1,58 @@ +#include "food_chain.h" +#include + +using namespace std; + +namespace food_chain { + +const vector data = { + {"fly", ""}, + {"spider", "It wriggled and jiggled and tickled inside her.\n"}, + {"bird", "How absurd to swallow a bird!\n"}, + {"cat", "Imagine that, to swallow a cat!\n"}, + {"dog", "What a hog, to swallow a dog!\n"}, + {"goat", "Just opened her throat and swallowed a goat!\n"}, + {"cow", "I don't know how she swallowed a cow!\n"}, + {"horse", "She's dead, of course!\n"} +}; + +string verse(int number) { + int idx = number - 1; + ostringstream ss; + + ss << "I know an old lady who swallowed a " << data[idx].name << ".\n"; + ss << data[idx].reaction; + + if (number == 8) { + return ss.str(); + } + + for (int i = idx; i > 0; --i) { + ss << "She swallowed the " << data[i].name << " to catch the " << data[i - 1].name; + if (i - 1 == 1) { + ss << " that wriggled and jiggled and tickled inside her"; + } + ss << ".\n"; + } + + ss << "I don't know why she swallowed the fly. Perhaps she'll die.\n"; + + return ss.str(); +} + +string verses(int start, int end) { + ostringstream ss; + + for (int i = start; i <= end; ++i) { + ss << verse(i); + ss << "\n"; + } + + return ss.str(); +} + +std::string sing() { + return verses(1, 8); +} + +} diff --git a/solutions/cpp/food-chain/1/food_chain.h b/solutions/cpp/food-chain/1/food_chain.h new file mode 100644 index 0000000..e1f7263 --- /dev/null +++ b/solutions/cpp/food-chain/1/food_chain.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace food_chain { + + struct Animal { + std::string name; + std::string reaction; + }; + + // Devuelve una sola estrofa (1 a 8) + std::string verse(int number); + + // Devuelve un rango de estrofa + std::string verses(int start, int end); + + // Devuelve la canción completa + std::string sing(); + +}