본문 바로가기

과제모음

[C++]자료의 입출력

반응형

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;


void max();
void fact();
void mul();

int main()
{
 int sel;
 
 while(1)
 {
  system("cls");
  cout << "===================================================" << endl;
  cout << " 메뉴1) 두 수를 입력받아 둘 중 큰 수를 출력하기" << endl;
  cout << " 메뉴2) 한 수를 입력받아 팩토리얼(!)연산의 식과 결과 값을 출력하기" << endl;
  cout << " 메뉴3) 구구단 출력하기" << endl;
  cout << " 메뉴4) 종료" << endl << "===================================================" << endl;
 
  cout << "선택 : [ ]\b\b";
  cin >> sel;
  switch(sel)
  {
  case 1:
   max();
   break;
  case 2:
   fact();
   break;
  case 3:
   mul();
   break; 
  case 4:
   cout <<endl << " 프로그램을 종료 합니다." << endl << endl;
   getch();
   exit(1);
  }
 }
 return 0;
}

void max()
{
 system("cls");
 int min, max;
 cout << "       *** 큰 수 구 하 기 ***" << endl << endl;
 cout << "두 개의 정수 입력 :";
 cin >> min >> max;

 if(min < max){
  cout << "▶ 두 수 중 큰 수는 [" << max << "] 입니다." << endl << endl;
  cout << "아무키나 누르면 메뉴로 돌아갑니다.";
 }
 else if(min > max){
  cout << "▶ 두 수 중 큰 수는 [" << min << "] 입니다." << endl << endl;
  cout << "아무키나 누르면 메뉴로 돌아갑니다.";
 }
 else{
  cout << "▶ 두 수는 같습니다." << endl << endl;
  cout << "아무키나 누르면 메뉴로 돌아갑니다.";
 }
 getch();
}

void fact()
{
 system("cls");
 int fac, n, resul;
 cout << "       *** 팩토리얼 ***" << endl << endl ;
 cout << "정수입력 : ";
 cin >> fac;
 resul = 1;
 cout << fac << "!  =  " ;
 for(n=fac; n >=1; n--)
 {
  cout << n;
  resul *= n;
  if(n > 1){
  cout << " * ";
  }
 }
 cout << endl << endl;
 cout << fac << "! = " << resul;
 cout << endl << endl << "아무키나 누르면 메뉴로 돌아갑니다.";
 getch();
}
void mul()
{
 while(1)
 {
  system("cls");
  cout << "        *** 구구단 ***" << endl << endl ;
  int star, end, n, i;
  cout << " 시작 단(1~9) : ";
  cin >> star;
  cout << "  끝 단(1~9) : ";
  cin >> end;

  if((star < 1 || star > 9) || (end < 1 || end > 9))
  {
   cout <<endl << " 1~9 사이의 정수만 입력해주세요." << endl;
   getch();
   continue;
  }

  for(n = star; n <= end; n++)
  {
   for(i = 1; i <= 9; i++)
   {
    cout << n << "*" << i << " = " << n*i << " ";
   }
   cout << endl;
  }
  cout << endl << endl << " 아무키나 누르면 메뉴로 돌아갑니다.";
  getch();
  break;
 }
}

 예전에 공부했던 자료들입니다.

반응형