자기참조 구조체 예제

왜 이 프로그램을 만들었는지 묻지 말자.

이 프로그램은 public domain 라이센스이다. 출처를 밝히지 않고도 누구나 자유롭게 이용할 수 있고, 상업적 목적으로 사용해도 무방하다.

#include

#include

#include

struct person{

char name[30];

char tel[14];

int hour;

int payment;

struct person *next;

};

main()

{

FILE *data;

struct person dummy;

struct person *start=&dummy;

struct person *wkdata;

struct person *wp;

struct person *best;

char name[30]=” “;

char buf[20];

char tel_input[14]=” “;

int hour_input=0;

int payment_input=0;

int i=0;

int totalpay=0;

start=&dummy;

start->next=NULL;

printf(“put your data in\n”);

while(1){

printf(“name:”);

gets(name);

if(strcmp(name,””)==0) break;

printf(“phone:”);

gets(tel_input);

printf(“work hour:”);

gets(buf);

hour_input=atoi(buf);

printf(“pay per hour:”);

gets(buf);

payment_input=atoi(buf);

wkdata=(struct person *)malloc(sizeof(struct person));

if(wkdata==NULL){

printf(“Memory allocation cannot be done\n”);


exit(1);

}

strcpy(wkdata->name,name);

strcpy(wkdata->tel,tel_input);

wkdata->hour=hour_input;

wkdata->payment=payment_input;

for(wp=start; wp->next != NULL; wp=wp->next){

if(payment_input*hour_input>wp


->next->payment*wp->next->hour

){

wkdata->next = wp->next;

wp->next = wkdata;

break;

}

}

if(wp->next==NULL){

wkdata->next=NULL;

wp->next=wkdata;

}

}

data=fopen(”

data

.txt”,”w”);

fprintf(data,”Name Phone Working

Hour pay/hour Total

Pay\n=========================

==============================

====\n”);

fclose(data);

for(wp=start->next;wp!=NULL;wp

=wp->next){

data=fopen(”

data

.txt”,”a+”);

fprintf(data,”%-30s%-14s\t%12d

\t%8d\t%8d\n”,

wp->name,wp->tel,wp->hour,wp-

>payment,wp->hour*wp->payment)

;

fclose(data);

i++;

totalpay+=wp->hour*wp->payment


;

}

data=fopen(”

data

.txt”,”a”);

fprintf(data,”================

==============================

=============\n”);

fclose(data);

best=start;

for(wp=start->next;wp!=NULL;wp

=wp->next){

if(wp->payment>best->payment)

best=wp;

}

printf(“\nTotal workers are %d\n”, i);

printf(“The highest paid person of %dwon is %s

.\n”,start->next->payment

,start->next->name);

printf(“Sum of total payment is %d.\n”,totalpay);

}












물론, 어디서 많이 보던 프로그램이다 싶은 사람들은 특정 사람들이겠지만…;

코멘트

댓글 남기기

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.