Study

[방송통신대] C프로그래밍 과제

cattaku 2020. 4. 14. 17:17

아래 조건을 만족하는 프로그램을 만들고 프로그램 실행 예와 같이 동작하는 프로그램을 작성하시오.

1. 반복문을 이용하여 3명의 이름과 학과, 주민등록번호를 입력받아 프로그램 실행 예와 같이 이름, 생년월일, 윤년 여부, 출생지역(대한민국, 외국), 성별(남자, 여자), 학과 이름을 출력한다.

생년월일

주민등록번호 앞 6자리 숫자와 7번째 숫자를 분석하여 xxxxxxxx일로 출력

성별

출생년대

출생지역

주민등록번호 7번째 숫자로 남자, 여자, 출생년대, 출생지역 구분

1. 1900년대 출생한 남자                2. 1900년대 출생한 여자
3. 2000년대 출생한 남자                4. 2000년대 출생한 여자
5. 1900년대 출생한 외국인 남자       6. 1900년대 출생한 외국인 여자
7. 2000년대 출생한 외국인 남자       8. 2000년대 출생한 외국인 여자
9. 1800년대 출생한 남자                10. 1800년대 출생한여자

윤년

윤년은 2월달이 29일이 되는 해를 말함
조건 1. 년도가 4로 나누어지고 100으로 나누어지면 안됨
조건 2. 조건 1번과 상관없이 400으로 나누어지면 윤년

2. 입력 받은 3명의 정보를 생년월일을 기준으로 오름차순으로 출력한다.

[ 소스코드 ]

#include <stdio.h>
#include <string.h>
#include <conio.h>
typedef struct Student Student;
//구조체
struct Student {
	char name[10];		//학생명
	char dept[15];		//학과
	char born[14];		//주민등록번호
	//주민등록번호 생년월일, 성별 
	int b_year;			
	int b_month;		
	int b_day;
	int gender;
};
// 정렬 함수 선언
void _Sort(Student* st, int count) {   // 매개변수로 정렬할 배열과 요소의 개수를 받음
	struct Student temp;  
	for (int i = 0; i < count; i++) {    // 요소의 개수만큼 반복
	
		for (int j = 0; j < count - 1; j++) {   // 요소의 개수 - 1만큼 반복
		
			if (st[j].b_year > st[j + 1].b_year) {   
			// 현재 요소의 값과 다음 요소의 값을 비교하여 큰 값을 다음 요소로 보냄
			                               
				temp = st[j];
				st[j] = st[j + 1];
				st[j + 1] = temp;            
			}
		}
	}
}
//메인 함수
int main(void) {
	Student* studentList = 0;
	studentList = (Student*)malloc(sizeof(Student) * 3); 
				//메모리 할당, 배열의 크기만큼 할당하기 위해 3를 곱함
	printf("3명의 학생 정보를 입력하시오.\n");
	printf("==================================================================\n");
	for (int i = 0; i < 3; i++) {
		
		//이름 입력
		printf("이 름 : ");
		scanf_s("%s", studentList[i].name, sizeof(studentList[i].dept));
		//학과 입력
		printf("학 과 : ");
		scanf_s("%s", studentList[i].dept, sizeof(studentList[i].dept));
		//주민등록번호 입력
		printf("주민등록번호 : ");
		scanf_s("%s", studentList[i].born, sizeof(studentList[i].born));
		
		//정상 작동하지 않아 주석처리함
		/*if (strlen(stut[i].born) != 13) {
			printf("주민등록번호는 13자리여야 합니다.\n");
			continue;
		}*/
		//입력된 주민번호를 년,월,일로 저장
		sscanf(studentList[i].born, "%2d%2d%2d", &studentList[i].b_year, &studentList[i].b_month,
		&studentList[i].b_day);
		// 주민등록번호 7번째 자리로 성별, 출신지역을 나눠 gender 변수에 저장
		if (studentList[i].born[6] == '1' || studentList[i].born[6] == '3' || studentList[i].born[6] == '9') 
			studentList[i].gender = 1;  //한국인 남자
		if (studentList[i].born[6] == '2' || studentList[i].born[6] == '4' || studentList[i].born[6] == '0')
			studentList[i].gender = 2;  //한국인 여자
		if (studentList[i].born[6] == '5' || studentList[i].born[6] == '7') studentList[i].gender = 5; 
						//외국인 남자
		if (studentList[i].born[6] == '6' || studentList[i].born[6] == '8') studentList[i].gender = 6;
						//외국인 여자
		
		// 주민등록번호 7번째 자리로 성별 출생년도 더하기
		if (studentList[i].born[6] == '1' || studentList[i].born[6] == '2' || studentList[i].born[6] == '5' 
			|| studentList[i].born[6] == '6')
			studentList[i].b_year = studentList[i].b_year + 1900;
		
		if (studentList[i].born[6] == '3' || studentList[i].born[6] == '4' || studentList[i].born[6] == '7' 
			|| studentList[i].born[6] == '8')
			studentList[i].b_year = studentList[i].b_year + 2000;
			
		if (studentList[i].born[6] == '0' || studentList[i].born[6] == '9') 
			studentList[i].b_year = studentList[i].b_year + 1800;
			
		//********** 입력된 정보 출력 **********//
		printf("\n");
		//이름 출력
		printf("%s, ", studentList[i].name);
		//생년월일 출력
		if (studentList[i].born[6] == '1' || studentList[i].born[6] == '2' || studentList[i].born[6] == '5'
		 || studentList[i].born[6] == '6') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		if (studentList[i].born[6] == '3' || studentList[i].born[6] == '4' || studentList[i].born[6] == '7' || studentList[i].born[6] == '8') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		if (studentList[i].born[6] == '0' || studentList[i].born[6] == '9') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		//윤년 여부 출력
		printf((studentList[i].b_year) % 4 == 0 && (studentList[i].b_year) % 100 != 0 || (studentList[i].b_year) % 400 == 0 ? "윤년, " : "윤년아님, ");
		//외국인 여부 출력
		printf("%s, ", studentList[i].gender == 5 || studentList[i].gender == 6 ? "외국인" : "대한민국");
		//성별 출력
		printf("%s, ", studentList[i].gender == 1 || studentList[i].gender == 5  ? "남자" : "여자");
		//학과 출력
		printf("%s\n", studentList[i].dept);
		printf("==================================================================\n");
	}
	printf("\n");
	printf("입력 받은 3명의 정보를 생년월일 기준으로 오름차순으로 출력합니다.\n\n");
	//정렬 함수 호출
	_Sort(studentList, 3);
	
	for (int i = 0; i < 3; i++) {
		//이름 출력
		printf("%s, ", studentList[i].name);
		//생년월일 출력
		if (studentList[i].born[6] == '1' || studentList[i].born[6] == '2' || studentList[i].born[6] == '5' || studentList[i].born[6] == '6') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		if (studentList[i].born[6] == '3' || studentList[i].born[6] == '4' || studentList[i].born[6] == '7' || studentList[i].born[6] == '8') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		if (studentList[i].born[6] == '0' || studentList[i].born[6] == '9') {
			printf("%d년 %d월 %d일, ", studentList[i].b_year, studentList[i].b_month, studentList[i].b_day);
		}
		
		//윤년 여부 출력
		printf((studentList[i].b_year) % 4 == 0 && (studentList[i].b_year) % 100 != 0 || (studentList[i].b_year) % 400 == 0 ? "윤년, " : "윤년아님, ");
		//외국인 여부 출력
		printf("%s, ", studentList[i].gender == 5 || studentList[i].gender == 6 ? "외국인" : "대한민국");
		//성별 출력
		printf("%s, ", studentList[i].gender == 1 || studentList[i].gender == 5 ? "남자" : "여자");
		//학과 출력
		printf("%s\n", studentList[i].dept);
		
	}
	return 0;
}

 

[ 프로그램 실행 화면 ]

 

flowchart