構造体を利用した社内スタッフの整理例です。
ID、氏名、身分、部門、給料
の情報を構造体内に格納し、管理します。
例題では、部署ごとの情報を表示し、平均給料を計算・表示を行っています。
また、IDにより情報を並べ替え、元の管理ファイルへの書き出しを行っています。
005 | #define F_NAME "992766A3.txt" |
007 | typedef struct tabPersonal { |
015 | int read(personal []); |
016 | void list(personal [], int ); |
017 | int newstaff(personal [], int ); |
018 | void search(personal [], int ); |
019 | void sort(personal [], int ); |
020 | void swap_record(personal* a, personal* b); |
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); |
035 | case 1: s = read(record); search(record,s); break ; |
036 | case 2: s = read(record); sort(record,s); break ; |
038 | default : puts ( "\nPlease enter only 1 to 4" ); } |
043 | int read(personal record[]) |
046 | if ( (fp = fopen (F_NAME, "r" )) == NULL ) |
047 | printf ( "\nCan not open staff records file.\n" ); |
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); |
060 | void search(personal record[], int s) |
062 | int i,x=0,total=0,count=0; |
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); |
070 | total += record[i].sal; |
075 | if ( x == 1 ) printf ( "\n\nAverage salary : %.0f\n" , avg); |
076 | else printf ( "\nThe department does not exist.\n" ); |
080 | void sort(personal record[], int s) |
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]); |
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); |
100 | void swap_record(personal* a, personal* b) |
103 | memcpy (&temp,a, sizeof (personal)); |
105 | strncpy (a->name,b->name,20); |
106 | strncpy (a->pos,b->pos,20); |
107 | strncpy (a->dep,b->dep,15); |
111 | memcpy (b,&temp, sizeof (personal)); |
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
実行結果
03 | 2169 michael CEO outside 10000 |
04 | 2374 arthor president company 8000 |
05 | 1922 mike secretary company 4000 |
06 | 343 ken staff room1 3000 |
07 | 1080 john staff room2 4000 |
08 | 1080 carl staff room3 2500 |
09 | 1278 david staff room1 3000 |
10 | 2231 dianne staff room2 4000 |
11 | 1175 alan staff room3 2500 |
12 | 1584 andreas staff room1 3000 |
13 | 1430 ben staff room2 4000 |
17 | 1. Searching by department |
18 | 2. Sort by staff number |
21 | Please enter your choice : 1 |
22 | Enter the department name : room1 |
31 | 1. Searching by department |
32 | 2. Sort by staff number |
35 | Please enter your choice : 1 |
36 | Enter the department name : extra |
38 | The department does not exist. |
41 | 1. Searching by department |
42 | 2. Sort by staff number |
45 | Please enter your choice : 2 |
48 | 1. Searching by department |
49 | 2. Sort by staff number |
52 | Please enter your choice : 3 |
55 | 343 ken staff room1 3000 |
56 | 1080 john staff room2 4000 |
57 | 1080 carl staff room3 2500 |
58 | 1175 alan staff room3 2500 |
59 | 1278 david staff room1 3000 |
60 | 1430 ben staff room2 4000 |
61 | 1584 andreas staff room1 3000 |
62 | 1922 mike secretary company 4000 |
63 | 2169 michael CEO outside 10000 |
64 | 2231 dianne staff room2 4000 |
65 | 2374 arthor president company 8000 |
0 件のコメント:
コメントを投稿