<<C语言程序设计>>课程设计
发布网友
发布时间:2022-04-22 00:45
我来回答
共1个回答
热心网友
时间:2023-11-21 23:34
给你一个我做的人事管理系统,回去把变量改下就好了,链表已经做好了,如果不会改的话我也没办法了.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int shouldsave=0;
struct employee
{
char num[10];
char name[20];
char sex[4];
};
typedef struct node
{
struct employee data;
struct node *next;
}Node,*Link;
void menu()
{
printf("********************Welcome*************************************\n");
printf(" 1.Input\n");
printf(" 2.sort\n");
printf(" 3.query\n");
printf(" 4.change\n");
printf(" 5.save\n");
printf("****************************************************************\n");
}
void printstart()
{
printf("----------------------------------------------------------------\n");
}
void Wrong()
{
printf("\n=====>wrong info!\n");
}
void Nofind()
{
printf("\n=====>no find!\n");
}
void printc()
{
printf(" number name sex\n");
}
void printe(Node *p)
{
printf(" %-12s %s\t %s",p->data.num,p->data.name,p->data.sex);
}
Node* Locate(Link l,char findmess[])
{
Node *r;
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
return 0;
}
void Add(Link l)
{
Node *p,*r,*s;
char num[10];
r=l;
s=l->next;
while(r->next!=NULL)
r=r->next;
while(1)
{
printf("input number:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
printf("=====>%d is already existed!\n",num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
}
s=s->next;
}
p=(Node *)malloc(sizeof(Node));
strcpy(p->data.num,num);
printf("input name:");
scanf("%s",p->data.name);
getchar();
printf("input sex:");
scanf("%s",p->data.sex);
getchar();
p->next=NULL;
r->next=p;
r=p;
shouldsave=1;
}
}
void Qur(Link l)
{
char findmess[20];
Node *p;
if(!l->next)
{
printf("no info!");
return;
}
printf("input number:");
scanf("%s",findmess);
p=Locate(l,findmess);
if(p)
{
printf("\t\t\t\tresult\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
void Disp(Link l)
{
Node *p;
p=l->next;
if(!p)
{
printf("\n=====>no info to display!\n");
return;
}
printf("\t\t\t\result:\n");
printstart();
printc();
printf("\n");
while(p)
{
printe(p);
p=p->next;
}
printstart();
printf("\n");
}
void Sort(Link l)
{
Link ll;
Node *p,*rr,*s;
ll=(Link)malloc(sizeof(Node));
ll->next=NULL;
if(l->next==NULL)
{
printf("\n=====>no info to sort!\n");
return ;
}
p=l->next;
while(p)
{
s=(Node*)malloc(sizeof(Node));
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL )
rr=rr->next;
if(rr->next==NULL)
rr->next=s;
else
{
s->next=rr->next;
rr->next=s;
}
p=p->next;
}
free(l);
l->next=ll->next;
printf("\n=====>sort is finished!\n");
}
void Save(Link l)
{
FILE* fp;
Node *p;
int flag=1,count=0;
fp=fopen("c:\\employee","wb");
if(fp==NULL)
{
printf("\n=====>error!\n");
exit(1);
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next;
count=count+1;
}
else
{
flag=0;
break;
}
}
if(flag)
{
printf("\n=====>successful! %d records!\n",count);
shouldsave=0;
}
fclose(fp);
}
void main()
{
Link l;
FILE *fp;
int sel;
char ch;
char set;
int count=0;
Node *p,*r;
l=(Node*)malloc(sizeof(Node));
l->next=NULL;
r=l;
fp=fopen("C:\\employee","rb");
if(fp==NULL)
{
printf("\n=====>not existed,set(y/n)?\n");
scanf("%c",&set);
if(set=='y'||set=='Y')
fp=fopen("C:\\employee","wb");
else
exit(0);
}
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp))
{
p->next=NULL;
r->next=p;
r=p;
count=count+1;
}
}
fclose(fp);
printf("\n=====>record finished,%d records\n",count);
while(1)
{
menu();
printf("choose selection:");
scanf("%d",&sel);
if(sel==0)
{
printf("\n=====>exit,bye!\n");
break;
}
switch(sel)
{
case 1:Add(l);break;
case 3:Qur(l);break;
case 4:Disp(l);break;
case 2:Sort(l);break;
case 5:Save(l);break;
default: Wrong();
getchar();
break;
}
}
}