学生管理系统、职工管理系统类似xx管理系统题目的模板,有需要的拿去修改
程序代码:
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> struct information{ int num; //工号 char name[30]; char birth[20]; char gender[10]; //性别 char education[20]; //学历 char position[30]; //职位 double wage; //工资 char address[200]; char phone[15]; struct information *next; }; typedef struct information infor; typedef infor *inforPtr; void menu(void); void add (inforPtr *sPtr,int value); int exist(inforPtr *sPtr, int value); void display(inforPtr newList); void query (inforPtr *sPtr, int (*compare)(inforPtr original, inforPtr creat)); int queryByWage (inforPtr original , inforPtr creat); void save (inforPtr *sPtr); void load ( inforPtr * sPtr); void del (inforPtr *sPtr,int value); int main (void) { inforPtr startList=NULL; int select; int num; int delNum; printf ("**************************欢迎使用职工信息管理系统*****************************\n"); load(&startList); //读取文件数据,传入链表以便传从load函数递链表至主函数 display(startList); printf("\n\n\n"); menu(); scanf("%d", &select); while(select != 9) { switch (select){ case 1: printf("输入工号:\n?"); scanf("%d", &num); if( exist(&startList ,num) ) { printf("记录已经存在,不能添加\n\n"); break; } else add (&startList ,num); break; case 2: display(startList); break; case 3: query(&startList,queryByWage); break; case 4: // save(&startList); break; case 5: // load(&startList); break; case 6: printf("输入要删除员工工号:\n?"); scanf("%d", &delNum); if( !exist(&startList ,delNum) ) { printf("记录不存在,无法删除!\n\n"); break; } else del(&startList,delNum); break; } menu(); scanf("%d", &select); } return 0; } /****菜单*****/ void menu (void) { puts("1. 增加新纪录."); puts("2. 按工号显示记录"); puts("3. 按工资排序"); // puts("4. 保存"); // puts("5. 读取"); puts("6. 删除"); puts("9. 退出"); printf("? "); } /*判断是否已经存在工号*/ int exist (inforPtr *sPtr , int value) /*判断工号是否已经存在*/ { inforPtr current; current = *sPtr; while(current != NULL ) { if (value == current->num) return 1; current=current->next; } return 0; } void add (inforPtr *sPtr,int value) /*使用链表 添加记录*/ { inforPtr current; inforPtr prev; inforPtr newList; int c; /* char str[450]={0}; */ if ( (newList = (struct information *) malloc (sizeof (struct information))) != NULL) { current = *sPtr; prev = NULL; newList->num = value; newList ->next = NULL; while( (c=getchar())!='\n' && c !=NULL); //fflush(stdin) /* printf("%-10s%-10s%-10s%-15s%-10s%-10s%-20s%s\n","姓名","出生年月","性别","教育程度","职位","工资","住址","电话"); gets(str); sscanf(str,"%[^0-9]%s%s%s%s%lf%s%s", newList->name, newList->birth, newList->gender, newList->education, newList->position, &newList->wage, newList->address, newList->phone); */ printf("姓名:\n"); scanf("%s",newList->name); printf("\n出生年月:\n"); scanf("%s",newList->birth); printf("\n性别:\n"); scanf("%s",newList->gender); printf("\n教育程度:\n"); scanf("%s",newList->education); printf("\n职位:\n"); scanf("%s",newList->position); printf("\n工资:\n"); scanf("%lf",&newList->wage); printf("\n住址:\n"); scanf("%s",newList->address); printf("\n电话:\n"); scanf("%s",newList->phone); while ( current != NULL && value > current ->num) { prev = current; current = current ->next; } if(prev == NULL) { newList->next=*sPtr; *sPtr = newList; } else { newList->next=current; prev->next = newList; } } else printf("内存已满,无法添加\n"); save(sPtr); } /*显示链表中数据*/ void display( inforPtr newList) { fflush(stdout); printf("\n\n************************************************************************************************************************\n\n"); printf("%-5s%-13s%-12s%-5s%-15s%-10s%-10s%-40s%s\n","工号","姓名","出生年月","性别","教育程度","职位","工资","住址","电话"); while (newList != NULL) { printf("%-5.3d%-13s%-12s%-5s%-15s%-10s%-10.2lf%-40s%s\n",newList->num, newList->name, newList->birth, newList->gender, newList->education, newList->position, newList->wage, newList->address, newList->phone); newList = newList->next; } printf("\n*************************************************************************************************************************\n\n"); } void query (inforPtr *sPtr, int (*compare)(inforPtr original, inforPtr creat)) { inforPtr SPtr; //equal to *sPtr inforPtr newPtr; //newList inforPtr currentPtr; inforPtr prevPtr; inforPtr headPtr; SPtr =*sPtr; newPtr = NULL; currentPtr = NULL; prevPtr = NULL; headPtr = NULL; while (SPtr != NULL) { if( ( newPtr = ( struct information *) malloc ( sizeof (struct information) ) ) != NULL) { prevPtr = NULL; currentPtr = headPtr; newPtr->next = NULL; /*move*/ while( currentPtr != NULL && (*compare)(SPtr , currentPtr)) { prevPtr = currentPtr; currentPtr = currentPtr ->next ; } if ( prevPtr == NULL) { newPtr->next = headPtr; headPtr = newPtr; } else { prevPtr->next = newPtr; newPtr ->next = currentPtr; } strcpy (newPtr->address , SPtr->address); strcpy (newPtr->birth ,SPtr ->birth ); strcpy (newPtr->education, SPtr->education); strcpy(newPtr->gender , SPtr->gender); strcpy(newPtr->name , SPtr ->name); newPtr ->num = SPtr->num; strcpy(newPtr->phone, SPtr ->phone); strcpy (newPtr ->position ,SPtr ->position); newPtr->wage = SPtr->wage; SPtr=SPtr->next; } else printf("OUT OF MEMORY \n"); } display(headPtr); } /*按照工资排序*/ int queryByWage (inforPtr original , inforPtr creat) { return original->wage < creat ->wage; } /*保存到文件*/ void save (inforPtr *sPtr) { FILE *fPtr; inforPtr SPtr; SPtr=*sPtr; if ( (fPtr=fopen("data.dat","wb++")) !=NULL) { while (SPtr !=NULL) { fwrite(SPtr ,sizeof( struct information) , 1 ,fPtr); SPtr=SPtr->next; } } else { printf("File can not open!\n\n"); exit(0); } fclose(fPtr); printf("\n*****自动保存成功!*****\n\n"); } void load ( inforPtr *sPtr) { FILE *fPtr; infor newList ; inforPtr currentPtr = NULL; inforPtr headPtr = NULL; inforPtr newPtr = NULL; inforPtr prevPtr = NULL; int verify=-100; struct _stat verifyFile; if ( (fPtr=fopen("data.dat","rb++")) == NULL) { if ( (fPtr=fopen("data.dat","wb++")) == NULL) printf("File can not open"); else printf ( "\n欢迎首次使用,本系统所有数据将自动保存!\n" ); } else { stat("data.dat",&verifyFile); if (verifyFile.st_size != 0) { headPtr=NULL; newPtr= NULL; currentPtr = NULL; while(!feof(fPtr)) { fread(&newList, sizeof(struct information),1,fPtr); /* printf("%-5.3d%-10s%-10s%-10s%-15s%-10s%-10.2lf%-20s%s\n",newList.num, newList.name, newList.birth, newList.gender, newList.education, newList.position, newList.wage, newList.address, newList.phone); */ if(verify != newList.num) { if( ( newPtr = ( struct information *) malloc ( sizeof (struct information) ) ) != NULL) { prevPtr = NULL; currentPtr = headPtr; newPtr->next = NULL; while( currentPtr != NULL && newList.num>currentPtr->num) { prevPtr = currentPtr; currentPtr = currentPtr ->next ; } if ( prevPtr == NULL) { newPtr->next = headPtr; headPtr = newPtr; } else { prevPtr->next = newPtr; newPtr ->next = currentPtr; } strcpy (newPtr->address , newList.address); strcpy (newPtr->birth ,newList.birth ); strcpy (newPtr->education, newList.education); strcpy(newPtr->gender , newList.gender); strcpy(newPtr->name , newList.name); newPtr ->num = newList.num; strcpy(newPtr->phone, newList.phone); strcpy (newPtr ->position ,newList.position); newPtr->wage = newList.wage; verify = newList.num; } /* END IF ALLOC */ } /* END IF VERIFY */ } /*END WHILE */ *sPtr=headPtr; /*把文件头传递到main*/ fclose(fPtr); printf("\n********读取 \"数据库\" 成功!********\n\n"); }//end stat if } /*end else*/ } void del (inforPtr *sPtr,int value) { inforPtr currentPtr; inforPtr prevPtr; currentPtr= *sPtr; prevPtr =NULL; while (currentPtr != NULL && value != currentPtr ->num) { prevPtr = currentPtr; currentPtr = currentPtr ->next; } if (prevPtr == NULL) { *sPtr=currentPtr->next; } else { prevPtr->next = currentPtr->next; } save(sPtr); }
我是按照下面帖子要求编的,其余系统都很类似,修改输出名称或者删除一些不需要的变量即可。
https://bbs.bccn.net/thread-342476-1-2.html
[ 本帖最后由 oszc 于 2011-6-13 22:17 编辑 ]