main.h:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct staff)
struct Date
{
int year;
int month;
int day;
};
struct staff
{
int num;
char name[10];
char sex[10];
struct Date date;
char school[10];
char position[10];
int wage;
char address[10];
char call[11];
struct staff *next;
};
int menu();
struct staff *init();
int add(struct staff *head);
void scan(struct staff *head);
void sort(struct staff *head);
void inquire(struct staff *head);
void del(struct staff *head);
void modify(struct staff*head);
void save(struct staff *head,int sum);
void load(struct staff *head,int sum);
#include"main.h"
int main()
{
struct staff *head;
int i,sum;
i=menu();
head=init(); //建立帶結點的鏈表
while(i>=0&&i<=6)
{ switch(i)
{
case 1:sum=add(head);break;
case 2:load(head,sum);break;
case 3:sort(head);break;
case 4:inquire(head);break;
case 5:del(head);break;
case 6:modify(head);
}
printf("1.職工資訊添加\t2.職工資訊瀏覽\t3.按工資排序\n4.按學歷查詢\t5.職工資訊洗掉\t6.修改功能\t7.退出\n");
printf("選擇(1-7)\n");
scanf("%d",&i);
while(i<0||i>7) //如果輸入的不是1-7中的數字,重新輸入
{
printf("選擇有誤,重新選擇(1-7)!\n");
scanf("%d",&i);
}
}
if(i==7) printf("退出管理系統\n");
return 0;
}
struct staff *init()
{
struct staff *head=(struct staff *)malloc(LEN);
head->next=NULL;
return head;
}
#include"main.h"
int add(struct staff *head)
{
int sum=0;
struct staff*p,*tail=head;
p=(struct staff *)malloc(LEN);
while(tail->next!=NULL)
tail=tail->next;
printf("請輸入職工號(輸入0終止職工資訊添加):\n");
scanf("%d",&p->num);
while(p->num!=0)
{
printf("請輸入姓名:\n");
scanf("%s",p->name);
printf("請輸入性別(female or male):\n");
scanf("%s",p->sex);
printf("請輸入出生年月日(中間用逗號隔開):\n");
scanf("%d,%d,%d",&(p->date).year,&(p->date).month,&(p->date).day);
printf("請輸入學歷:\n");
scanf("%s",p->school);
printf("請輸入職務:\n");
scanf("%s",p->position);
printf("請輸入工資:\n");
scanf("%d",&p->wage);
printf("請輸入住址:\n");
scanf("%s",p->address);
printf("請輸入電話:\n");
scanf("%s",p->call);
tail->next=p;
tail=p;
p=(struct staff *)malloc(LEN);
printf("請輸入職工號(輸入0終止職工資訊添加):\n");
scanf("%d",&p->num);
}
tail->next=NULL;
free(p);
p=head->next;
while(p!=NULL)
{
sum=sum+1;
}
save(head,sum);
return sum;
}
#include"main.h"
void scan(struct staff *head)
{
struct staff *p;
if(head->next==NULL)
{
printf("\n無資料!\n");
return;
}
printf("\n職工資訊瀏覽:\n");
p=head->next;
while(p!=NULL)
{
printf("NO.%d,姓名:%s,性別:%s,出生年/月/日:%d/%d/%d,學歷:%s,職務:%s,工資:%d,住址:%s,電話:%s\n",\
p->num,p->name,p->sex,(p->date).year,(p->date).month,(p->date).day,p->school,p->position,p->wage,p->address,p->call);
p=p->next;
}
}
#include"main.h"
void sort(struct staff *head)
{
struct staff *p,*q;
int t1;
char t2[11];
for(p=head->next;(p->next)!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->num>q->num)
{
t1=p->num;
p->num=q->num;
q->num=t1;
t1=(p->date).year;
(p->date).year=(q->date).year;
(q->date).year=t1;
t1=(p->date).month;
(p->date).month=(q->date).month;
(q->date).month=t1;
t1=(p->date).day;
(p->date).day=(q->date).day;
(q->date).day=t1;
t1=p->wage;
p->wage=q->wage;
q->wage=t1; //
strcpy(t2,p->name);
strcpy(p->name,q->name);
strcpy(q->name,t2);
strcpy(t2,p->sex);
strcpy(p->sex,q->sex);
strcpy(q->sex,t2);
strcpy(t2,p->school);
strcpy(p->school,q->school);
strcpy(q->school,t2);
strcpy(t2,p->position);
strcpy(p->position,q->position);
strcpy(q->position,t2);
strcpy(t2,p->address);
strcpy(p->address,q->address);
strcpy(q->address,t2);
strcpy(t2,p->call);
strcpy(p->call,q->call);
strcpy(q->call,t2);
}
}
}
}
#include"main.h"
void inquire(struct staff *head)
{
char school[10];
struct staff *p;
int flag=0;
printf("請輸入要查詢的學歷:\n");
scanf("%s",school);
if(head->next==NULL)
{
printf("無資料!\n");
return;
}
p=head->next;
while(p!=NULL)
{
while(strcmp(school,p->school)!=0)
p=p->next;
if(strcmp(school,p->school)==0)
{
printf("NO.%d學歷為%s\n",p->num,p->school);
p=p->next;
flag=1;
}
}
if(flag==0) printf("未找到該學歷職工!\n");
}
#include"main.h"
void del(struct staff *head)
{
int data;
struct staff *p1,*p2;
printf("請輸入要洗掉資訊的職工號:\n");
scanf("%d",&data);
if(head->next==NULL)
{
printf("無資料!\n");
return;
}
p2=head;
p1=head->next;
while(data!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(data=https://bbs.csdn.net/topics/=p1->num)
{
p2->next=p1->next;
printf("洗掉的職工號為:%d\n",data);
free(p1);
}
else printf("未找到職工號為%d的職員!\n",data);
}
#include"main.h"
void modify(struct staff*head)
{
struct staff *p;
int num;
printf("請輸入想要修改資訊的職工的職工號:\n");
scanf("%d",&num);
if(head->next==NULL)
{
printf("無資料!\n");
return;
}
p=head->next;
while(p!=NULL)
{
while(p->num!=num)
p=p->next;
if(p->num==num)
{
printf("請輸入想要修改的姓名:\n");
scanf("%s",p->name);
printf("請輸入想要修改的性別(female or male):\n");
scanf("%s",p->sex);
printf("請輸入想要修改的出生年月日(中間用逗號隔開):\n");
scanf("%d,%d,%d",&(p->date).year,&(p->date).month,&(p->date).day);
printf("請輸入想要修改的學歷:\n");
scanf("%s",p->school);
printf("請輸入想要修改的職務:\n");
scanf("%s",p->position);
printf("請輸入想要修改的工資:\n");
scanf("%d",&p->wage);
printf("請輸入想要修改的住址:\n");
scanf("%s",p->address);
printf("請輸入想要修改的電話:\n");
scanf("%s",p->call);
break;
}
}
if(p==NULL) printf("無此職工號!\n");
}
#include"main.h"
int menu()//顯示選單并選擇選單項
{
int i;
printf("\n\n");
printf("********************************************\n");
printf(" 歡迎進入職工資訊管理系統\n");
printf("********************************************\n");
printf("1.職工資訊建立\t2.職工資訊瀏覽\t3.按工資排序\n4.按學歷查詢\t5.職工資訊洗掉\t6.修改功能\t7.退出\n");
printf("選擇(1-7)\n");
scanf("%d",&i);
while(i<0||i>7) //如果輸入的不是1-7中的數字,重新輸入
{
printf("選擇有誤,重新選擇(1-7)!\n");
scanf("%d",&i);
}
return i; // 回傳選擇的選單項的數字
}
#include "main.h"
void save(struct staff *head,int sum)
{
FILE *fp;
struct staff *p;
if((fp=fopen("staff.dat","wb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
p=head->next;
while(p!=NULL);
{
if(fwrite(p,LEN,sum,fp)!=sum)
printf("file write error\n");
}
fclose(fp);
}
#include "main.h"
void load(struct staff *head,int sum)
{
FILE *fp;
struct staff *p;
p=head->next;
if((fp=fopen("staff.dat","rb"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
while(p!=NULL)
{
if(fread(p,LEN,sum,fp)!=sum)
{
if(feof(fp))
{
fclose(fp);
return;
}
printf("file read error\n");
}
else
scan(head);
}
fclose(fp);
}
uj5u.com熱心網友回復:
你的程式有什么問題嗎?uj5u.com熱心網友回復:
應該是檔案輸入有問題,添加職工資訊輸入0就運行終止了
uj5u.com熱心網友回復:
等會我幫你看一下uj5u.com熱心網友回復:
#include "main.h"
void save(struct staff *head,int sum)
{
FILE *fp;
struct staff *p;
if((fp=fopen("staff.dat","wb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
p=head->next;
//while(p!=NULL); //多個分號
while(p!=NULL)
{
//if(fwrite(p,LEN,sum,fp)!=sum)
if(fwrite(p,LEN,sum,fp)!=sum * LEN)
printf("file write error\n");
}
fclose(fp);
}
//#include "main.h"
void load(struct staff *head,int sum)
{
FILE *fp;
struct staff *p;
p=head->next;
if((fp=fopen("staff.dat","rb"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
while(p!=NULL)
{
//if(fread(p,LEN,sum,fp) != sum)
if(fread(p,LEN,sum,fp) != sum * LEN)
{
if(feof(fp))
{
fclose(fp);
return;
}
printf("file read error\n");
}
else
scan(head);
}
fclose(fp);
}
供參考~
對比注釋掉的部分
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63041.html
標籤:C語言
上一篇:請問為什么無法輸出呢
