C++ Programming Ch.11 Exercise 7 Solution

Problem: 0에서 127까지 ASCII 코드와 해당 문자를 다음과 같이 출력하는 프로그램을 작성하라. 화면에 출력가능하지 않는 ASCII 코드는 '.'으로 출력하라. Execution Result: Objective & Hints: cout으로 포맷 출력 응용 연습 문자가 출력 가능한지 알기 위해 bool isprint(int c); 함수를 사용하면 됩니다. 매개 변수 c는 문자 코드 값이고, 헤더 파일을 include 해야합니다. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include #include using namespace std; void showDec(int d) { // 10진수 출력 cout << setw(10) << dec << d; } void showHexa(int h) { // 16진수 출력 cout << setw(10) << hex << h; } void showChar(int c) { // ASCII 출력 int i=0; if( (i = isprint(c)) != 0) // 출력 가능한 문자인지 확인 cout << setw(10) << (char)c; else // 출력 불가능한 문자이면 "." 출력 cout << setw(10) << "."; } void print() { for(int i=0; i<4; i++){ cout << setw(10) << "dec"; cout << setw(10) << "hexa"; cout << setw(10) << "char"; } cout << endl; for(int i=0; i<4; i++){ cout << setw(10) << "---"; cout << setw(10) << "----"; cout << setw(10) << "----"; } cout << endl; for(int i=0; i<128; i++){ // 127번 반복하여 dec, hexa, ASCII 출력 if(i%4==0 && i!=0) cout << endl; showDec(i); showHexa(i); showChar(i); } } int main() { cout.setf(ios::left); // 출력 포맷 왼쪽 정렬 print(); } Explanation: ...

April 2, 2020 · 2 min · Sobamemil