명품 C++ programming 실습 문제 11장 7번

문제 : 0에서 127까지 ASCII 코드와 해당 문자를 다음과 같이 출력하는 프로그램을 작성하라. 화면에 출력가능하지 않는 ASCII 코드는 ‘.‘으로 출력하라. 실행 결과 : 목적 및 힌트 : cout으로 포맷 출력 응용 연습 문자가 출력 가능한지 알기 위해 bool isprint(int c); 함수를 사용하면 됩니다. 매개 변수 c는 문자 코드 값이고, 헤더 파일을 include 해야합니다. 코드 : 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(); } 설명 : ...

April 2, 2020 · 2 min · Sobamemil