項目要求
以菜單方式工作管理每個職工的個人信息。
(1)總共有職工若干名,每個職工是一個記錄,包括的信息有: 職工號、姓名、性別、出生年月、學(xué)歷、職務(wù)、部門、工資、住址、電話等等(可以根據(jù)自己的需求添加),并且可以對職工信息進(jìn)行錄入、刪除、修改、瀏覽等操作。
(2)可以按姓名進(jìn)行查詢。
(3)可以修改職工的信息。
(4)用一個文檔來存儲職工的信息,并實(shí)現(xiàn)數(shù)據(jù)的讀取。
單個職工的頭文件:staff.h
#ifndef STAFF_H_INCLUDED
#define STAFF_H_INCLUDED
?
//結(jié)構(gòu)體創(chuàng)建
structstaff
{
charID[10];
charname[10];
charsex[10];
intpay;
intreward;
intfactpay;
};
?
//自定義結(jié)構(gòu)體
typedefstructstaff staff;
//單個職工信息創(chuàng)建
staff Createstaff();
//單個職工信息輸出
voidDisplaystaff(staff staff);
//修改職工信息
voidupdatestaff(staff *Staff);
#endif // STAFF_H_INCLUDED
單個職工的cpp文件:staff.cpp
#include
#include
#include "staff.h"
?
staff Createstaff()
{
staff staff;
printf("-----------ID-----------\n");
scanf("%s", staff.ID);
printf("-----------name-----------\n");
scanf("%s", staff.name);
printf("-----------sex-----------\n");
scanf("%s", staff.sex);
printf("-----------pay-----------\n");
scanf("%d", &staff.pay);
printf("-----------reward-----------\n");
scanf("%d", &staff.reward);
staff.factpay = staff.pay + staff.reward;
printf("\n");
returnstaff;
}
?
voidDisplaystaff(staff staff)
{
printf("s", staff.ID);
printf("s", staff.name);
printf("s", staff.sex);
printf("d", staff.pay);
printf("d", staff.reward);
printf("d", staff.factpay);
printf("\n");
}
?
?
voidupdatestaff(staff *Staff)
{
printf("-----請顯示要修改的數(shù)據(jù)--------\n");
Displaystaff(*Staff);
?
printf("-------請輸入要修改的數(shù)據(jù)---------");
printf("-----------pay-----------\n");
scanf("%d", &Staff->pay);
printf("-----------reward-----------\n");
scanf("%d", &Staff->reward);
Staff->factpay = Staff->pay + Staff->reward;
printf("\n");
?
}
2.鏈表的創(chuàng)建
鏈表的頭文件:.h
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include "staff.h"
//鏈表結(jié)點(diǎn)創(chuàng)建
structNode
{
structstaff Staff;
structNode *next;
};
//自定義結(jié)點(diǎn)
?
typedefstructNode node;
typedefstructNode *linklist;
//創(chuàng)建鏈表
node *Createlinklist();
//輸出鏈表中的數(shù)據(jù)
voidDisplaylinklist(node *head);
//按職工號查找職工
node *searchnode(node *head, charID[]);
//按姓名查找職工
voidsearchnodebyname(node *head, charname[]);
//刪除職工
voiddelenode(linklist head, charID[]);
//插入職工
voidinsertnode(linklist head, staff Staff);
//鏈表銷毀
voiddistroylinklist(linklist head);
#endif // LINKLIST_H_INCLUDED
鏈表創(chuàng)建的源程序
linklist.cpp
#include
#include
#include
#include "staff.h"
#include "linklist.h"
node *Createlinklist()
{
node *head, *p;
head = (node *)malloc(sizeof(node));
head->next = NULL;
staff a[100] = {{"11111", "mmm", "f", 12000, 2000, 14000},
{"22222", "aaa", "m", 13000, 3000, 16000},
{"33333", "sss", "f", 15000, 3000, 18000},
{"44444", "fff", "m", 17000, 8000, 25000},
{"55555", "ggg", "f", 20000, 5000, 25000}};
for(inti = 0; i<5; i++)
{
p = (node *)malloc(sizeof(node));
p->Staff = a[i];
p->next = head->next;
head->next = p;
}
returnhead;
}
?
voidDisplaylinklist(node *head)
{
linklist p;
p = head->next;
while(p!=NULL)
{
Displaystaff(p->Staff);
p = p->next;
}
}
node *searchnode(node *head, charID[])
{
linklist p;
p = head;
while(p!=NULL&&strcmp(p->next->Staff.ID, ID)!=0)
{
p = p->next;
}
returnp->next;
}
?
voidsearchnodebyname(node *head, charname[])
{
linklist p;
p = head;
while((p!=NULL)&&(strcmp((p->next)->Staff.name, name)!=0))
{
p = p->next;
}
printf("-----′?è??a---------\n");
printf("%s", p->next->Staff.name);
printf("\n");
}
?
?
voiddelenode(linklist head, charID[])
{
linklist p;
p = head;
while(p->next&&(strcmp(p->next->Staff.ID, ID)!=0))
{
p = p->next;
}
if(p->next)
{
p->next = p->next->next;
}
else
{
printf("=====NO FOUND========\n");
}
}
voidinsertnode(linklist head, staff Staff)
{
linklist p;
p = (node *)malloc(sizeof(node));
p->Staff = Staff;
p->next = head->next;
head->next = p;
}
?
voiddistroylinklist(linklist head)
{
linklist p;
p = head;
while(p!=NULL)
{
p = p->next;
free(p);
}
}
3.文件存盤
file.h
#ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED
#include "linklist.h"
#include "staff.h"
//職工信息存盤
voidsaveinformation(linklist head );
//職工信息加載
voidloadinformation(linklist head );
?
#endif // FILE_H_INCLUDED
file.cpp
#include
#include
#include
#include "file.h"
#include "linklist.h"
#include "staff.h"
?
voidsaveinformation(linklist h )
{
FILE*fp;
linklist p;
if( (fp = fopen("stu.txt","w") ) == NULL)
{
printf("Failure to open stu.txt!\n");
exit(0);
}
for( p = h->next; p; p=p->next )
{
fwrite( &(p->Staff), sizeof(node), 1, fp);
}
fclose(fp);
}
?
voidloadinformation( linklist h )
{
FILE*fp;
staff nodeBuffer;
if((fp = fopen("stu.txt","r")) == NULL)
{
printf("\n\t數(shù)據(jù)文件丟失或為首次運(yùn)行, 將加載測試數(shù)據(jù)\n");
return;
}
while( fread(&nodeBuffer, sizeof(node), 1, fp)!=0 )
{
insertnode(h, nodeBuffer);
}
}
4.主函數(shù):.cpp
mainmeun.cpp
#include
#include
#include "linklist.h"
#include "staff.h"
#include "file.h"
voidmainmeun(linklist head);
voidsearchmenu(linklist head);
?
int main(void)
{
linklist head=NULL;
//int n;
//printf("------請輸入你要存的數(shù)據(jù)----------\n");
//scanf("%d", &n);
head = Createlinklist();
system("cls");
//Displaylinklist(head);
mainmeun(head);
printf("\n\n");
//loadinformation(head);
//saveinformation(head);
return0;
}
voidmainmeun(linklist head)
{
linklist p;
charID[10];
//char name[10];
staff Staff;
intselection;
intflag = 1;
do
{
printf("=================職工管理系統(tǒng)===================\n");
printf("==========1.鏈表輸出=====2.數(shù)據(jù)查詢=====\n");
printf("=======3.數(shù)據(jù)刪除===4.數(shù)據(jù)修改=====5.添加數(shù)據(jù)======\n");
printf("=======6.鏈表銷毀===7.信息存盤=====8.放棄存盤=====\n");
printf("==================================================\n");
printf("======請選擇功能(1~8):");
scanf("%d", &selection);
switch(selection)
{
case1:
Displaylinklist(head);
break;
case2:
searchmenu(head);
break;
case3:
printf("=========請輸入工號==========\n");
scanf("%s", ID);
delenode(head, ID);
break;
case4:
printf("=========請輸入工號==========\n");
scanf("%s", ID);
p = searchnode(head, ID);
updatestaff(&(p->Staff));
break;
case5:
printf("========添加數(shù)據(jù)=========");
Staff = Createstaff();
insertnode(head, Staff);
break;
case6:
distroylinklist(head);
break;
case7:
loadinformation(head);
saveinformation(head);
break;
case8:
flag = 0;
break;
}
}while(flag == 1);
printf("========BYE=====BYE======");
}
voidsearchmenu(linklist head)
{
linklist p;
intflag = 1;
charID[10];
charname[10];
do
{
printf("=========查找菜單===========\n");
printf("===1.ID======2.name====3.退出====\n");
printf("=================================\n");
intselection;
printf("==請選擇功能(1~3):");
scanf("%d", &selection);
switch(selection)
{
case1:
printf("=====請輸入ID=======\n");
scanf("%s", ID);
p = searchnode(head, ID);
Displaystaff(p->Staff);
break;
case2:
printf("=====請輸入name======\n");
scanf("%s", name);
searchnodebyname(head, name);
break;
case3:
flag = 0;
break;
}
system("pause");
system("cls");
}while(flag == 1);
}
需要源碼可以自取!
此外操作系統(tǒng)課程設(shè)計報告-文件系統(tǒng) 源碼,我也給大家分享我收集的資源,從最零基礎(chǔ)開始的教程到C語言C++項目案例操作系統(tǒng)課程設(shè)計報告-文件系統(tǒng) 源碼,幫助大家在學(xué)習(xí)C語言的道路上披荊斬棘!