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

명품 C언어 프로젝트 11.1장 디지털 주판

꾸준함. 2017. 11. 4. 13:21

[응용 11.1]

/*

최대 9자리 정수를 주판에 표시하는 프로그램

*/

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <conio.h>

#include <Windows.h>

 

void draw_rectangle(int c, int r);

void display_abacus(int m[]);

void div_number(int mc[], long number);

void gotoxy(int x, int y);

 

int main(void)

{

        long number = 0;

        int mc[9];

        printf("입력된 숫자를 주판모양으로 출력\n");

        printf("0을 입력하면 종료\n");

        div_number(mc, number);

        gotoxy(1, 5);

        draw_rectangle(14, 8);

        display_abacus(mc);

        do

        {

               gotoxy(1, 16);

               printf("현재 값:%ld\n", number);

               gotoxy(1, 17);

               printf("최대 9자리 정수만 입력하고 Enter> ");

               gotoxy(34, 17);

               scanf("%ld", &number);

               if (number == 0)

                       break;

               div_number(mc, number);

               display_abacus(mc);

        } while (1 <= number);

        gotoxy(1, 17);

        return 0;

}

 

void gotoxy(int x, int y)

{

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

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

숫자를 분리하여 배열에 저장하는 함수 div_number

*/

void div_number(int mc[], long number)

{

        int i;

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

        {

               mc[i] = (int)(number / pow(10, 8 - i));

               number = number - (long)pow(10, 8 - i)*mc[i];

        }

}

 

/*

주판을 표시하는 함수 display_abacus

*/

void display_abacus(int m[])

{

        int i, j;

        unsigned char a1 = 0xa6, a2 = 0xa1;

        gotoxy(3, 4);

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

               printf("%2d ", m[i]);

        gotoxy(3, 6);

        //5 이상인지 여부

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

               if (m[i] / 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        gotoxy(3, 7);

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

               if (m[i] < 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        //1~4 표현

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

               m[i] %= 5;

        gotoxy(3, 8);

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

               printf("%c%c", a1, a2);

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

        {

               gotoxy(3, 8 + i);

               for (j = 0; j <= 8; j++)

                       if (m[j] >= i)

                              printf("%c%c ", a2, 0xdf);

                       else if (m[j] == 0)

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

                       else

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

        }

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

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

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

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

        {

               printf("%c%c", a, b[2]);

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

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 


[개선과 확장 1번]

/*

주판은 소수 이하 자릿수도 표현할 수 있으므로 소수점의 기준위치를 표시하여

입력된 임의의 실수에 대해 주판으로 표시할 수 있도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <conio.h>

#include <Windows.h>

 

void draw_rectangle(int c, int r);

void display_abacus(int m[]);

void div_number(int mc[], double number);

void gotoxy(int x, int y);

 

int main(void)

{

        double number = 0.0;

        int mc[9] = { 0 };

        printf("입력된 숫자를 주판모양으로 출력\n");

        printf("0을 입력하면 종료\n");

        div_number(mc, number);

        gotoxy(1, 5);

        draw_rectangle(14, 8);

        display_abacus(mc);

        do

        {

               gotoxy(1, 16);

               printf("현재 값:%.4lf\n", number);

               gotoxy(1, 17);

               printf("실수 입력하고 Enter(소숫점 4자리까지, 정수부분도 4자리까지) >");

               gotoxy(64, 17);

               scanf("%lf", &number);

               if ((int)number > 9999)

               {

                       gotoxy(1, 20);

                       printf("다시입력하세요");

                       gotoxy(64, 17);

                       printf("            ");

                       Sleep(1000);

                       gotoxy(1, 20);

                       printf("              ");

                       continue;

               }

               if (number == 0.0)

                       break;

               div_number(mc, number);

               display_abacus(mc);

               Sleep(1000);

               gotoxy(64, 17);

               printf("            ");

        } while (number>=1.0);

        gotoxy(1, 17);

        return 0;

}

 

void gotoxy(int x, int y)

{

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

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

숫자를 분리하여 배열에 저장하는 함수 div_number

*/

void div_number(int mc[], double number)

{

        int i;

        int tempNum = (int)number;

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

        {

               mc[i] = (int)(tempNum / pow(10, 3 - i));

               tempNum = tempNum - (long)pow(10, 3 - i)*mc[i];

        }

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

        {

               number *= 10;

               mc[i] = (int)number % 10;

        }

        gotoxy(1, 24);

}

 

/*

주판을 표시하는 함수 display_abacus

*/

void display_abacus(int m[])

{

        int i, j;

        unsigned char a1 = 0xa6, a2 = 0xa1;

        gotoxy(3, 4);

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

               if (i != 4)

                       printf("%2d ", m[i]);

               else

                       printf(" . ");

        gotoxy(3, 6);

        //5 이상인지 여부

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

               if (m[i] / 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        gotoxy(3, 7);

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

               if (m[i] < 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        //1~4 표현

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

               m[i] %= 5;

        gotoxy(3, 8);

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

               printf("%c%c", a1, a2);

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

        {

               gotoxy(3, 8 + i);

               for (j = 0; j <= 8; j++)

                       if (m[j] >= i)

                              printf("%c%c ", a2, 0xdf);

                       else if (m[j] == 0)

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

                       else

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

        }

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

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

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

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

        {

               printf("%c%c", a, b[2]);

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

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

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

                printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 


[개선과 확장 2번]

/*

1부터 10까지 숫자를 더해 나가는 과정을 animation과 같이 단계별로 화면에 출력되도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <conio.h>

#include <Windows.h>

 

void draw_rectangle(int c, int r);

void display_abacus(int m[]);

void div_number(int mc[], long number);

void gotoxy(int x, int y);

 

int main(void)

{

        long number = 0;

        int i = 1;

        int mc[9];

        printf("1~10까지의 숫자를 더해 나가는 과정 표현\n");

        div_number(mc, number);

        gotoxy(1, 5);

        draw_rectangle(14, 8);

        display_abacus(mc);

        gotoxy(1, 20);

        printf("키보드를 입력하면 시작");

        getch();

        gotoxy(1, 20);

        printf("                      ");

        do

        {

               number += i;

               gotoxy(1, 16);

               printf("%d", number);

               div_number(mc, number);

               display_abacus(mc);

               Sleep(1000);

               i++;

        } while (i<=10);

        gotoxy(1, 17);

        return 0;

}

 

void gotoxy(int x, int y)

{

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

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

숫자를 분리하여 배열에 저장하는 함수 div_number

*/

void div_number(int mc[], long number)

{

        int i;

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

        {

               mc[i] = (int)(number / pow(10, 8 - i));

               number = number - (long)pow(10, 8 - i)*mc[i];

        }

}

 

/*

주판을 표시하는 함수 display_abacus

*/

void display_abacus(int m[])

{

        int i, j;

        unsigned char a1 = 0xa6, a2 = 0xa1;

        gotoxy(3, 4);

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

               printf("%2d ", m[i]);

        gotoxy(3, 6);

        //5 이상인지 여부

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

               if (m[i] / 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        gotoxy(3, 7);

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

               if (m[i] < 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        //1~4 표현

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

               m[i] %= 5;

        gotoxy(3, 8);

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

               printf("%c%c", a1, a2);

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

        {

               gotoxy(3, 8 + i);

               for (j = 0; j <= 8; j++)

                       if (m[j] >= i)

                              printf("%c%c ", a2, 0xdf);

                       else if (m[j] == 0)

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

                       else

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

        }

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

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

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

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

        {

               printf("%c%c", a, b[2]);

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

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 


[개선과 확장 3번]

/*

사칙연산을 위한 값의 입력과 계산과정을 주판을 통해 보여줄 수 있도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <conio.h>

#include <Windows.h>

 

void draw_rectangle(int c, int r);

void display_abacus(int m[]);

void div_number(int mc[], long number);

void gotoxy(int x, int y);

 

int main(void)

{

        long number1 = 0, number2=0;

        int i = 1;

        int mc[9];

        printf("1~10까지의 숫자를 더해 나가는 과정 표현\n");

        div_number(mc, number1);

        gotoxy(1, 5);

        draw_rectangle(14, 8);

        display_abacus(mc);

        gotoxy(1, 20);

        printf("키보드를 입력하면 시작");

        getch();

        gotoxy(1, 20);

        printf("                      ");

        do

        {

               int sel;

               gotoxy(1, 19);

               printf("              ");

               gotoxy(20, 16);

               printf("              ");

               gotoxy(30, 17);

               printf("              ");

               gotoxy(50, 18);

               printf("              ");

               gotoxy(1, 16);

               printf("첫번째 숫자 입력:");

               gotoxy(20, 16);

               scanf("%d", &number1);

               div_number(mc, number1);

               display_abacus(mc);

               Sleep(1500);

               gotoxy(1, 17);

               printf("연산 선택> 1.+ 2.- 3.* 4./ >");

               gotoxy(30, 17);

               scanf("%d", &sel);

               gotoxy(1, 18);

               printf("두번째 숫자 입력:(프로그램 종료 원할시 0 입력)>");

               gotoxy(50, 18);

               scanf("%d", &number2);

               div_number(mc, number2);

               display_abacus(mc);

               Sleep(1500);

               switch (sel)

               {

               case 1:

                       number1 += number2;

                       break;

               case 2:

                       number1 -= number2;

                       break;

               case 3:

                       number1 *= number2;

                       break;

               case 4:

                       number1 = (int)(number1 / number2);

                       break;

               }

               div_number(mc, number1);

               display_abacus(mc);

               gotoxy(1, 19);

               printf("결과: %d", number1);

               Sleep(1500);

        } while (number2!=0);

        gotoxy(1, 20);

        return 0;

}

 

void gotoxy(int x, int y)

{

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

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

숫자를 분리하여 배열에 저장하는 함수 div_number

*/

void div_number(int mc[], long number)

{

        int i;

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

        {

               mc[i] = (int)(number / pow(10, 8 - i));

               number = number - (long)pow(10, 8 - i)*mc[i];

        }

}

 

/*

주판을 표시하는 함수 display_abacus

*/

void display_abacus(int m[])

{

        int i, j;

        unsigned char a1 = 0xa6, a2 = 0xa1;

        gotoxy(3, 4);

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

               printf("%2d ", m[i]);

        gotoxy(3, 6);

        //5 이상인지 여부

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

               if (m[i] / 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        gotoxy(3, 7);

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

               if (m[i] < 5)

                       printf("%c%c ", a1, 0xa2);

               else

                       printf("%c%c ", a2, 0xdf);

        //1~4 표현

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

               m[i] %= 5;

        gotoxy(3, 8);

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

               printf("%c%c", a1, a2);

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

        {

               gotoxy(3, 8 + i);

               for (j = 0; j <= 8; j++)

                       if (m[j] >= i)

                              printf("%c%c ", a2, 0xdf);

                       else if (m[j] == 0)

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

                       else

                       {

                              printf("%c%c ", a1, 0xa2);

                              m[j] = 5;

                       }

        }

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

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

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

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

        {

               printf("%c%c", a, b[2]);

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

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

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

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 


개발환경:Visual Studio 2017


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


[참고] 명품 C언어 프로젝트 안기수 저

반응형