Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 Hut를 인쇄하는 프로그램

<시간/>

이 자습서에서는 Hut 패턴을 인쇄하는 프로그램에 대해 설명합니다.

이를 위해 인쇄할 오두막의 너비(예:N)가 제공됩니다. 우리의 임무는 별을 사용하여 주어진 너비의 오두막 구조를 인쇄하고 선 문자를 사용하여 오두막 내부의 게이트를 인쇄하는 것입니다.

예시

#include <iostream>
using namespace std;
//printing the given hut structure
int print_hut(int n){
   int i, j, t;
      if (n % 2 == 0) {
         n++;
      }
      for (i = 0; i <= n - n / 3; i++) {
         for (j = 0; j < n; j++) {
            t = 2 * n / 5;
            if (t % 2 != 0) {
               t--;
            }
         //calculating the distance from the initial
         //character
         //and printing the outer boundary of the hut
         if (i == n / 5
            || i == n - n / 3
            || (j == n - 1 && i >= n / 5)
            || (j >= n / 5 && j < n - n / 5 && i == 0)
            || (j == 0 && i >= n / 5)
            || (j == t && i > n / 5)
            || (i <= n / 5 && (i + j == n / 5 || j - i == n / 5))
            || (j - i == n - n / 5)) {
               cout << "*";
            }
            //printing the structure of the door
         else if (i == n / 5 + n / 7 && (j >= n / 7 && j <= t - n / 7)) {
            cout << "_";
         }
         else if (i >= n / 5 + n / 7 && (j == n / 7 || j == t - n / 7)) {
            cout << "|";
         }
         else {
            cout << " ";
         }
      }
      cout << "\n";
   }
}
int main(){
   int n = 12;
   print_hut(n);
   return 0;
}

출력

 **********
* *         *
*************
*___*       *
*| |*       *
*| |*       *
*| |*       *
*| |*       *
*| |*       *
*************