From 90a995b848212e17dd6b133396317301faceaecf Mon Sep 17 00:00:00 2001 From: Severin Kaderli Date: Mon, 7 Mar 2022 17:39:52 +0100 Subject: [PATCH] Add assignment for ascii_table Signed-off-by: Severin Kaderli --- CMakeLists.txt | 3 +- ascii_table/CMakeLists.txt | 3 + ascii_table/main.cpp | 119 +++++++++++++++++++++++++++++++++++++ hello_world/CMakeLists.txt | 4 +- 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 ascii_table/CMakeLists.txt create mode 100644 ascii_table/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c81a07..fcf3b2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,4 @@ cmake_minimum_required(VERSION 3.22) project(CPP) -add_subdirectory(hello_world) \ No newline at end of file +add_subdirectory(hello_world) +add_subdirectory(ascii_table) \ No newline at end of file diff --git a/ascii_table/CMakeLists.txt b/ascii_table/CMakeLists.txt new file mode 100644 index 0000000..7f64a9d --- /dev/null +++ b/ascii_table/CMakeLists.txt @@ -0,0 +1,3 @@ +project(ASCIITable) +set(SOURCES main.cpp) +add_executable(${PROJECT_NAME} ${SOURCES}) \ No newline at end of file diff --git a/ascii_table/main.cpp b/ascii_table/main.cpp new file mode 100644 index 0000000..8b0b59c --- /dev/null +++ b/ascii_table/main.cpp @@ -0,0 +1,119 @@ +/** + * This program outputs an ASCII-Table using only std::cout, std::cin and input/output manipulators. + */ + +#include +#include +#include +#include + +const int CHAR_WIDTH = 5; +const int DEC_WIDTH = 4; +const int HEX_WIDTH = 4; +const int OCT_WIDTH = 4; + +int getIntInRangeFromInput(std::string message, int start, int end) { + int value = 0; + bool valueIsValid = false; + while (!valueIsValid) { + std::cout << message; + std::cin >> value; + + if (value < start || value > end) { + std::cout << "Please enter a valid value." << std::endl; + valueIsValid = false; + } else { + valueIsValid = true; + } + + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } + return value; +} + +void printTableDivider() { + std::cout << std::setfill('-') + << "|" + << std::setw(CHAR_WIDTH) + << "" + << std::setw(1) + << "|" + << std::setw(DEC_WIDTH) + << "" + << std::setw(1) + << "|" + << std::setw(HEX_WIDTH) + << "" + << std::setw(1) + << "|" + << std::setw(OCT_WIDTH) + << "" + << std::setw(1) + << "|" + << std::endl; +} + +void printTableHeader() { + std::cout << std::setfill(' ') + << " " + << std::left + << std::setw(CHAR_WIDTH) + << "CHAR" + << std::setw(1) + << " " + << std::right + << std::setw(DEC_WIDTH) + << "DEC" + << std::setw(1) + << " " + << std::left + << std::setw(HEX_WIDTH) + << "HEX" + << std::setw(1) + << " " + << std::left + << std::setw(OCT_WIDTH) + << "OCT" + << std::endl; +} + +int main(int argc, char *argv[]) { + int startValue = getIntInRangeFromInput("Enter ASCII Table Begin [32..255]: ", 32, 255); + int endValue = getIntInRangeFromInput("Enter ASCII Table End [110..255]: ", 110, 255); + + printTableHeader(); + printTableDivider(); + for (int i = startValue; i <= endValue; i++) { + std::cout << std::setfill(' ') + << std::setw(1) + << "| '" + << std::left + << std::setw(1) + << (char) i + << std::setw(1) + << "' |" + << std::right + << std::setw(DEC_WIDTH) + << std::dec + << i + << std::setw(1) + << "|0x" + << std::left + << std::setw(HEX_WIDTH-2) + << std::hex + << i + << std::setw(1) + << "|0" + << std::left + << std::setw(OCT_WIDTH-1) + << std::oct + << i + << std::setw(1) + << "|" + << std::endl; + printTableDivider(); + } + + return 0; +} diff --git a/hello_world/CMakeLists.txt b/hello_world/CMakeLists.txt index 0a04bf1..b8d9b3f 100644 --- a/hello_world/CMakeLists.txt +++ b/hello_world/CMakeLists.txt @@ -1,3 +1,3 @@ -project(helloworld) -set(SOURCES main.cpp) +project(HelloWorld) +set(SOURCES main.cpp ../ascii_table/main.cpp) add_executable(${PROJECT_NAME} ${SOURCES}) \ No newline at end of file