์ฝ๋ ์ค๋ช
์ฌ์ฉ์๋ก๋ถํฐ ๋ฌธ์์ด์ ์ ๋ ฅ ๋ฐ๊ณ , ์ด๋ฅผ ํผ๋ผ๋ฏธ๋ ํํ๋ก ์ถ๋ ฅํ๋ ๊ฒ์ ๋๋ค.
์ผ๋ฐ์ ์ธ ์์ ์ธ asterisk(*) ํผ๋ผ๋ฏธ๋์๋ ๋ค๋ฅด๊ฒ ๋ฐ๋ณต๋ฌธ๊ณผ string์ ๋ํ ์ดํด๊ฐ ํ์ํด์ ์กฐ๊ธ ์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.
(๋์) ์ฝ๋
#include <iostream>
#include <string>
using namespace std;
int main() {
string choice{};
cout << "Enter the string you want to make it to a pyramid: ";
getline(cin,choice); // melon
string reversed_choice{choice}; // melon
reverse(reversed_choice.begin(), reversed_choice.end()); // nolem
cout << "You entered: " << choice << endl << endl; // You entered: melon
// for each row: 1, 2, 3, 4, 5
for (size_t row{1}; row <= choice.size(); row++) {
// making a blank for each row
// for melon, choice.size() = 5, so blank goes
// (5 - 1 = 4), (5 - 2 = 3), (5 - 3 = 2), (5 - 4 = 1), and (5 - 5 = 0)
for (size_t blank{choice.size()}; blank > row; blank--) {
cout << " ";
}
// num is for the first half (including the middle alphabet) string
// through each rows, it outputs a string with 1, 2, 3, 4, and 5
// characeters.
for (size_t num{}; num < row; num++) {
cout << choice.at(num);
}
// num2 is for the second half (excluding the middle alphabet) string
// that means that it must produce -1 length than first half of the final
// string through each rows, it outputs a reversed string in the index of 4,
// (3-4), (2-4), (1-4), and (0-5).
for (size_t num2{choice.size() - row + 1}; num2 < choice.size(); num2++) {
cout << reversed_choice.at(num2);
}
cout << endl;
}
}
์ํ๋ ๋๋ก ๋์จ ๊ฒ ๊ฐ๊ธด ํ์ง๋ง ๋ญ๊ฐ ๋นํจ์จ์ ์ด๊ณ ๋ถ์์ ํ ๋๋์ด๋ค.
์ฒ์์ ๋๋ฌด ํท๊ฐ๋ ค์ ํผ๋ผ๋ฏธ๋๋ฅผ ๋ง๋๋ ๊ณผ์ ์ ํฐ for loop ๋ด๋ถ์ 3๊ฐ์ง ์ธ๋ถ ๊ณผ์ ์ผ๋ก ๋๋์ด์ ์๊ฐํ๋ค.
1. ํ ์ค์ ์์ฑํ ๋ ํ๋ฒ์ฉ ๋ฐ๋ณตํ๋ external for loop์ ๋ง๋ค์.
2. ํด๋น for loop ๋ด๋ถ์์ ๊ฐ ์ค ๋น ๋น ๊ณต๊ฐ์ ๋ง๋๋ for loop์ ๊ตฌํํ์.
3. ๋น๊ณต๊ฐ ์ดํ์ ๋ฌธ์์ด์ ์ ๋ฐ (ํ์์ผ ์ ๊ฐ์ด๋ฐ์ ๋ฌธ์๋ ํฌํจ) ์ ์ถ๋ ฅํ๋ for loop์ ๊ตฌํํ์.
4. ๋ง์ง๋ง์ผ๋ก ๋ค์งํ ๋ฌธ์์ด์ ๋ค์ชฝ substring์ ์ถ๋ ฅํ์.
udemy ์์ assignment์๋๋ฐ ๋ค ํ๊ณ ๋์ ๋ชจ๋ฒ ๋ต์์ ๋ณด๋ ๋ด ์ฝ๋๋ณด๋ค ํจ์ฌ ์ง๊ด์ ์ด๊ณ ๊น๋ํ๋ค.
๋ชจ๋ฒ๋ต์
// Letter Pyramid
// Written by Frank J. Mitropoulos
#include <iostream>
#include <string>
int main() {
std::string letters{};
std::cout
<< "Enter a string of letters so I can create a Letter Pyramid from it: ";
getline(std::cin, letters);
size_t num_letters = letters.length();
int position{0};
// for each letter in the string
for (char c : letters) {
size_t num_spaces = num_letters - position;
while (num_spaces > 0) {
std::cout << " ";
--num_spaces;
}
// Display in order up to the current character
for (size_t j = 0; j < position; j++) {
std::cout << letters.at(j);
}
// Display the current 'center' character
std::cout << c;
// Display the remaining characters in reverse order
for (int j = position - 1; j >= 0; --j) {
// You can use this line to get rid of the size_t vs int warning if you
// want
auto k = static_cast<size_t>(j);
std::cout << letters.at(k);
}
std::cout << std::endl; // Don't forget the end line
++position;
}
return 0;
}
'C++ > ํ๋ก์ ํธ (Project)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] (Program) ํจ์ ์์ฉ ํ๋ก๊ทธ๋จ (0) | 2023.12.18 |
---|---|
[C++] (Program) ์ฌ๊ท ํจ์ ์์ : ์ ์์ ์๋ฆฟ์ ๋ง์ (0) | 2023.12.18 |
[C++] (Program) String ์์ฉ ์ํธํ ํ๋ก๊ทธ๋จ (string::npos) (1) | 2023.12.15 |
[C++] (Program) ๋ฐ๋ณต๋ฌธ ์์ฉ ํ๋ก๊ทธ๋จ (1) | 2023.12.13 |