论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

简单的职工治理系统(V1.1修改版)内含中文注释

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:44:08

/*------------------------------------------------------------------------
 * filename - workmanagerment.c
 *
 * written by
 *        沈豪杰(01213229)&&王鹏(01213208)
 *-----------------------------------------------------------------------*/

/*[]---------------------------------------------------[]*/
/*|                         |*/
/*| Turbo C Run workmanagerment.c- Version 1.1      |*/
/*|                         |*/
/*|                         |*/
/*| Copyright (c) 2003 6 28 by shj and wp       |*/
/*| All Rights Reserved.                |*/
/*|                         |*/
/*[]---------------------------------------------------[]*/
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#define maxworker 100

FILE *fp;  /*定义全局变量fp,指向数据库文件 worker.txt*/

struct date{      /*日期的结构,用于存储工人的生日,工作日期等*/
  int year;
  int month;
  int day;
};
struct address {  /*地址的结构, 用于存储工人的地址*/
  char province[10];
  char city[10];
  char street[20];
};
struct WORKER {       /*工人的资料总结构*/
 char name[20];
 enum {male,female}sex;
 struct date birthday;
 enum {elesch,middle,high,university,other}rofs;
 struct date workday;
 int workeri;
 char position[20];
 struct address addr;
 int workerj;
 char tel[13];
 };
/*程序的初始化函数,用于从数据库中读出现存的资料到内存中*/
data_load(int *i,struct WORKER *p){
 char c;
 long int k;
 fp=fopen("worker.txt","r");
 if(!fp){fopen("worker.txt","w+");*i=0;}
 else{
 for((*i)=0;(*i)<maxworker;(*i)++){
  if(feof(fp)) return;
  c=fgetc(fp);
  if(c==EOF) return;
  fflush(stdin);
  k=*i;
  fseek(fp,k*sizeof(struct WORKER),SEEK_SET);
  fread(&p[*i],sizeof(struct WORKER),1,fp);
  }
 }
fclose(fp);
}
/*添加一个工人的资料*/

void add(int *i,struct WORKER *profile){
 char add_name[20],add_position[20],add_addr_province[20],add_addr_city[20],add_addr_street[30],add_tel[13];
 int add_birthday_year,add_birthday_month,add_birthday_day,add_workday_year,add_workday_month,add_workday_day;
 int add_sex,add_rofs;
 enum {false,true}status;
 status=false;
 printf("\n\tNow add NO. %d worker's profile\n",*i+1);

 /*以下是处理输入的循环,可能有些繁杂,希望高手给指点一下
  其中status是个flag,用于指定是否有输错的地方,假如有的话,
  马上跳的最初的提示。
 */

 while(status==false){
  printf("Enter the name of the worker:");
  scanf("%s",add_name);
     if(strlen(add_name)==0 || strlen(add_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",&add_sex);
     if(add_sex!=0 && add_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",&add_birthday_year);
     if(add_birthday_year>9999 || add_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",&add_birthday_month);
     if(add_birthday_month>12 || add_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",&add_birthday_day);
     if(add_birthday_day>31 || add_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",&add_workday_year);
     if(add_workday_year>9999 || add_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",&add_workday_month);
     if(add_workday_month>12 || add_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",&add_workday_day);
     if(add_workday_day>31 || add_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",&add_rofs);
     if(add_rofs!=0 && add_rofs!=1 &&add_rofs!=2 &&add_rofs!=3 &&add_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",add_position);
     if(strlen(add_position)==0 || strlen(add_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",add_addr_province);
     if(strlen(add_addr_province)==0 || strlen(add_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",add_addr_city);
     if(strlen(add_addr_city)==0 || strlen(add_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",a dd_addr_street);
     if(strlen(add_addr_street)==0 || strlen(add_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",add_tel);
     if(strlen(add_tel)==0 || strlen(add_tel)>13){
        status=false;puts("input error,telephone number cannot over 13 character or less than 0 character!");
        continue;
     }
    else
      status=true;
 }

 /*检查完毕,开始将输入的东西保存到结构数组中*/

strcpy(profile[*i].name,add_name);
profile[*i].sex=add_sex;

profile[*i].birthday.year=add_birthday_year;
profile[*i].birthday.day=add_birthday_day;
profile[*i].birthday.month=add_birthday_month;

profile[*i].workday.year=add_workday_year;
profile[*i].workday.day=add_workday_day;
profile[*i].workday.month=add_workday_month;

profile[*i].rofs=add_rofs;

strcpy(profile[*i].position,add_position);
strcpy(profile[*i].addr.province,add_addr_province);
strcpy(profile[*i].addr.city,add_addr_city);
strcpy(profile[*i].addr.street,add_addr_street);

strcpy(profile[*i].tel,add_tel);
++(*i);
printf("\n\nin function add :i=%d\n\n",*i);
}

/*显示所有的工人资料的列表*/

void list(int i,struct WORKER *prof){
int j;
char temp[20];
if(i==0){printf("There is no profile in the list!\nPress any key to return!\n");getch();return;}
printf("========There are %d workers' profiles========\n\n",i);
 for(j=0;j<i;j++){
  printf("NO. %d worker's profile=>\n",(j+1));
 printf("\n\tName: %s\n",prof[j].name);
 if(prof[j].sex)
   strcpy(temp,"Female");
 else
   strcpy(temp,"Male");
 printf("\tGender:%s\n",temp);
 printf("\tBirthday:");
   printf("%4d %2d %2d\n",prof[j].birthday.year,prof[j].birthday.month,prof[j].birthday.day);
 printf("\tWorkday:");
   printf("%4d %2d %2d\n",prof[j].workday.year,prof[j].workday.month,prof[j].workday.day);
 switch(prof[j].rofs){
  case 0:
   strcpy(temp,"Element school");
   break;
  case 1:
   strcpy(temp,"Middle school");
   break;
  case 2:
   strcpy(temp,"High school");
   break;
  case 3:
   strcpy(temp,"University");
   break;
  case 4:
   strcpy(temp,"Other");
   break;
  default:
  strcpy(temp,"Error");
 }
 printf("\tThe record of formal schooling:%s\n",temp);
 printf("\tPosition:%s\n",prof[j].position);
 printf("\tAddress:%s %s city %s province\n",prof[j].addr.street,prof[j].addr.city,prof[j].addr.province);
 printf("\tTelephone number:%s\n",prof[j].tel);
 getch();
 }
}

/*显示指定的工人的资料*/

void display(int dk,struct WORKER *prof){
char temp[20];
 if(dk>maxworker)
  return;
printf("\n===============The profile of %s=================\n\n",prof[dk].name);
printf("\n\tName: %s\n",prof[dk].name);
 if(prof[dk].sex)
   strcpy(temp,"Female");
 else
   strcpy(temp,"Male");
 printf("\tGender:%s\n",temp);
 printf("\tBirthday:%4d %2d %2d\n",prof[dk].birthday.year,prof[dk].birthday.month,prof[dk].birthday.day);
 printf("\tWorkday:%4d %2d %2d\n",prof[dk].workday.year,prof[dk].workday.month,prof[dk].workday.day);
 switch(prof[dk].rofs){
  case 0:
   strcpy(temp,"Element school");
   break;
  case 1:
   strcpy(temp,"Middle school");
   break;
  case 2:
   strcpy(temp,"High school");
   break;
  case 3:
   strcpy(temp,"University");
   break;
  case 4:
   strcpy(temp,"Other");
   break;
  default:
  strcpy(temp,"Error");
 }
 printf("\tThe record of formal schooling:%s\n",temp);
 printf("\tPosition:%s\n",prof[dk].position);
 printf("\tAddress:%s %s city %s province\n",prof[dk].addr.street,prof[dk].addr.city,prof[dk].addr.province);
 printf("\tTelephone number:%s\n",prof[dk].tel);
 getch();
}

/*从数据库中搜索一个指定的工人的资料*/

int search(int i,struct WORKER * sp,int mode,char trans[]){
 char search_name[20];
 int j,n;

 /*错误处理*/
if(!strcmp(trans,"")){
  printf("Please enter the name that you want to search:");
  scanf("%s",search_name);
}
 else
   strcpy(search_name,trans);
 /*开始搜索*/
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;
  char c;
  int i=0;
  clrscr();
 
  data_load( &i,profile);
  do
  { 
    printf("\n\t[]-----------------------------------------------------[]\n");
    printf("\t|\t\tSample Worker Management System" " " " " " " " " " " "" " " " " "" " " " " "|\n");
    printf("\t[]-----------------------------------------------------[]\n\n");
    printf("\t1.Add one worker to the file\n");
    printf("\t2.Delete one worker from the file\n");
    printf("\t3.Search one worker from the file\n");
    printf("\t4.Modify one worker's information\n");
    printf("\t5.List all the profiles\n");
    printf("\t6.Save new data\n");
    printf("\t7.Sort the profiles\n");
    printf("\t8.Reload data from database file\n");
    printf("\t9.Delete all data\n");
    printf("\t0.exit without save\n\n\n");
    printf("\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')exit(0);
            break;
          default:
           printf("please enter the correct selection!");
         }
 }while(1);
}

上一篇:{实例}简单扫雷游戏 人气:6399
下一篇:{实例}我的黑白棋 人气:5912
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058