#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct //定義職員的結構體,包括名字,性別,出生年月,工作年月,學歷,職務,地址,電話
{
char name[20];//姓名
char sex; //性別
int birthday; //出生年月
int year; //工作年月
char degree; // 學歷
char position; //職務
char address; // 地址
char phone; //電話
}employee;
struct employee emp[80]; //結構體數組變量
int menu_select() //菜單函數
{
char c;
do{
printf("職工管理系統\n"); //菜單選擇
printf(" | 1. Input Records \n");
printf(" | 2. Display All Records |\n");
printf(" | 3. Sort |\n");
printf(" | 4. Insert a Record |\n");
printf(" | 5. Delete a Record |\n");
printf(" | 6. Query |\n");
printf(" | 7. Change |\n");
printf(" | 0. Quit |\n");
printf("Give your Choice(0-7):");
c=getchar(); //讀入選擇
}while(c<'0'||c>'7');
return(c-'0'); //返回選擇
}
int Input(employee emp[],int n) //輸入若干條記錄
{
int i=0;
char sign;
do
{
printf("employee's name:\n"); //交互輸入
scanf("%s",&employee[i].name);
printf("employee's sex:\n");
scanf("%s",&employee[i].sex);
printf("employee's birthday:\n");
scanf("%d",&employee[i]. birthday);
printf("employee's year:\n");
scanf("%d",&employee[i].year);
printf("employee's degree:\n");
scanf("%s",&employee[i].degree);
printf("employee's position:\n");
scanf("%s",&employee[i].position);
printf("employee's address:\n");
scanf("%s",&employee[i].address);
printf("employee's phone:\n");
scanf("%s",&employee[i].phone);
printf("any more records?(Y/N)\n");
scanf("%c",&sign);//輸入判斷
i++;
} while(sign!='n'&&sign!='N'); //判斷
return n+i;
}
Display(employee emp[],int n)//顯示所有記錄
{
int i;
printf("顯示記錄\n");
for(i=1;i<n+1;i++)//循環輸入
printf("%s\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\n",emp[i-1].name,emp[i-1].sex,emp[i-1].birthday,emp[i-1].year,emp[i-1].degree,emp[i-1].position,emp[i-1].address,emp[i-1].phone);
}
void Sort_by_name(employee emp[],int n) /*按學號排序*/
{
int i,j,k,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(emp[j].name,emp[j+1].name)>0)
{
strcpy(t,emp[j+1].name); //復制姓名
strcpy(emp[j+1].name,emp[j].name);
strcpy(emp[j].name,t);
strcpy(t,emp[j+1].sex); //復制性別
strcpy(emp[j+1].sex,emp[j].sex);
strcpy(emp[j].sex,t);
s=emp[j+1]. birthday;//復制出生年月
emp[j+1].birthday=emp[j].birthday;
emp[j].birthday=s;
k=emp[j+1].year; //工作年月
emp[j+1].year=emp[j].year;
emp[j].year=k;
strcpy(t,emp[j+1].degree); //學歷
strcpy(emp[j+1].degree,emp[j].degree);
strcpy(emp[j].degree,t);
strcpy(t,emp[j+1].position); //職務
strcpy(emp[j+1].position,emp[j].position);
strcpy(emp[j].position,t);
strcpy(t,emp[j+1].address); //住址
strcpy(emp[j+1].address,emp[j].address);
strcpy(emp[j].address,t);
strcpy(t,emp[j+1].phone); // 電話
strcpy(emp[j+1].phone,emp[j].phone);
strcpy(emp[j].phone,t);
}
}
int Insert_a_record(employee emp[],int n) /*插入一條記錄*/
{
printf("employee's name:\n");
scanf("%s",&employee[i].name);
printf("employee's sex:\n");
scanf("%s",&employee[i].sex);
printf("employee's birthday:\n");
scanf("%d",&employee[i]. birthday);
printf("employee's year:\n");
scanf("%d",&employee[i].year);
printf("employee's degree:\n");
scanf("%s",&employee[i].degree);
printf("employee's position:\n");
scanf("%s",&employee[i].position);
printf("employee's address:\n");
scanf("%s",&employee[i].address);
printf("employee's phone:\n");
scanf("%s",&employee[i].phone);
n++;
Sort_by_name(emp,n); /*調用排序函數*/
printf("成功插入記錄\n"); /*返回成功信息*/
return(n);
}
int Delete_a_record(employee emp[],int n) /*按姓名查找,刪除一條記錄*/
{
char s[20];
int i=0,j;
printf("請輸入姓名:\n"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(emp[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到\n"); /*返回失敗信息*/
return(n);
}
for(j=i;j<n-1;j++) /*刪除操作*/
{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].name,stud[j+1].name);
stud[j].score=stud[j+1].score;
}
printf("刪除成功!\n"); /*返回成功信息*/
return(n-1);
}
void Query_a_record(employee emp[],int n) /*查找並顯示一個記錄*/
{
char s;
int i=0;
printf("輸入性別 男性:m 女性:w \n"); /*交互式輸入*/
scanf("%s",s);
while(strcmp(emp[i].sex,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到記錄!\n"); /*輸入失敗信息*/
return;
}
printf("%s\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\n",emp[i].name,emp[i].sex,emp[i].birthday,emp[i].year,emp[i].degree,emp[i].position,emp[i].address,emp[i].phone);
}
void Change_a_record(employee emp[],int n) //查找記錄並修改記錄
{
char s[20], s, m;
int i=0;
printf("請輸入姓名:\n"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(emp[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到\n"); /*返回失敗信息*/
return(n);
}
printf("請輸入要修改的信息 學歷:d 職務:p 住址:a 電話:t\n");
scanf("%s",&s);
printf("請輸入修改後的信息:\n");
scanf("%s",&m);
switch(s)
{
case d: strcpy(emp[i].degree,m); break;
case p: strcpy(emp[i].position,m); break;
case a: strcpy(emp[i].address,m); break;
case t: strcpy(emp[i].phone,m); break;
default: printf("輸入錯誤!\n");
}
}
void main() /*主函數*/
{
int n=0;
for(;;)
{
switch(menu_select()) /*選擇判斷*/
{
case 1:
printf("Input Records\n"); /*輸入若干條記錄*/
n=Input(emp,n);
break;
case 2:
printf("Display All Records\n"); /*顯示所有記錄*/
Display(emp,n);
break;
case 3:
printf("Sort\n");
Sort_by_name(emp,n); /*按姓名排序*/
printf("Sort Suceessed!\n");
break;
case 4:
printf("Insert a Record\n");
n=Insert_a_record(emp,n); /*插入一條記錄*/
break;
case 5:
printf("Delete a Record\n");
n=Delete_a_record(emp,n); /*按姓名查找,刪除一條記錄*/
break;
case 6:
printf("Query\n");
Query_a_record(emp,n); /*查找並顯示一個記錄*/
break;
case 7:
printf("Change\n");
Change_a_record(emp,n); /*新增功能,輸出統計信息*/
break;
case 0:
printf("Have a Good Luck,Bye-bye!\n"); /*結束程序*/
exit(0);
}
}
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct //定義職員的結構體,包括名字,性別,出生年月,工作年月,學歷,職務,地址,電話
{
char name[20];//姓名
char sex[4]; //性別
int birthday; //出生年月
int year; //工作年月
char degree[8]; // 學歷///這個也是字符表示吧
char position[30]; //職務///同樣一個字符也表示不了職務
char address[30]; // 地址///一個字符怎麼表示地址???
char phone[15]; //電話///
}employee;
employee emp[80]; ///結構體數組變量,去掉"struct"
int menu_select() //菜單函數
{
char c;
do{
printf("職工管理系統\n"); //菜單選擇
printf(" | 1. Input Records \n");
printf(" | 2. Display All Records |\n");
printf(" | 3. Sort |\n");
printf(" | 4. Insert a Record |\n");
printf(" | 5. Delete a Record |\n");
printf(" | 6. Query |\n");
printf(" | 7. Change |\n");
printf(" | 0. Quit |\n");
printf("Give your Choice(0-7):");
c=getchar(); //讀入選擇
}while(c<'0'||c>'7');
return(c-'0'); //返回選擇
}
int Input(employee emp[],int n) //輸入若干條記錄
{
int i=0;
char sign;
do
{
printf("employee's name:\n"); //交互輸入
scanf("%s",&emp[i].name); ///沒"employee"數組,只有"emp"數組,以下幾處都是這樣的
printf("employee's sex:\n");
scanf("%s",&emp[i].sex); ///
printf("employee's birthday:\n");
scanf("%d",&emp[i]. birthday);///
printf("employee's year:\n");
scanf("%d",&emp[i].year); ///
printf("employee's degree:\n");
scanf("%s",&emp[i].degree);///
printf("employee's position:\n");
scanf("%s",&emp[i].position);///
printf("employee's address:\n");
scanf("%s",&emp[i].address);///
printf("employee's phone:\n");
scanf("%s",&emp[i].phone);///
printf("any more records?(Y/N)\n");
getchar();
sign=getchar();//輸入判斷///第一次運行,輸入數據時退不出來了
i++;
} while(sign!='n'&&sign!='N'); //判斷
return n+i;
}
Display(employee emp[],int n)//顯示所有記錄
{
int i;
printf("顯示記錄\n");
for(i=0;i<n;i++)//循環輸入///你那個輸出太不容易看了
{
printf("num:\t\t%d\n",i+1);
printf("name:\t\t%s\n",emp[i].name);
printf("sex:\t\t%s\n",emp[i].sex);
printf("birthday:\t%d\n",emp[i].birthday);
printf("year:\t\t%d\n",emp[i].year);
printf("degree:\t\t%s\n",emp[i].degree);
printf("position:\t%s\n",emp[i].position);
printf("phone:\t\t%s\n",emp[i].phone);
}
}
void Sort_by_name(employee emp[],int n) /*按學號排序*/
{
int i,j;
employee t;///中間量用一個定義的結構體變量更好
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(emp[j].name,emp[j+1].name)>0)
{
strcpy(t.name,emp[j+1].name); //復制姓名
strcpy(emp[j+1].name,emp[j].name);
strcpy(emp[j].name,t.name);
strcpy(t.sex,emp[j+1].sex); //復制性別
strcpy(emp[j+1].sex,emp[j].sex);
strcpy(emp[j].sex,t.sex);
t.birthday=emp[j+1]. birthday;//復制出生年月
emp[j+1].birthday=emp[j].birthday;
emp[j].birthday=t.birthday;
t.year=emp[j+1].year; //工作年月
emp[j+1].year=emp[j].year;
emp[j].year=t.year;
strcpy(t.degree,emp[j+1].degree); //學歷
strcpy(emp[j+1].degree,emp[j].degree);
strcpy(emp[j].degree,t.degree);
strcpy(t.position,emp[j+1].position); //職務
strcpy(emp[j+1].position,emp[j].position);
strcpy(emp[j].position,t.position);
strcpy(t.address,emp[j+1].address); //住址
strcpy(emp[j+1].address,emp[j].address);
strcpy(emp[j].address,t.address);
strcpy(t.phone,emp[j+1].phone); // 電話
strcpy(emp[j+1].phone,emp[j].phone);
strcpy(emp[j].phone,t.phone);
}
}
int Insert_a_record(employee emp[],int n) /*插入一條記錄*/
{
printf("employee's name:\n");
scanf("%s",&emp[n].name); ///i改成n,下面也是
printf("employee's sex:\n");
scanf("%s",&emp[n].sex); ///
printf("employee's birthday:\n");
scanf("%d",&emp[n]. birthday);///
printf("employee's year:\n");
scanf("%d",&emp[n].year); ///
printf("employee's degree:\n");
scanf("%s",&emp[n].degree);///
printf("employee's position:\n");
scanf("%s",&emp[n].position);///
printf("employee's address:\n");
scanf("%s",&emp[n].address);///
printf("employee's phone:\n");
scanf("%s",&emp[n].phone);///
n++;
Sort_by_name(emp,n); /*調用排序函數*/
printf("成功插入記錄\n"); /*返回成功信息*/
return(n);
}
int Delete_a_record(employee emp[],int n) /*按姓名查找,刪除一條記錄*/
{
char s[20];
int i=0,j;
printf("請輸入姓名:\n"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(emp[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到\n"); /*返回失敗信息*/
return(n);
}
for(j=i;j<n-1;j++) /*刪除操作*/
{
///我想"stud"應該是"emp",結構體內容和你定義的都不一樣,你是不是從其它地方復制過來的?
strcpy(emp[j].name,emp[j+1].name);
strcpy(emp[j].sex,emp[j+1].sex);
emp[j].birthday=emp[j+1].birthday;
emp[j].year=emp[j+1].year;
strcpy(emp[j].degree,emp[j+1].degree);
strcpy(emp[j].position,emp[j+1].position);
strcpy(emp[j].address,emp[j+1].address);
strcpy(emp[j].phone,emp[j+1].phone);
}
printf("刪除成功!\n"); /*返回成功信息*/
return(n-1);
}
void Query_a_record(employee emp[],int n) /*查找並顯示一個記錄*/
{
char s;
int i=0;
printf("輸入性別 男性:m 女性:w \n"); /*交互式輸入*/
scanf("%s",s);
while(strcmp(emp[i].sex,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到記錄!\n"); /*輸入失敗信息*/
return;
}
printf("%s\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\n",emp[i].name,\
emp[i].sex,emp[i].birthday,emp[i].year,emp[i].degree,emp[i].position,emp[i].address,emp[i].phone);
}
void Change_a_record(employee emp[],int n) //查找記錄並修改記錄
{
char s[20], c, m; ///將字符變量"s"改為"c"
int i=0;
printf("請輸入姓名:\n"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(emp[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("未查找到\n"); /*返回失敗信息*/
return(n);
}
printf("請輸入要修改的信息 學歷:d 職務:p 住址:a 電話:t\n");
c=getchar();
printf("請輸入修改後的信息:\n");
scanf("%s",&m);
switch(c)
{
case 'd': strcpy(emp[i].degree,m); break;///字符常量加上單引號,與變量相區別
case 'p': strcpy(emp[i].position,m); break;
case 'a': strcpy(emp[i].address,m); break;
case 't': strcpy(emp[i].phone,m); break;
default: printf("輸入錯誤!\n");
}
}
void main() /*主函數*/
{
int n=0;
for(;;)
{
switch(menu_select()) /*選擇判斷*/
{
case 1:
printf("Input Records\n"); /*輸入若干條記錄*/
n=Input(emp,n);
break;
case 2:
printf("Display All Records\n"); /*顯示所有記錄*/
Display(emp,n);
break;
case 3:
printf("Sort\n");
Sort_by_name(emp,n); /*按姓名排序*/
printf("Sort Suceessed!\n");
break;
case 4:
printf("Insert a Record\n");
n=Insert_a_record(emp,n); /*插入一條記錄*/
break;
case 5:
printf("Delete a Record\n");
n=Delete_a_record(emp,n); /*按姓名查找,刪除一條記錄*/
break;
case 6:
printf("Query\n");
Query_a_record(emp,n); /*查找並顯示一個記錄*/
break;
case 7:
printf("Change\n");
Change_a_record(emp,n); /*新增功能,輸出統計信息*/
break;
case 0:
printf("Have a Good Luck,Bye-bye!\n"); /*結束程序*/
exit(0);
}
}
}
本來以為只要小改幾處,沒想要改的地方那麼多,我改的地方用"///"標出
以前也想到過編一個這樣的程序,但一直沒有實行
這個程序真正用還不行,沒有讀存程序,一旦退出,所輸入數據都沒有了