?? 主執行文件.txt
字號:
/*開始搜索*/
for(j=0;j<i;j++){
if(!strcmp(sp[j].name,search_name))
if(mode){
display(j,sp);
return j;
}
else return j;
}
printf("not find this worker called %s\n",search_name);
return -1;
}
/*修改一個工人的資料的函數*/
void modify(int i,struct WORKER *mp){
char mod_name[20];
char temp[20];
int mi;
enum {false,true}status;
status=false;
printf("Enter the worker's name to modify:");
scanf("%s",mod_name);
/*調用search函數(在上面已經聲明了的),搜索到的話就返回工人的id,沒有的話就返回-1*/
if((mi=search(i,mp,1,mod_name))==-1)return;
/*下面的處理跟添加一個工人的資料的處理一模一樣,比較繁*/
printf("\nNow modify %s's profile...\n",mod_name);
while(status==false){
printf("Enter the name of the worker:");
scanf("%s",mp[mi].name);
if(strlen(mp[mi].name)==0 || strlen(mp[mi].name)>20){
status=false;puts("input error,name cannot over 8 character or less than 0 character!");
continue;
}
else
status=true;
printf("Enter the gender of worker:(1=>female,0=>male)");
scanf("%d",&mp[mi].sex);
if(mp[mi].sex!=0 && mp[mi].sex!=1){
status=false;puts("only 0 or 1 can be accepted!");
continue;
}
else
status=true;
printf("Enter the birthday of worker:\n");
printf("\tYear:");
scanf("%4d",&mp[mi].birthday.year);
if(mp[mi].birthday.year>9999 || mp[mi].birthday.year<0){
status=false;puts("input error,the year of birthday cannot more than 9999 or less than 0!");
continue;
}
else
status=true;
printf("\tMonth:");
scanf("%2d",&mp[mi].birthday.month);
if(mp[mi].birthday.month>12 || mp[mi].birthday.month<1){
status=false;puts("input error,the month of birthday cannot be more than 12 or less than 1");
continue;
}
else
status=true;
printf("\tDay:");
scanf("%2d",&mp[mi].birthday.day);
if(mp[mi].birthday.day>31 || mp[mi].birthday.day<1){
status=false;puts("input error,the year of birthday cannot be more than 31 or less than 1!");
continue;
}
else
status=true;
printf("Enter the workday of worker:\n");
printf("\tYear:");
scanf("%4d",&mp[mi].workday.year);
if(mp[mi].workday.year>9999 || mp[mi].workday.year<0){
status=false;puts("input error,the year of workday cannot more than 9999 or less than 0!");
continue;
}
else
status=true;
printf("\tMonth:");
scanf("%2d",&mp[mi].workday.month);
if(mp[mi].workday.month>12 || mp[mi].workday.month<1){
status=false;puts("input error,the month of workday cannot be more than 12 or less than 1");
continue;
}
else
status=true;
printf("\tDay:");
scanf("%2d",&mp[mi].workday.day);
if(mp[mi].workday.day>31 || mp[mi].workday.day<1){
status=false;puts("input error,the year of workday cannot be more than 31 or less than 1!");
continue;
}
else
status=true;
printf("Enter the record of formal schooling of worker:(0=>element school,1=>middle school,2=>high school,3=>university,4=>other)");
scanf("%d",&mp[mi].rofs);
if(mp[mi].rofs!=0 && mp[mi].rofs!=1 &&mp[mi].rofs!=2 &&mp[mi].rofs!=3 &&mp[mi].rofs!=4){
status=false;puts("only digit can be accepted!you can only enter 0,1,2,3,4");
continue;
}
else
status=true;
printf("Enter the position of worker:");
scanf("%s",mp[mi].position);
if(strlen(mp[mi].position)==0 || strlen(mp[mi].position)>20){
status=false;puts("input error,position cannot over 20 character or less than 0 character!");
continue;
}else
status=true;
printf("Enter the address of worker:\n");
printf("\tProvince:");
scanf("%s",mp[mi].addr.province);
if(strlen(mp[mi].addr.province)==0 || strlen(mp[mi].addr.province)>20){
status=false;puts("input error,province cannot over 20 character or less than 0 character!");
continue;
}
else
status=true;
printf("\tCity:");
scanf("%s",mp[mi].addr.city);
if(strlen(mp[mi].addr.city)==0 || strlen(mp[mi].addr.city)>20){
status=false;puts("input error,city cannot over 20 character or less than 0 character!");
continue;
}
else
status=true;
printf("\tStreet:");
scanf("%s",mp[mi].addr.street);
if(strlen(mp[mi].addr.street)==0 || strlen(mp[mi].addr.street)>30){
status=false;puts("input error,street cannot over 30 character or less than 0 character!");
continue;
}
else
status=true;
printf("Enter the telephone number of worker:(area code/telephone code)");
scanf("%s",mp[mi].tel);
if(strlen(mp[mi].tel)==0 || strlen(mp[mi].tel)>13){
status=false;puts("input error,telephone number cannot over 13 character or less than 0 character!");
continue;
}
else
status=true;
}
}
/*從數據庫中刪除一個工人的資料*/
void del(int *i,struct WORKER *dp){
char del_name[20],choice;
int di,dj,mi;
printf("Enter the worker's name to delete:");scanf("%s",del_name);
/*調用search函數(在上面已經聲明了的),搜索到的話就返回工人的id,沒有的話就返回-1*/
if((mi=search(*i,dp,1,del_name))==-1)return;
printf("Are you sure to delete this profile?(y/n)");
choice=getch();
if(choice=='n')
return;
for(di=dj=0;di<*i;di++)
if(di!=mi)
dp[dj++]=dp[di];
--*i;
}
/*將內存中的工人資料保存一遍,也就是將被你操作后的工人資料都保存到數據庫中
如果你不保存的話,所有的操作都將白費(我說的好象是廢話哦)
*/
void save(int i,struct WORKER *sp){
int si;
fp=fopen("worker.txt","w");
/*錯誤處理*/
if(!fp){
printf("fail to save!\nMaybe the database file named worker.txt cannot be opened!Please check this file,it cannot be reak only!");
return;
}
/*保存操作的循環*/
for(si=0;si<i;si++)
fwrite(&sp[si],sizeof(struct WORKER),1,fp);
/*檢測是否在保存的時候遇到錯誤,如果錯誤的話提示再保存一遍*/
if(ferror(fp)){
printf("save error!you try again!");
clearerr(fp);
fclose(fp);
return;
}else{
puts("save successfully!");
fclose(fp);
}
}
/*排序,用的是冒泡排序法*/
void my_sort(int i,struct WORKER *sortp)
{
int sortj,sorti;
char schoice;
struct WORKER sorttemp;
for(sorti=0;sorti<i;sorti++){
for(sortj=sorti;sortj<i;sortj++){
if(strcmp(sortp[sorti].name,sortp[sortj].name)>0){
sorttemp=sortp[sorti];
sortp[sorti]=sortp[sortj];
sortp[sortj]=sorttemp;
}
}
}
/*排序完成后提示是否再次查看列表*/
printf("Sort successfully!Do you want to list all the profiles?(y/n)");
schoice=getch();
if(schoice=='y')list(i,sortp);
}
/*清楚所有數據的函數,就是將數據庫全部清空*/
void cleardb(int *i,struct WORKER *clrp){
char clrc;
printf("Are you sure to clear all the profiles?it will delete all the data(y/n)");
clrc=getch();
if(clrc=='n') return;
/*當以只寫模式打開文件的時候,指針會指向文件的頭部,并且將文件內數據全部清空*/
fp=fopen("worker.txt","w");
fclose(fp);
data_load(i,clrp);
printf("\n\n\tClear successfully!");
getch();
}
/*下面就是主函數了*/
void main(void)
{
struct WORKER profile[maxworker];
int selection;
int GD=DETECT,GM;
char c;
int i=0,k=1;
initgraph(&GD,&GM,"e:\\tc\\bgi");
setbkcolor(LIGHTBLUE);
if ((al=fopen("hzk16","rb"))==NULL)
{ printf("Can't open haz16,Please add it");
getch(); closegraph(); exit(0);
}
rectangle(20,20,620,460);
rectangle(10,10,630,470);
hz(200,80,40,2,10,"人事管理系統");/*數字依次為橫坐標縱坐標字體間距字體大小字體顏色*/
hz(100,150,25,1,11,"長沙理工大學計算機系軟件工程課程設計");
hz(100,200,25,1,11,"設計人員:安國兵姚金伶揚寒賀曉玲肖紅吳涵");
hz(100,250,25,1,11,"按任意鍵進入");
getch();
closegraph();
close(al);
clrscr();
initgraph(&GD,&GM,"e:\\tc\\bgi");
setbkcolor(LIGHTBLUE);
if ((al=fopen("hzk16","rb"))==NULL)
{ printf("Can't open haz16,Please add it");
getch(); closegraph(); exit(0);
}
data_load(&i,profile);
do
{ if(k!=1)
{printf("\nPlease press any key to return to the main meun");
getch();}
k++;
clrscr();
initgraph(&GD,&GM,"e:\\tc\\bgi");
setbkcolor(LIGHTBLUE);
hz(200,20,40,2,10,"人事管理系統");
printf("\n\n\n\n\n");
printf("\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t\t*1.Add one worker to the file\n");
printf("\t\t\t*2.Delete one worker from the file\n");
printf("\t\t\t*3.Search one worker from the file\n");
printf("\t\t\t*4.Modify one worker's information\n");
printf("\t\t\t*5.List all the profiles\n");
printf("\t\t\t*6.Save new data\n");
printf("\t\t\t*7.Sort the profiles\n");
printf("\t\t\t*8.Reload data from database file\n");
printf("\t\t\t*9.Delete all data\n");
printf("\t\t\t*0.exit without save\n\n\n");
printf("\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t\tPlease input your selection number:");
scanf("%d",&selection);
switch(selection)
{
case 1:
add(&i,profile);
printf("\n\nin function main,i=%d",i);
break;
case 2:
del(&i,profile);
break;
case 3:
search(i,profile,1,"");
break;
case 4:
modify(i,profile);
break;
case 5:
list(i,profile);
break;
case 6:
save(i,profile);
break;
case 7:
my_sort(i,profile);
break;
case 8:
data_load(&i,profile);
break;
case 9:
cleardb(&i,profile);
break;
case 0:
printf("Are you sure to exit without save?New data that you enter will be lost!(y/n)");
c=getch();
if(c=='y'||'Y')exit(0);
break;
default:
printf("please enter the correct selection!");
return 0;
}
}while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -