#include <stdio.h> #include <stdlib.h>
#define MAX 100 #define TRUE 1 #define FALSE 0
struct record { char fname[15+1]; // 성이고 +1은 NULL값을 위한 자리 char lname[20+1]; // 이름 char phone[11+1]; // 전화번호 long income; //수입 int month; int day; int year; //각각 생일의 월, 일 년도 };
struct record list[MAX]; //실제 구조체를 선언 int last_entry=0; //항목의 전체 수 void main(void); void get_data(void); void display_report(void); int continue_func(void); void clear(void);
void main(){ int cont = TRUE; int ch;
while(cont == TRUE) { printf("\n\n MENU\n"); printf("========\n"); printf("\n1. 이름 입력"); printf("\n2. 보고서 출력"); printf("\n3. 종료"); printf("\n\n선택하세요 ==>"); ch = getchar(); fflush(stdin); //추가 문자 제거
switch(ch) { case'1':get_data(); break; case'2':display_report(); break; case'3':printf("\n\n이 프로그램을 이용해주셔서 감사합니다.\n"); cont = FALSE; break; default: printf("유효하지 않은 항목입니다. 1~3항목 중에서만 선택해주세요!"); break; } } }
/* get_data의 목적은 사용자에게서 데이터를 구합니다. -- 100명의 자료를 입력하거나 사용자가 계속하지 않도록 선택할 때 까지 데이터를 구합니다. -- 복귀 값 : 없음 -- 사용자가 분명하지 않은 경우에 생일에 0/0/0이 자동으로 입력됩니다. -- 각 월별로 모두 31일을 허용합니다. */
void get_data(void){ int cont; for (cont = TRUE; last_entry<MAX&&cont==TRUE;last_entry++) { printf("%d번째 사용자에 대한 정보를 입력하는 항목입니다.",last_entry+1);
printf("\n\n성을 입력해주세요 : "); gets(list[last_entry].fname);
printf("\n\n이름을 입력해주세요 : "); gets(list[last_entry].lname);
printf("전화번호를 -를 포함하여 입력해주세요 : "); gets(list[last_entry].phone);
printf("\n연봉을 입력해주세요"); scanf("%ld",&list[last_entry].income); printf("\n생일을 입력해주세요");
do{ printf("생일의 월 : "); scanf("%d",&list[last_entry].month); }while(list[last_entry].month < 0 || list[last_entry].month > 12);
do{ printf("생일의 일 : "); scanf("%d",&list[last_entry].day); }while(list[last_entry].day < 0 || list[last_entry].day > 31); do{ printf("생일의 년도 : "); scanf("%d",&list[last_entry].year); }while(list[last_entry].year != 0 && list[last_entry].year <1800 || list[last_entry].year > 1997);
cont = continue_func(); if(last_entry == MAX) printf("\n\n이미 전부 입력하셨습니다!\n"); } } /* display_report() -- 화면에 보고서를 출력합니다. -- 복귀값 : 없음 -- 보고서를 인쇄할 땐 stdout을 stdprn으로 변경하면 됩니다. */
void display_report() { long month_total=0, grand_total=0; // 합계 int x, y;
fprintf(stdout, "\n\n"); fprintf(stdout,"\nreport"); fprintf(stdout,"\n========");
for(x=0; x<=12; x++) // 월별로 반복 { month_total=0; for(y=0; y < last_entry; y++) { if(list[y].month==x) { fprintf(stdout,"\n\t%s %s %s %ld",list[y].fname, list[y].lname, list[y].phone, list[y].income); } } fprintf(stdout, "\n%d월 총 수입은 : %ld",x,month_total); grand_total += month_total; } fprintf(stdout,"\n\n보고서 총 합 :"); fprintf(stdout,"\n총 수입 : %ld", grand_total); fprintf(stdout,"\n평균 수입 : %ld", grand_total/last_entry); fprintf(stdout,"\n\n -- 보고서 끝 --"); }
/*continue_func() --사용자에게 계속할 것인지 묻습니다. --TRUE 계속할 경우, FALSE 마칠 경우 */
int continue_func(void){ int ch; printf("계속하시겠습니까? (Y/N) :"); fflush(stdin); ch = getchar(); while(ch!='n' && ch!='N'&& ch!='y' && ch!='Y') { printf("유효하지 않은 선택입니다!"); printf("끝내시려면 N, 계속하시려면 Y를 입력해주세요 :"); fflush(stdin); ch=getchar(); } clear(); if(ch=='n'||ch=='N') return(FALSE); else return(TRUE); }
void clear(void){ char junk[80]; gets(junk); } |