~cpp
#include <iostream>
#include <windows.h>
using namespace std;
const int MIN_X=-5;
const int MAX_X=5;
const float TAB_X=1;
const float TAB_Y=0.5;
int banollim(float number)
{
//반올림하는 함수
float temp_sosu=number-(int)number;
if (temp_sosu<=-0.5 || temp_sosu>=0.5)
++number;
return number;
}
void gotoxy(int x, int y)
{
COORD Pos = {x - 1, y - 1};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
float function_x_to_y(float x)
{
//이번에 그림 함수
float y=x*x;
return y;
}
void make_image(int where_x, int where_y, float min_x, float max_x, float tab_x, float tab_y)
{
//앞에서 부터 그리기 시작할 x좌표, y좌표, x최소값, x최대값, x간격, y간격
////초기화.
int max_y=0;
int min_y=function_x_to_y(min_x);
//y의 최대 최소 구함
for (register float x=min_x; x<=max_x; x+=tab_x)
{
if (max_y<function_x_to_y(x))
max_y=function_x_to_y(x);
else if (min_y>function_x_to_y(x))
min_y=function_x_to_y(x);
}
////x,y축 생성
for (register float x=min_x; x<=max_x; ++x)
{
gotoxy(banollim(x-min_x+1+where_x),(where_y+max_y*tab_y));
printf (".");
}
for (register float y=min_y; y<=max_y; ++y)
{
gotoxy(banollim(-min_x+1+where_x),(where_y-banollim(y)*tab_y+max_y*tab_y));
printf (".");
}
////그래프 작성
for (register float x=min_x; x<=max_x; x+=tab_x)
{
gotoxy(banollim(x-min_x+1+where_x),(where_y-banollim(function_x_to_y(x))*tab_y+max_y*tab_y));
printf ("*");//(%.1f,%.1f)",x,function_x_to_y(x));
}
}
void main()
{
system("CLS");
cout << "2차함수 출력 프로그램.";
make_image( 10 , 5 , MIN_X, MAX_X,TAB_X,TAB_Y);
gotoxy(30,5);
cout << "x y";
for (register int i=0; i<MAX_X-MIN_X+1;i+=1)
{
gotoxy(30,i+6);
printf ("%3d -> %3.0f",i+MIN_X,function_x_to_y(i+MIN_X));
}
}