함수를 사용하여 명함 출력하기

신동원·2021년 9월 19일
0

C

목록 보기
6/10
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

#define NSTAR 30
#define NAME "Shin-dongwon"
#define ADDRESS "Seoul, Korea"

void star_print(int n_std, char c, bool print_newline);
void std_print(char str[]);

int main()
{
star_print(NSTAR, '*', true);

std_print(NAME);
std_print(ADDRESS);
star_print(NSTAR, '*', false);
return 0;
}

void star_print(int n_std, char c, bool print_newline)
{
for (int i = 0; i < n_std; i++)
{
	printf("%c", c);
}
if (print_newline)
	printf("\n");
}
void std_print(char str[])
{
int sp = 0;
sp = (NSTAR - strlen(str)) / 2;
star_print(sp, ' ', false);
printf("%s\n", str);
}

#define NSTAR 30
#define NAME "Shin-dongwon"
#define ADDRESS "Seoul, Korea"
유지보수를 쉽게 하기 위하여 define과 함수를 이용하여 프로그래밍하였다.
함수를 사용하였기 때문에 define값만 변경해주면 쉽게 원하는 출력값을 얻을 수 있다.


void star_print(int n_std, char c, bool print_newline)
{
for (int i = 0; i < n_std; i++)
{
printf("%c", c);
}
if (print_newline)
printf("\n");
}
먼저 '*'이나 띄어쓰기를 구현할 수 있는 함수이다.
입력값 n_std에 들어오는 숫자로 문자를 얼마나 출력할지 결정해주고 입력값 c로 출력할 문자를 결정한다. bool 입력으로 출력후 줄바꿈을 실행할지 안할지 결정하여주는 함수이다.


void std_print(char str[])
{
int sp = 0;
sp = (NSTAR - strlen(str)) / 2;
star_print(sp, ' ', false);
printf("%s\n", str);
}
두번째로 명함에 들어갈 내용을 작성해주는 함수이다.
NSTAR(별의 개수)에서 str의 길이를 빼고 2로 나눈값을 위에서 작성했던 star_print함수를 활용하여 띄어쓰기를 얼마나 실행할지 출력하여준다.
그 이후에 원하는 문자 str을 출력해주는 함수이다.



설정한 대로 명함이 제대로 출력된 것을 확인할 수 있다.

profile
오늘보다 내일 더 나은 사람이 되기 위해 노력하자

0개의 댓글