119 lines
3.1 KiB
C++
119 lines
3.1 KiB
C++
/**
|
|
* 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;
|
|
}
|