C/명품 C언어 프로젝트(안기수 저)

명품 C언어 프로젝트 4.4장 연습문제

꾸준함. 2017. 7. 11. 23:23

[1번 문제]

/*

좌표를 나타내는 부분을 구조체로 정의하여 출력하는 프로그램으로 수정한다

*/

#include <stdio.h>

#include <math.h>

 

#define PI 3.14

 

typedef struct _point

{

        int x;

        int y;

}Point; //m[2]를 구조체로 변경

 

void rotation(Point *p, double result[], double d);

double dtor(double degree);

 

int main(void)

{

        Point p = { 2, 1 };

        double result[2];

        rotation(&p, result, 90);

        for (int i = 0; i < 2; i++)

               printf("%4.1f ", result[i]);

        printf("\n");

        return 0;

}

 

void rotation(Point *p, double result[], double d)

{

        double rotation_m[2][2] = { cos(dtor(d)), -sin(dtor(d)), sin(dtor(d)), cos(dtor(d)) };

        int r, c, k;

        for (r = 0; r < 2; r++)

        {

               for (c = 0; c < 2; c++)

               {

                       result[r] = 0;

                       result[r] += rotation_m[r][0] * p->x;

                       result[r] += rotation_m[r][1] * p->y;

               }

        }

}

 

double dtor(double degree)

{

        return degree*PI / 180;

}


[2번 문제]

/*

R을 실수 전체 집합이라 하고, 함수 f g를 다음과 같이 정의할 때 f(g(x))=x

만족하는 x의 값을 구하는 프로그램을 작성합니다.

, x의 범위는 0.1<=x<=1 사이의 실수라고 가정합니다

*/

#include <stdio.h>

 

double fx(double x);

double gx(double x);

 

int main(void)

{

        for (double i = 0.1; i < 1.0; i += .00001)

        {

               if (fx(gx(i)) == i)

                       printf("x=%.5f일 때 f(g(%.5f))=%.5f가 성립된다\n", i, i, i);

        }

        return 0;

}

 

double fx(double x)

{

        return x / (x + 1);

}

 

double gx(double x)

{

        return 1 / (1 - x);

}

[3번 문제]

/*

다음 함수에 의해 계산된 값을 [예제 4.4.3]을 이용하여 좌표상에 나타낸다.

R을 정수 전체 집합이라 하고, 함수 f를 다음과 같이 정의할 때 함수 값 f(-2)

f(-1), f(0), f(1), f(2)를 계산한다고 가정합니다

 

1.f:R->R f(x)=x^2-2x+3

2.f:R->R f(x)=x^3-2*x^2-5

*/

#include <stdio.h>

#include <Windows.h>

 

#define f(x) ((x*x)-2*x+3)

#define g(x) ((x*x*x)-2*(x*x)-5)

 

void gotoxy(int x, int y);

void axis(void);

void draw_xy_pair1(int x[], int fx[]);

void draw_xy_pair2(int x[], int gx[]);

 

int main(void)

{

        int x[] = { -2, -1, 0, 1, 2}, fx[5], gx[5];

        system("cls");

        axis();

        draw_xy_pair1(x, fx);

        Sleep(5000); //5초 후

        system("cls");

        axis();

        draw_xy_pair2(x, gx);

        gotoxy(1, 23);

        printf("\n");

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

void axis(void)

{

        int i;

        for (i = 1; i <= 24; i++)

        {

               gotoxy(39, i);

               printf("|");

        }

        for (i = 1; i <= 79; i++)

        {

               gotoxy(i, 12);

               printf("-");

        }

        gotoxy(39, 12);

        printf("+");

}

 

void draw_xy_pair1(int x[], int fx[])

{

        int i, xt, yt;

        gotoxy(1, 1);

        printf("x^2-2*x+3");

        for (i = 0; i < 5; i++)

        {

               fx[i] = f(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 

void draw_xy_pair2(int x[], int gx[])

{

        int i, xt, yt;

        gotoxy(1, 1);

        printf("x^3-2*x^2-5");

        for (i = 0; i < 5; i++)

        {

               gx[i] = g(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - gx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}


[4번 문제]

/*

정수 x의 범위가 1<=x<=16일 때 다음의 함수의 값을 계산하고,

이 결과를 좌표상에 그래프로 나타내는 프로그램을 작성한다

 

y=2^|x|, y=(1/2)^x, y=2^(2-x)-2, y=log(x+6)/log(2), y=-log(x+x/2)/log(2)

*/

#include <stdio.h>

#include <Windows.h>

#include <math.h>

 

#define f(x) pow(2, x)

#define g(x) pow(1/2, x)

#define h(x) pow(2, 2-x)-2

#define i(x) log(x+6)/log(2)

#define j(x) -log(x+x/2)/log(2)

 

void gotoxy(int x, int y);

void axis(void);

void draw_xy_pair1(int x[], double fx[]);

void draw_xy_pair2(int x[], double fx[]);

void draw_xy_pair3(int x[], double fx[]);

void draw_xy_pair4(int x[], double fx[]);

void draw_xy_pair5(int x[], double fx[]);

 

int main(void)

{

        int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

        double fx[16];

        system("cls");

        axis();

        draw_xy_pair1(x, fx);

        Sleep(2000); //2초간 화면정지

        system("cls");

        axis();

        draw_xy_pair2(x, fx);

        Sleep(2000);

        system("cls");

        axis();

        draw_xy_pair3(x, fx);

        Sleep(2000);

        system("cls");

        axis();

        draw_xy_pair4(x, fx);

        Sleep(2000);

        system("cls");

        axis();

        draw_xy_pair5(x, fx);

        gotoxy(1, 23);

        printf("\n");

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

void axis(void)

{

        int i;

        for (i = 1; i <= 24; i++)

        {

               gotoxy(39, i);

               printf("|");

        }

        for (i = 1; i <= 79; i++)

        {

               gotoxy(i, 12);

               printf("-");

        }

        gotoxy(39, 12);

        printf("+");

}

 

void draw_xy_pair1(int x[], double fx[])

{

        double xt, yt;

        gotoxy(1, 1);

        printf("y=2^|x|");

        for (int i = 0; i<16; i++)

        {

               fx[i] = f(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 

void draw_xy_pair2(int x[], double fx[])

{

        double xt, yt;

        gotoxy(1, 1);

        printf("y=(1/2)^x");

        for (int i = 0; i<16; i++)

        {

               fx[i] = g(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 

void draw_xy_pair3(int x[], double fx[])

{

        double xt, yt;

        gotoxy(1, 1);

        printf("y=2^(2-x)-2");

        for (int i = 0; i<16; i++)

        {

               fx[i] = h(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 

void draw_xy_pair4(int x[], double fx[])

{

        double xt, yt;

        gotoxy(1, 1);

        printf("y=log(x+6)/log(2)");

        for (int i = 0; i<16; i++)

        {

               fx[i] = i(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 

void draw_xy_pair5(int x[], double fx[])

{

        double xt, yt;

        gotoxy(1, 1);

        printf("y=-log(x+x/2)/log(2)");

        for (int i = 0; i<16; i++)

        {

               fx[i] = j(x[i]);

               xt = 39 + 2 * x[i];

               yt = 12 - fx[i];

               if ((1 <= yt) && (yt <= 23))

               {

                       gotoxy(xt, yt);

                       printf("*");

               }

        }

}

 


[5번 문제]

/*

천만원을 연 이율 0.1을 적용하여 1년마다 복리로 10년간 예금한다고 할 때 원리합계를 계산합니다

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int main(void)

{

        double principle, rate; //원금 이자율

        double *income; //수익

        int year;

 

        printf("원금: ");

        scanf("%lf", &principle);

 

        printf("이자율: ");

        scanf("%lf", &rate);

 

        printf("기간: ");

        scanf("%d", &year);

 

        income = (double*)malloc(sizeof(double)*year);

 

        for (int i = 0; i < year; i++)

        {

               income[i] = principle*(double)(pow(1 + 0.01*rate, i + 1)); //원금*(1+이자율)^예치기간

               printf("%d년 예치시:%.2f(%.1f%%)\n", i + 1, income[i], rate);

        }

        free(income); //메모리 해제

        return 0;

}

 


[6번 문제]

/*

다음의 형식에 맞게 상용로그를 출력하는 프로그램을 작성합니다.

, 값은 소수 이하 4째 자리까지만 계산합니다

*/

#include <stdio.h>

#include <Windows.h>

#include <math.h>

 

int main(void)

{

        printf("x      ");

        for (double x = 1.0; x <= 2.0; x+=.1)

        {

               printf("%3.4lf ", x);

        }

        printf("\n");

        printf("log10x ");

        for (double x = 1.0; x <= 2.0; x += .1)

        {

               printf("%3.4lf ", log10(x));

        }

        printf("\n");

        return 0;

}


[7번 문제]

/*

임의의 좌표(x, y)에 대해 전환과 회전을 연속적으로 적용할 수 있는 프로그램을 작성한다

*/

#include <stdio.h>

#include <math.h>

 

#define PI 3.14

 

void rotation(double m[], double result[], double d);

double dtor(double degree);

 

int main(void)

{

        int angle;

        int num1, num2;

        double m[2] = { 0, 0 }, result[2];

        while (1)

        {

               printf("x y 좌표 입력:(0, 0 입력시 종료) ");

               scanf("%d %d", &num1, &num2);

               if (num1 == 0 && num2 == 0)

                       break;

               m[0] = num1;

               m[1] = num2;

               printf("회전하고 싶은 각도: ");

               scanf("%d", &angle);

               rotation(m, result, angle);

               for (int i = 0; i < 2; i++)

                       printf("%4.1f ", result[i]);

               printf("\n");

        }

        return 0;

}

 

void rotation(double m[], double result[], double d)

{

        double rotation_m[2][2] = { cos(dtor(d)), -sin(dtor(d)), sin(dtor(d)), cos(dtor(d)) };

        int r, c, k;

        for (r = 0; r < 2; r++)

        {

               for (c = 0; c < 2; c++)

               {

                       result[r] = 0;

                       for (k = 0; k < 2; k++)

                              result[r] += rotation_m[r][k] * m[k];

               }

        }

}

 

double dtor(double degree)

{

        return degree*PI / 180;

}

 


[8번 문제]

/*

각도가 0~360도로 15도씩 변화할 때마다 좌표 상에 sine 곡선을 출력하는 프로그램을 작성한다

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

#define OFFSET 10

#define WIDTH 20

#define VSYMBOL '|'

#define HSYMBOL '-'

#define CSYMBOL '*'

 

#define PI 3.141592

#define delta 0.1

 

void draw_v_boundary();

void blank(int no);

 

int main(void)

{

        int i, k, pos;

        double theta, s;

 

        draw_v_boundary();

 

        for (theta = 0.0; theta <= 2.0*PI; theta += delta)

        {

               blank(OFFSET);

               printf("%c", VSYMBOL);

               s = sin(theta);

               if (s<0)  //사인값이 음수라면

               {

                       pos = (int)(WIDTH + s*WIDTH);

                       blank(pos);

                       printf("%c", CSYMBOL);

                       blank(WIDTH - pos - 1);

                       printf("%c", VSYMBOL);

                       blank(WIDTH);

               }

               else //사인값이 양수라면

               {

                       pos = (int)(s*WIDTH);

                       blank(WIDTH);

                       printf("%c", VSYMBOL);

                       blank(pos);

                       printf("%c", CSYMBOL);

                       blank(WIDTH - pos - 1);

               }

               printf("%c", VSYMBOL);

               printf("\n");

        }

 

        draw_v_boundary();

 

        system("PAUSE");

 

        return 0;

}

 

void draw_v_boundary() //직사각형의 가로

{

        int i;

        blank(OFFSET);

        for (i = 0; i<2 * WIDTH + 3; i++)

               printf("%c", HSYMBOL);

        printf("\n");

}

 

void blank(int no) //공백

{

        while (0<no--)

               printf(" ");

}



개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 명품 C언어 프로젝트 안기수 저, http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=129941183&qb=Q+yWuOyWtCBTSU4g6rOh7ISg&enc=utf8&section=kin&rank=9&search_sort=0&spq=0


*8번 같은 경우 gotoxy()문으로는 실수를 표현하는데 한계를 느껴 결국 검색해봤습니다

반응형