반응형
스택을 이용해서 문자열 뒤집기
# 입력받은 문자열의 길이를 확인할 수 있으므로 배열 기반 스택을 사용했다.
#include "stack_array.h"
#include <iostream>
#include <tchar.h>
#if defined(UNICODE) || defined(_UNICODE)
#define tcout std::wcout
#define tcin std::wcin
#else
#define tcout std::cout
#define tcin std::cin
#endif
int main()
{
TCHAR str[256] = { 0, };
tcout << "뒤집을 문자열을 입력하세요" << std::endl;
tcin >> str;
int strLen = _tcslen(str);
StackArray<TCHAR> rStr(strLen);
for (int i = 0; i < strLen; i++) {
rStr.Push(str[i]);
}
for (int i = 0; i < strLen; i++) {
tcout << rStr.Peek();
rStr.Pop();
}
tcout << std::endl;
}스택을 이용해서 10진수를 2진수로 변환하기.
# 스택에 몇개의 데이터가 입력될지 예상할 수 없으므로 연결 리스트 기반 스택을 사용했다.
#include "StackList.h"
#include <iostream>
int main()
{
int decimal = 0;
std::cout << "2진수로 변환할 10진수를 입력하세요." << std::endl;
std::cin >> decimal;
StackList<int> binary;
do {
binary.Push(decimal%2);
decimal /= 2;
} while (decimal != 0);
while (binary.Count() != 0) {
std::cout << binary.Pop();
}
}반응형