2011年3月13日日曜日

構造体による社員名簿管理

構造体を利用した社内スタッフの整理例です。

ID、氏名、身分、部門、給料
の情報を構造体内に格納し、管理します。

例題では、部署ごとの情報を表示し、平均給料を計算・表示を行っています。
また、IDにより情報を並べ替え、元の管理ファイルへの書き出しを行っています。

001#include <stdio.h>
002#include <stdlib.h>
003#include <string.h>
004 
005#define F_NAME "992766A3.txt"
006//-----------------------------------------------------------------
007typedef struct tabPersonal {    //staff detail
008    char name[20];    // name
009    char pos[20];    // position
010    char dep[15];    // department
011    int no;            // staff No
012    int sal;        // salary
013} personal;
014//-----------------------------------------------------------------
015int read(personal []);
016void list(personal [], int);
017int newstaff(personal [], int);
018void search(personal [], int);
019void sort(personal [], int);
020void swap_record(personal* a, personal* b);
021//-----------------------------------------------------------------
022 
023int main()
024{
025    personal record[50];
026    int i,s;
027    read(record);
028    while ( 1 ) {
029        printf("\n    M E N U\n");
030        printf(" 1. Searching by department\n");
031        printf(" 2. Sort by staff number\n");
032        printf(" 3. Exit program\n");
033        printf("\n Please enter your choice : "); scanf("%d",&i);
034        switch ( i ) {
035        case 1: s = read(record); search(record,s); break;
036        case 2: s = read(record); sort(record,s); break;
037        case 3: exit(0);
038        default: puts("\nPlease enter only 1 to 4"); }
039    }
040    return 0;
041}
042 
043int read(personal record[])
044{
045    FILE *fp; int n=0;
046    if ( (fp = fopen(F_NAME,"r")) == NULL )
047        printf("\nCan not open staff records file.\n");
048    else {
049        char buf[256];
050        while ( fgets(buf,256,fp) ) {
051            if ( buf[0] == '#' ) continue;
052            sscanf(buf,"%d%s%s%s%d",&record[n].no,record[n].name,record[n].pos,record[n].dep,&record[n].sal);
053            n ++;
054        }
055        fclose(fp);
056    }
057    return n;
058}
059 
060void search(personal record[], int s)
061{
062    int i,x=0,total=0,count=0;
063    float avg;
064    char dep[25];
065    printf("Enter the department name : "); scanf("%s",dep);
066    for ( i=0; i<s; i++ ) {
067        if ( (strcmp(record[i].dep,dep)) == 0 ){
068            printf("\n\%s\t%s",record[i].name,record[i].pos);
069            count++;
070            total += record[i].sal;
071            avg = total/count;
072            x=1;
073        }
074    }
075    if ( x == 1 ) printf("\n\nAverage salary : %.0f\n", avg);
076    else printf("\nThe department does not exist.\n");
077    getchar();
078}
079 
080void sort(personal record[], int s)
081{
082    FILE *ofp;
083    int inner,outer,j;
084    for ( outer=0; outer<s-1; outer++ ) {
085        for ( inner=outer+1; inner<s; inner++ ) {
086            if ( record[inner].no < record[outer].no ) {
087                swap_record(&record[inner],&record[outer]);
088            }
089        }
090    }
091    // ファイルの書き出し(新規書き出し)
092    ofp = fopen(F_NAME,"w");
093    fprintf(ofp,"#ID 氏名 身分 部門 給料\n");
094    for ( j=0; j<s; j++ ) {
095        fprintf(ofp,"%d %s %s %s %d\n",record[j].no,record[j].name,record[j].pos,record[j].dep,record[j].sal);
096    }
097    fclose(ofp);
098}
099 
100void swap_record(personal* a, personal* b)
101{
102    personal temp;
103    memcpy(&temp,a,sizeof(personal));
104     
105    strncpy(a->name,b->name,20);
106    strncpy(a->pos,b->pos,20);
107    strncpy(a->dep,b->dep,15);
108    a->no = b->no;
109    a->sal = b->sal;
110 
111    memcpy(b,&temp,sizeof(personal));
112}

992766A3.txt
#ID 氏名 身分 部門 給料
2169 michael CEO outside 10000
2374 arthor president company 8000
1922 mike secretary company 4000
343 ken staff room1 3000
1080 john staff room2 4000
1080 carl staff room3 2500
1278 david staff room1 3000
2231 dianne staff room2 4000
1175 alan staff room3 2500
1584 andreas staff room1 3000
1430 ben staff room2 4000
実行結果
01% cat 992766A3.txt
02#ID 氏名 身分 部門 給料
032169 michael CEO outside 10000
042374 arthor president company 8000
051922 mike secretary company 4000
06343 ken staff room1 3000
071080 john staff room2 4000
081080 carl staff room3 2500
091278 david staff room1 3000
102231 dianne staff room2 4000
111175 alan staff room3 2500
121584 andreas staff room1 3000
131430 ben staff room2 4000
14% ./list_xxx
15 
16    M E N U
17 1. Searching by department
18 2. Sort by staff number
19 3. Exit program
20 
21 Please enter your choice : 1
22Enter the department name : room1
23 
24ken     staff
25david   staff
26andreas staff
27 
28Average salary : 3000
29 
30    M E N U
31 1. Searching by department
32 2. Sort by staff number
33 3. Exit program
34 
35 Please enter your choice : 1
36Enter the department name : extra
37 
38The department does not exist.
39 
40    M E N U
41 1. Searching by department
42 2. Sort by staff number
43 3. Exit program
44 
45 Please enter your choice : 2
46 
47    M E N U
48 1. Searching by department
49 2. Sort by staff number
50 3. Exit program
51 
52 Please enter your choice : 3
53% cat 992766A3.txt
54#ID 氏名 身分 部門 給料
55343 ken staff room1 3000
561080 john staff room2 4000
571080 carl staff room3 2500
581175 alan staff room3 2500
591278 david staff room1 3000
601430 ben staff room2 4000
611584 andreas staff room1 3000
621922 mike secretary company 4000
632169 michael CEO outside 10000
642231 dianne staff room2 4000
652374 arthor president company 8000
66%

0 件のコメント:

コメントを投稿