Add assignment for ascii_table
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
parent
e06858dcf5
commit
90a995b848
4 changed files with 126 additions and 3 deletions
|
@ -1,3 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.22)
|
||||
project(CPP)
|
||||
add_subdirectory(hello_world)
|
||||
add_subdirectory(hello_world)
|
||||
add_subdirectory(ascii_table)
|
3
ascii_table/CMakeLists.txt
Normal file
3
ascii_table/CMakeLists.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
project(ASCIITable)
|
||||
set(SOURCES main.cpp)
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
119
ascii_table/main.cpp
Normal file
119
ascii_table/main.cpp
Normal file
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
* This program outputs an ASCII-Table using only std::cout, std::cin and input/output manipulators.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
|
||||
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<std::streamsize>::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;
|
||||
}
|
|
@ -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})
|
Loading…
Add table
Add a link
Reference in a new issue