C语言链表参数传递
- 格式:docx
- 大小:23.03 KB
- 文档页数:9
#include #include #include #define LEN sizeof(struct staff) struct staff { long num; char name[10]; char sex[5]; int age; char degree[10]; long salary; char addr[20]; char tel[15]; struct staff *next; };
int number=0;
void menu() //菜单界面. {printf("\n\t\t>>>>>>>>>>职工信息管理系统<<<<<<<<<<<<\n"); printf("\t\t======================================\n"); printf("\t\t* 1--职工信息浏览 * 2--按工资查询 \n"); printf("\t\t* 3--按学历查询 * 4--按职工号排序 \n"); printf("\t\t* 5--修改职工信息 * 6--删除职工信息 \n"); printf("\t\t* 7--插入一位职工 * 8--程序介绍\n"); printf("\t\t* 9--退出\n"); printf("\t\t======================================\n"); }
void inquire() //提示回到主菜单. { printf(" 按任意键回到主菜单."); //提示. getchar(); //使界面停留. getchar(); system("cls"); menu(); }
void intro() //程序使用前的简介. { static w=0; //定义变量,记录是第几次跳到此函数. w++; system("cls"); //先清楚屏幕. printf("\n\n\t\t -----欢迎使用职工信息管理系统-----\n\n"); //程序说明. printf(" * 1.输入职工信息时,各信息间请用空格分开,并按提示顺序输入各信息.否则程序将不能正常运行.\n"); printf(" * 2.输入一个 0 并按enter键后将结束职工信息的录入.\n"); printf(" * 3.输入 9 将终止整个程序.\n"); printf("\n\t\t\t-----感谢您的使用-----\n\n"); printf(" 按任意键跳到主菜单."); getchar(); //接收字符前停留在简介界面 if(w!=1)getchar(); //如不是第一次执行此程序则此if语句可以使界面停留直到接收一字符. system("cls"); menu(); //出现菜单栏. }
struct staff *creat() //建立链表保存职工信息. { struct staff *scan(); //函数声明. struct staff *head; //定义头指针. struct staff *p1,*p2; p1=p2=(struct staff *)malloc(LEN); //开辟内存. printf(" 请输入职工信息(职工号、姓名、性别、年龄、学历、工资、住址、电话):\n"); //提示需输入的信息. scanf("%ld",&p1->num); //接收职工号 head=NULL; while(p1->num!=0) //职工号若是0则结束输入.
{ scanf("%s%s%d%s%ld%s%s",p1->name,p1->sex,&p1->age,p1->degree,&p1->salary,p1->addr,p1->tel); number++; //记录职工人数. if(number==1) head=p1; else p2->next=p1; p2=p1; p1=(struct staff *)malloc(LEN); //开辟内存. scanf("%ld",&p1->num); } p2->next=NULL; return(head); //返回链表的头指针. }
void print(struct staff *head) //输出信息到屏幕上. { struct staff *p; p=head; if(number==0) //如果还未输入过职工信息,则提示先输入. { printf(" 文件中无职工信息.\n"); inquire(); } else if(head!=NULL) //否则输出文件里的信息. { printf(" =================================================================\n"); printf(" 职工号姓名性别年龄学历工资住址电话\n"); do
{printf(" %-8ld%-8s%-8s%-8d%-8s%-8ld%-8s%-s\n",p->num,p->name,p->sex,p->age,p->degree,p->salary,p->addr,p->tel); p=p->next; //逐个输出链表的信息. }while(p!=NULL); printf(" ==================================================================\n"); inquire(); } }
void save(struct staff *head) //保存职工信息到文件。 { FILE *fp; struct staff *p; p=(struct staff *)malloc(LEN); if((fp=fopen("staff.dat","wb"))==NULL) //打开输出文件并使fp指向此文件. {printf("cannot open file\n"); //无法打开则输出"打不开"的信息. exit(0); //终止程序. } p=head; //保存到文件. if(head!=NULL) do { if(fwrite(p,LEN,1,fp)!=1) //把链表中的信息逐个保存到文件中. printf("file write error\n"); p=p->next; //使p指向下一个节点. }while(p!=NULL); fclose(fp); //关闭文件. printf(" 保存到文件成功.\n"); inquire(); }
void lookup1(struct staff *head) //输入职工工资,按工资查询。 { int k=0; long sala; struct staff *p; p=(struct staff *)malloc(LEN); if(number==0) printf(" 请先录入职工信息.\n"); else { p=head; printf(" 请输入要查询的职工工资:"); scanf("%ld",&sala); //输入要查询的工资. if(p!=NULL) { do { if(p->salary==sala) //若找到相对应的工资则输出此职工信息 { k++; if(k==1) //if语句中的内容只输出一次. { printf(" ============================================================\n"); printf(" 职工号姓名性别年龄学历工资住址电话\n"); }
printf(" %-8ld%-8s%-8s%-8d%-8s%-8ld%-8s%-s\n",p->num,p->name,p->sex,p->age,p->degree,p->salary,p->addr,p->tel); } p=p->next; }while(p!=NULL); if(k==1) printf(" ============================================================\n"); } if(k==0) printf(" 没有职工的工资是%d\n",sala); inquire(); } }
void lookup2(struct staff *head) //输入学历,按学历查询。 { int k=0; struct staff *p; p=(struct staff *)malloc(LEN); char str[10]; if(number==0) { printf(" 请先录入职工信息.\n"); inquire(); } else { p=head; printf(" 请输入要查询的职工的学历:"); scanf("%s",str); //输入学历. if(p!=NULL) { do { if(strcmp(p->degree,str)==0) //用字符串比较函数查找相对应的学历. { k++; if(k==1) { printf(" ============================================================\n"); printf(" 职工号姓名性别年龄学历工资住址电话\n"); }
printf(" %-8ld%-8s%-8s%-8d%-8s%-8ld%-8s%-s\n",p->num,p->name,p->sex,p->age,p->degree,p->salary,p->addr,p->tel); } p=p->next; }while(p!=NULL); if(k==1) printf(" ============================================================\n"); } if(k==0) printf(" 没有职工的学历是%s\n",str); inquire(); } }
struct staff * sort(struct staff *head) //按职工号排序. { struct staff *end; struct staff *p; struct staff *p1,*p2; if(number==0)