수업 교재 ch17 Console Input-Ouput

2022. 11. 29. 17:31언어(Language)/c++

[Exercise 17-1. CONSOLA 사용하기]

#include <iostream>
using namespace std;
#include "Consola.h"

int main() {
	gotoxy(10, 5);
	cout << "안녕하세요.";
	gotoxy(25, 12);
	cout << "제 이름은 천다인입니다.";
	return 0;
}

 

 

[Exercise 17-2 콘솔 색상 제어]

#include <iostream>
using namespace std;
#include "Consola.h"

#define _SOLIDCURSOR 2
#define  BLUE 1
#define YELLOW 14

int main() {
	clrscr(); //화면을 모두 지움
	_setcursortype(2); //cursor 모양은 BOX 모양으로
	textbackground(1);
	textcolor(14);
	gotoxy(10, 5);
	cout << "안녕하세요.";
	gotoxy(25, 12);
	cout << "제 이름은 ???입니다.";
	return 0;
}

 

 

[Exercise 17-3 콘솔 함수 확장]

ex17_3.cpp)

#include <iostream>
#include <conio.h> //getch 사용하기 위함
using namespace std;
#include "Consola.h"
#include "Util.h"
void init() { //프로그램 실행 직후 초기화를 진행할 함수 //콘솔창의 크기와 이름을 결정
	system("mode con cols=88 lines=28 | title=Welcome to PushPush World!");
}
int main() {
	init();
	clrscr(); //화면을 모두 자움
	_setcursortype(_NOCURSOR);
	Util::fillbox(3, 3, 77, 22, WHITE); //전체

	Util::fillbox(20 + 1, 5 + 1, 60 + 1, 8 + 1, DARKGRAY);
	Util::fillbox(20, 5, 60, 8, BLUE);
	textcolor(YELLOW);
	Util::xyputstr(25, 6, "안녕하세요");

	Util::fillbox(20 + 1, 15 + 1, 60 + 1, 18 + 1, DARKGRAY);
	Util::fillbox(20, 15, 60, 18, RED);
	for (int i = 0; i < 20; ++i) { //delay(int milisec)를 이용하여 중간 중간 출력시간을 조정함으로써 애니메이션 효과도 줄 수 있다. //박스가 옆으로 커지는 효과를 보여준다.
		Util::fillbox(20 - i, 15, 60 + i, 18, CYAN);
		delay(100);
	}
	textcolor(BLACK);
	Util::xyputstr(22, 16, "제 이름은 ???입니다.");
	_getch();


	
	return 0;
}

 

Util.cpp)

#include "Util.h"

 

Util.h)

#pragma once
#include <iostream>
using namespace std;
#include "Consola.h"

class Util
{
public:
	static void xyputc(int x, int y, const char ch) { //(x, y) 좌표에 문자 출력
		gotoxy(x, y);
		cout << ch;
	}
	static void xyputstr(int x, int y, const char str[]) { //문자열 출력
		gotoxy(x, y);
		cout << str;
	}
	static void fillbox(int x1, int y1, int x2, int y2,  char color) { //좌측상단(x1, y1)에서 우측하단 (x2, y2)으로 표현하는 사각형의 내부를 지정된 color로 색칠할 것
		textbackground(color); //이걸 빼먹어서 실행이 되지 않았던 것임
		for (int y = y1; y <= y2; ++y) {
			for (int x = x1; x <= x2; ++x)
				xyputc(x, y, ' ');
		}
	}
};

 

 

 

[Exercise 17-5 키 코드(key code) 찾기]

#include <iostream>
#include <conio.h>
#include "Consola.h"
using namespace std;
int main() {
	char ch;
	while (1) {
		ch = _getch();
		cout << ch << ",0x" << hex << (int)ch << endl;
	}
	return 0;
}

 

 

[Exercise 17-6 getKey() 작성하기]