#define SIZE 50
/* 定義結構體 */
struct date
{int month;
int day;
int year;
};
typedef struct employee
{char name[50] ,num[10],sex,edu[10],duty[15];
int age,income;
struct employee *next;
}empl[SIZE];
/* 逐項輸出鏈表中的內容 */
void view (struct employee* head)
{
struct employee * p;
p = head;
while (p != NULL)
{
printf("%-10s%-15s%3c%-10s%-8s%6d%9d\n", p->name, p->num,p->sex, p->edu,p->duty, p->age,p->income);
p = p->next;
}
}
/* 定義保存函數 */
void save ()
{FILE *fp;
int i;
if ((fp=fopen("emp_list","wb"))==NULL)
{printf("cannot open file\n");
return;
}
for (i=0;iname, name))
p = p->next;
return p;
}
/* 通過工號查找職工 */
struct employee * Search2 (struct employee* head, char * num)
{
struct employee * p = head;
while (p && strcmp(p->num, num))
p = p->next;
return p;
}
/* 刪除職工信息 */
void Delete (struct employee* head, char * i)
{
struct employee * p = head, *m, *q;
while (p && strcmp(p->num, i) != 0)
{
q = p;
p = p->next;
}
if ((q != NULL) && (q->next != NULL) && head != NULL)
{
m = q->next;
q->next = m->next;
free(m);
printf("刪除成功\n");
} else printf("職工不存在!\n");
}
/* 添加一個職工的信息 */
void AddHead (struct employee* head, char name [50], char num [10], char sex,char edu[10],char duty[15], int age, int income) /*增加職工信息*/
{
struct employee * p, *q = head;
while (q->next != NULL)
q = q->next;
p = (struct employee* )malloc(sizeof(employee));
strcpy(p->name, name);
strcpy(p->num, num);
p->sex = sex;
strcpy(p->edu, edu);
strcpy(p->duty, duty);
p->age = age;
p->income = income;
p->next = NULL;
q->next = p;
}
void main (void)
{
int i, f, g;
struct employee * head, *s;
char a [50], b [50], t, e [15], h [15],x[8],d[20], k [15];
system("cls");
/* 為首地址分配空間 */
head=(struct employee* )malloc(sizeof(struct employee));
head->next=NULL;
/* 添加預置信息 */
AddHead(head, "張天", "0610312100", 'M', "高中", "保安" ,20,2600);
AddHead(head, "李行", "0610312101", 'M', "本科", "經理" ,36,5000);
AddHead(head, "周若", "0610312102", 'W', "本科", "會計" ,20,1600);
AddHead(head, "王昭", "0610312103", 'W', "碩士","工程師",27,6000);
AddHead(head, "劉系", "0610312104", 'M', "碩士","工程師",27,6000);
save();
for ( ; ; )
{
printf("--------------------------------------------------------\n");
printf("姓名 工號 性別 學歷 職務 年齡 工資\n");
view(head->next);
printf("--------------------------------------------------------\n");
printf("1.添加職工信息\n");
printf("2.刪除職工信息\n");
printf("3.根據姓名查找職工\n");
printf("4.根據工號查找職工\n\n");
printf("請作出你的選擇:");
scanf("%d", &i);
switch (i)
{
case 1:
printf("請輸入職工的信息:");
printf("\n姓名:");
scanf("%s", a);
printf("工號:");
scanf("%s", e);
printf("性別:");
getchar();
scanf("%c", &t);
printf("\n學歷:");
scanf("%s", x);
printf("職務:");
scanf("%s", d);
printf("年齡:");
scanf("%d", &f);
printf("工資:");
scanf("%d", &g);
AddHead(head, a, e, t, x,d,f, g);
system("cls");
break;
case 2:
printf("\n工號:");
scanf("%s", h);
Delete(head, h);
system("cls");
break;
case 3:
printf("請輸入職工的姓名:");
scanf("%s", b);
s = Search1(head, b);
system("cls");
printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
break;
case 4:
printf("請輸入職工的工號:");
scanf("%s", k);
s = Search2(head, k);
system("cls");
printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
break;
}
}
}
要求:
職工信息包括職工號、姓名、性別、出生年月、學歷、職務、工資、住址、電話等(職工號不重復)。試設計一職工信息管理系統,使之能提供以下功能:
職工信息錄入(創建)功能(職工信息用文件保存)
職工信息瀏覽功能
查詢或排序功能:(至少一種查詢方式)
按工資查詢
按學歷查詢等
職工信息添加、刪除、修改功能