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

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

꾸준함. 2017. 6. 23. 18:24

[1번 문제]

/*

다음과 같은 메뉴에 의해 실제 값을 입력받아 해당 연산을 처리하는 프로그램을 작성합니다

*/

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <math.h>

 

int menu_display(void);

int sub_menu_display01(void);

int sub_menu_display02(void);

void sub_main01(void);

void sub_main02(void);

 

void add(void);

void subtract(void);

void multiply(void);

void divide(void);

void remain(void);

void press_any_key(void);

 

void naturallog(void);

void decimallog(void);

 

int main(void)

{

        int c;

        while ((c = menu_display()) != 3)

        {

               switch (c)

               {

               case 1:

                       sub_main01();

                       break;

               case 2:

                       sub_main02();

                       break;

               default:

                       break;

               }

        }

        return 0;

}

 

int menu_display(void)

{

        int select;

        system("cls");

        printf("1.사칙 연산\n");

        printf("2.로그 연산\n");

        printf("3.프로그램 종료\n\n");

        printf("메뉴번호 입력>");

        select = getch() - 48;

        return select;

}

 

void sub_main01(void)

{

        int c;

        while ((c = sub_menu_display01()) != 6)

        {

               switch (c)

               {

               case 1:

                       add();

                       break;

               case 2:

                       subtract();

                       break;

               case 3:

                       divide();

                       break;

               case 4:

                       multiply();

                       break;

               case 5:

                       remain();

                       break;

               default:

                       break;

               }

        }

}

 

void sub_main02(void)

{

        int c;

        while ((c = sub_menu_display02()) != 3)

        {

               switch (c)

               {

               case 1:

                       naturallog();

                       break;

               case 2:

                       decimallog();

                       break;

               default:

                       break;

               }

        }

}

 

int sub_menu_display01(void)

{

        int select;

        system("cls");

        printf("사칙 연산\n\n");

        printf("1.덧셈(+) 연산\t");

        printf("2.뺄셈(-) 연산\n");

        printf("3.나눗셈(/) 연산\t");

        printf("4.곱셈(*) 연산\n");

        printf("5.나머지(%) 연산\t");

        printf("6.메인 메뉴로 이동\n");

        select = getch() - 48;

        return select;

}

 

int sub_menu_display02(void)

{

        int select;

        system("cls");

        printf("로그 연산\n\n");

        printf("1.자연로그(log) 계산\t");

        printf("2.상용로그(log10) 계산\n");

        printf("3.메인 메뉴로 이동\n");

        select = getch() - 48;

        return select;

}

 

void add(void)

{

        int num1, num2;

        printf("두 정수 입력:");

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

        printf("%d+%d=%d\n", num1, num2, num1 + num2);

        press_any_key();

}

 

void subtract(void)

{

        int num1, num2;

        printf("두 정수 입력:");

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

        printf("%d-%d=%d\n", num1, num2, num1 - num2);

        press_any_key();

}

 

void divide(void)

{

        int num1, num2;

        printf("두 정수 입력:");

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

        printf("%d/%d=%d\n", num1, num2, num1 / num2);

        press_any_key();

}

 

void multiply(void)

{

        int num1, num2;

        printf("두 정수 입력:");

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

        printf("%d*%d=%d\n", num1, num2, num1 * num2);

        press_any_key();

}

 

void remain(void)

{

        int num1, num2;

        printf("두 정수 입력:");

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

        printf("(%d)%%(%d)=%d\n", num1, num2, num1 % num2);

        press_any_key();

}

 

void press_any_key(void)

{

        printf("\n\n");

        printf("아무키나 누르면 이전 메뉴로...");

        getch();

}

 

void naturallog(void)

{

        double num;

        printf("숫자 입력:");

        scanf("%lf", &num);

        printf("%f를 자연로그연산 시키면 %f\n", num, log(num));

        press_any_key();

}

 

void decimallog(void)

{

        double num;

        printf("숫자 입력:");

        scanf("%lf", &num);

        printf("%f를 로그연산 시키면 %f\n", num, log10(num));

        press_any_key();

}

 


[2번 문제]

/*

다음과 같은 서브 메뉴로 진행하되 각각의 서브 메뉴에 대해 간단한 조리방법을 소개하는 프로그램을 작성합니다

*/

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

 

int menu_display(void);

int sub_menu_display01(int);

int sub_menu_display02(int);

void sub_main01(void);

void sub_main02(void);

void press_any_key(void);

 

 

 

int main(void)

{

        int c;

        while ((c = menu_display()) != 3)

        {

               switch (c)

               {

               case 1:

                       sub_main01();

                       break;

               case 2:

                       sub_main02();

                       break;

               default:

                       break;

               }

        }

        return 0;

}

 

int menu_display(void)

{

        int select;

        system("cls");

        printf("1.한식\n");

        printf("2.중식\n");

        printf("3.종료\n");

        select = getch() - 48;

        return select;

}

 

void sub_main01(void)

{

        int c;

        while ((c = sub_menu_display01(1)) != 3)

        {

               switch (c)

               {

               case 1:

                       while ((c = sub_menu_display02(1)) != 3)

                       {

                              switch (c)

                              {

                              case 1:

                                      printf("갈비찜\n");

                                      press_any_key();

                                      break;

                              case 2:

                                      printf("잡채\n");

                                      press_any_key();

                                      break;

                              }

                       }

                       break;

               case 2:

                       while ((c = sub_menu_display02(2)) != 3)

                       {

                              switch (c)

                              {

                              case 1:

                                      printf("육개장\n");

                                      press_any_key();

                                      break;

                              case 2:

                                      printf("비빔밥\n");

                                      press_any_key();

                                      break;

                              default:

                                      break;

                              }

                       }

                       break;

               default:

                       break;

               }

        }

}

 

void sub_main02(void)

{

        int c;

        while ((c = sub_menu_display01(2)) != 3)

        {

               switch (c)

               {

               case 1:

                       while ((c = sub_menu_display02(3)) != 3)

                       {

                              switch (c)

                              {

                              case 1:

                                      printf("탕수육\n");

                                      press_any_key();

                                      break;

                              case 2:

                                      printf("팔보채\n");

                                      press_any_key();

                                      break;

                              default:

                                      break;

                              }

                       }

                       break;

               case 2:

                       while ((c = sub_menu_display02(4)) != 3)

                       {

                              switch (c)

                              {

                              case 1:

                                      printf("짜장면\n");

                                      press_any_key();

                                      break;

                              case 2:

                                      printf("짬뽕\n");

                                      press_any_key();

                                      break;

                              default:

                                      break;

                              }

                       }

                       break;

               default:

                       break;

               }

        }

}

 

int sub_menu_display01(int select)

{

        int num;

        system("cls");

        if (select == 1)

        {

               printf("1.1 요리부\n");

               printf("1.2 식사부\n");

               printf("1.3 메인 메뉴로 이동\n");

        }

        else

        {

               printf("2.1 요리부\n");

               printf("2.2 식사부\n");

               printf("2.3 메인 메뉴로 이동\n");

        }

        num = getch() - 48;

        return num;

}

 

int sub_menu_display02(int select)

{

        int num;

        system("cls");

        if (select == 1)

        {

               printf("1.1.1 갈비찜\n");

               printf("1.1.2 잡채\n");

               printf("1.1.3 이전 메뉴로 이동\n");

        }

        else if (select == 2)

        {

               printf("1.2.1 육개장\n");

               printf("1.2.2 비빔밥\n");

               printf("1.2.3 이전 메뉴로 이동\n");

        }

        else if (select == 3)

        {

               printf("2.1.1 탕수육\n");

               printf("2.1.2 팔보채\n");

               printf("2.1.3 이전 메뉴로 이동\n");

        }

        else if (select == 4)

        {

               printf("2.2.1 짜장면\n");

               printf("2.2.2 짬뽕\n");

               printf("2.2.3 이전 메뉴로 이동\n");

        }

        num = getch() - 48;

        system("cls");

        return num;

}

 

void press_any_key(void)

{

        printf("중략\n\n");

        printf("아무키나 누르면 이전 메뉴로...");

        getch();

}

 


[3번 문제]

/*

다음과 같은 메뉴로 진행하는 구구단 학습 프로그램을 작성합니다

*/

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

 

int menu_display(void);

void submain01(void);

void submain02(void);

void press_any_key(void);

 

int main(void)

{

        int c;

        while ((c = menu_display()) != 3)

        {

               switch (c)

               {

               case 1:

                       submain01();

                       break;

               case 2:

                       submain02();

                       break;

               default:

                       break;

               }

        }

        return 0;

}

 

int menu_display(void)

{

        int select;

        system("cls");

        printf("1.구구단 보기\n");

        printf("2.구구단 문제\n");

        printf("3.종료\n");

        select = getch() - 48;

        return select;

}

 

void submain01(void)

{

        int dan, num=0;

        while (num != 3)

        {

               system("cls");

               printf("1.1 원하는 단만 보기\n");

               printf("1.2 전체 보기\n");

               printf("1.3 메인 메뉴로 이동\n");

               num = getch() - 48;

               switch (num)

               {

               case 1:

                       printf("원하는 단:");

                       scanf("%d", &dan);

                       for (int i = 1; i < 10; i++)

                              printf("%d*%d=%d\n", dan, i, dan*i);

                       break;

               case 2:

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

                       {

                              for (int j = 1; j < 10; j++)

                              {

                                      printf("%d*%d=%d\n", i, j, i*j);

                              }

                       }

                       break;

               }

               press_any_key();

        }

}

 

void submain02(void)

{

        int dan, is, num=0, answer;

        srand((unsigned)time(NULL));

        while (num != 3)

        {

               system("cls");

               printf("1.1 원하는 단의 문제 풀기\n");

               printf("1.2 임의의 문제 풀기\n");

               printf("1.3 메인 메뉴로 이동\n");

               num = getch() - 48;

               switch (num)

               {

               case 1:

                       printf("원하는 단:");

                       scanf("%d", &dan);

                       for (int i = 1; i < 10; i++)

                       {

                              printf("%d*%d=", dan, i);

                              scanf("%d", &answer);

                              if (answer == dan*i)

                                      printf("맞았습니다\n");

                              else

                                      printf("틀렸습니다. 답은 %d입니다\n\n", dan*i);

                       }

                       break;

               case 2:

                       dan = rand() % 9 + 1;

                       is = rand() % 9 + 1;

                       printf("%d*%d=", dan, is);

                       scanf("%d", &answer);

                       if (answer == dan*is)

                              printf("맞았습니다\n");

                       else

                              printf("틀렸습니다. 답은 %d입니다\n\n", dan*is);

                       break;

               }

               press_any_key();

        }

}

 

void press_any_key()

{

        printf("\n\n");

        printf("아무키나 누르면 이전 메뉴로...");

        getch();

}

 


개발환경:Visual Studio 2017


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


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


반응형