/*这是我开始做课程设计时写的*/
#include "stdio.h" /*标准输入输出函数库*/
#include "stdlib.h" /*标准函数库*/
#include "string.h" /*字符串函数库*/
#include "conio.h" /*屏幕操作函数库*/
#define HEADER1 " -------------------------------ZGGZ---------------------------------------- \n"
#define HEADER2 "| number| name | jbgz | jj | kk | yfgz | sk | sfgz | \n"
#define HEADER3 "|--------|-----------|--------|--------|--------|--------|--------|--------| \n"
#define FORMAT "|%-8s|%-10s |%8.2f|%8.2f|%8.2f|%8.2f|%8.2f|%8.2f| \n"
#define DATA p->num,p->name,p->jbgz,p->jj,p->kk,p->yfgz,p->sk,p->sfgz
#define END "---------------------------------------------------------------------------- \n"
#define N 60
int saveflag=0; /*是否需要存盘的标志变量*/
/*定义与职工有关的数据结构*/
typedef struct employee /*标记为employee*/
{
char num[10]; /*职工编号*/
char name[15]; /*职工姓名*/
float jbgz; /*基本工资*/
float jj; /*奖金*/
float kk; /*扣款*/
float yfgz; /*应发工资*/
float sk; /*税款*/
float sfgz; /*实发工资*/
}ZGGZ;
void menu() /*主菜单*/
{
system("cls"); /*调用DOS命令,清屏.与clrscr()功能相同*/
textcolor(10); /*在文本模式中选择新的字符颜色*/
gotoxy(10,5); /*在文本窗口中设置光标*/
cprintf(" The Employee' Salary Management System \n");
gotoxy(10,8);
cprintf(" *************************Menu********************************\n");
gotoxy(10,9);
cprintf(" * 1 input record 2 delete record *\n");
gotoxy(10,10);
cprintf(" * 3 search record 4 modify record *\n");
gotoxy(10,11);
cprintf(" * 5 insert record 6 count record *\n");
gotoxy(10,12);
cprintf(" * 7 sort reord 8 save record *\n");
gotoxy(10,13);
cprintf(" * 9 display record 0 quit system *\n");
gotoxy(10,14);
cprintf(" *************************************************************\n");
/*cprintf()送格式化输出至文本窗口屏幕中*/
}
void printheader() /*格式化输出表头*/
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(ZGGZ pp) /*格式化输出表中数据*/
{
ZGGZ* p;
p=&pp;
printf(FORMAT,DATA);
}
void Disp(ZGGZ tp[],int n) /*显示数组tp[]中存储的记录,内容为employee结构中定义的内容*/
{
int i;
if(n==0) /*表示没有职工工资记录*/
{
printf("\n=====>Not employee record!\n");
getchar();
return;
}
printf("\n\n");
printheader(); /*输出表格头部*/
i=0;
while(i
printdata(tp[i]);
i++;
printf(HEADER3);
}
getchar();
}
void Wrong() /*输出按键错误信息*/
{
printf("\n\n\n\n\n***********Error:input has wrong! press any key to continue**********\n");
getchar();
}
void Nofind() /*输出未查找此职工的信息*/
{
printf("\n=====>Not find this employee record!\n");
}
/*************************************************************
作用:用于定位数组中符合要求的记录,并返回保存该记录的数组元素下标值
参数:findmess[]保存要查找的具体内容; nameornum[]保存按什么在数组中查找;
**************************************************************/
int Locate(ZGGZ tp[],int n,char findmess[],char nameornum[])
{
int i=0;
if(strcmp(nameornum,"num")==0) /*按职工编号查询*/
{
while(i
if(strcmp(tp[i].num,findmess)==0) /*若找到findmess值的职工编号*/
return i;
i++;
}
}
else if(strcmp(nameornum,"name")==0) /*按职工姓名查询*/
{
while(i
if(strcmp(tp[i].name,findmess)==0) /*若找到findmess值的姓名*/
return i;
i++;
}
}
return -1; /*若未找到,返回一个整数-1*/
}
/*输入字符串,并进行长度验证(长度
{
char n[255];
do{
printf(notice); /*显示提示信息*/
scanf("%s",n); /*输入字符串*/
if(strlen(n)>lens) printf("\n exceed the required length! \n"); /*进行长度校验,超过lens值重新输入*/
}while(strlen(n)>lens);
strcpy(t,n); /*将输入的字符串拷贝到字符串t中*/
}
/*输入数值,0<=数值)*/
float numberinput(char *notice)
{
float t=0.00;
do{
printf(notice); /*显示提示信息*/
scanf("%f",&t); /*输入如工资等数值型的值*/
if(t<0) printf("\n score must >=0! \n"); /*进行数值校验*/
}while(t<0);
return t;
}
/*增加职工工资记录*/
int Add(ZGGZ tp[],int n)
{
char ch,num[10];
int i,flag=0;
system("cls");
Disp(tp,n); /*先打印出已有的职工工资信息*/
while(1) /*一次可输入多条记录,直至输入职工编号为0的记录才结束添加操作*/
{
while(1) /*输入职工编号,保证该编号没有被使用,若输入编号为0,则退出添加记录操作*/
{
stringinput(num,10,"input number(press '0'return menu):"); /*格式化输入编号并检验*/
flag=0;
if(strcmp(num,"0")==0) /*输入为0,则退出添加操作,返回主界面*/
{return n;}
i=0;
while(i
if(strcmp(tp[i].num,num)==0)
{
flag=1;
break;
}
i++;
}
if(flag==1) /*提示用户是否重新输入*/
{ getchar();
printf("==>The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return n;
}
else
{break;}
}
strcpy(tp[n].num,num); /*将字符串num拷贝到tp[n].num中*/
stringinput(tp[n].name,15,"Name:");
tp[n].jbgz=numberinput("jbgz:"); /*输入并检验基本工资*/
tp[n].jj=numberinput("jiangjin:"); /*输入并检验奖金*/
tp[n].kk=numberinput("koukuan:"); /*输入并检验扣款*/
tp[n].yfgz=tp[n].jbgz+tp[n].jj-tp[n].kk; /*计算应发工资*/
tp[n].sk=tp[n].yfgz*0.12; /*计算税金,这里取应发工资的百分之一十二*/
tp[n].sfgz=tp[n].yfgz-tp[n].sk; /*计算实发工资*/
saveflag=1;
n++;
}
return n;
}
/*按职工编号或姓名,查询记录*/
void Qur(ZGGZ tp[],int n)
{
int select; /*1:按编号查,2:按姓名查,其他:返回主界面(菜单)*/
char searchinput[20]; /*保存用户输入的查询内容*/
int p=0;
if(n<=0) /*若数组为空*/
{
system("cls");
printf("\n=====>No employee record!\n");
getchar();
return;
}
system("cls");
printf("\n =====>1 Search by number =====>2 Search by name\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按编号查询*/
{
stringinput(searchinput,10,"input the existing employee number:");
p=Locate(tp,n,searchinput,"num");/*在数组tp中查找编号为searchinput值的元素,并返回该数组元素的下标值*/
if(p!=-1) /*若找到该记录*/
{
printheader();
printdata(tp[p]);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else if(select==2) /*按姓名查询*/
{
stringinput(searchinput,15,"input the existing employee name:");
p=Locate(tp,n,searchinput,"name");
if(p!=-1)
{
printheader();
printdata(tp[p]);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else
Wrong();
getchar();
}
/*删除记录:先找到保存该记录的数组元素的下标值,然后在数组中删除该数组元素*/
int Del(ZGGZ tp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{ system("cls");
printf("\n=====>No employee record!\n");
getchar();
return n;
}
system("cls");
Disp(tp,n);
printf("\n =====>1 Delete by number =====>2 Delete by name\n");
printf(" please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"input the existing employee number:");
p=Locate(tp,n,findmess,"num");
getchar();
if(p!=-1)
{
for(i=p+1;i
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
tp[i-1].jbgz=tp[i].jbgz;
tp[i-1].jj=tp[i].jj;
tp[i-1].kk=tp[i].kk;
tp[i-1].yfgz=tp[i].yfgz;
tp[i-1].jbgz=tp[i].sk;
tp[i-1].sfgz=tp[i].sfgz;
}
printf("\n==>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2) /*先按姓名查询到该记录所在的数组元素的下标值*/
{
stringinput(findmess,15,"input the existing employee name:");
p=Locate(tp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;i
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
tp[i-1].jbgz=tp[i].jbgz;
tp[i-1].jj=tp[i].jj;
tp[i-1].kk=tp[i].kk;
tp[i-1].yfgz=tp[i].yfgz;
tp[i-1].jbgz=tp[i].sk;
tp[i-1].sfgz=tp[i].sfgz;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
return n;
}
/*修改记录。先按输入的职工编号查询到该记录,然后提示用户修改编号之外的值,编号不能修改*/
void Modify(ZGGZ tp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{ system("cls");
printf("\n=====>No employee record!\n");
getchar();
return ;
}
system("cls");
printf("modify employee recorder");
Disp(tp,n);
stringinput(findmess,10,"input the existing employee number:"); /*输入并检验该编号*/
p=Locate(tp,n,findmess,"num"); /*查询到该数组元素,并返回下标值*/
if(p!=-1) /*若p!=-1,表明已经找到该数组元素*/
{
printf("Number:%s,\n",tp[p].num);
printf("Name:%s,",tp[p].name);
stringinput(tp[p].name,15,"input new name:");
printf("jbgz:%8.2f,",tp[p].jbgz);
tp[p].jbgz=numberinput("jbgz:");
printf("jiangjin:%8.2f,",tp[p].jj);
tp[p].jj=numberinput("jiangjin:");
printf("koukuan:%8.2f,",tp[p].kk);
tp[p].kk=numberinput("koukuan:");
tp[n].yfgz=tp[n].jbgz+tp[n].jj-tp[n].kk;
tp[n].sk=tp[n].yfgz*0.12;
tp[n].sfgz=tp[n].yfgz-tp[n].sk;
printf("\n=====>modify success!\n");
getchar();
Disp(tp,n);
getchar();
saveflag=1;
}
else
{Nofind();
getchar();
}
return ;
}
/*插入记录:按职工编号查询到要插入的数组元素的位置,然后在该编号之后插入一个新数组元素。*/
int Insert(ZGGZ tp[],int n)
{
char ch,num[10],s[10]; /*s[]保存插入点位置之前的编号,num[]保存输入的新记录的编号*/
ZGGZ newinfo;
int flag=0,i=0,kkk=0;
system("cls");
Disp(tp,n);
while(1)
{ stringinput(s,10,"please input insert location after the Number:");
flag=0;i=0;
while(i
if(strcmp(tp[i].num,s)==0) {kkk=i;flag=1;break;}
i++;
}
if(flag==1)
break; /*若编号存在,则进行插入之前的新记录输入操作*/
else
{ getchar();
printf("\n=====>The number %s is not existing,try again?(y/n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
}
/*以下新记录的输入操作与Add()相同*/
while(1)
{ stringinput(num,10,"input new employee Number:");
i=0;flag=0;
while(i
if(strcmp(tp[i].num,num)==0) {flag=1;break;}
i++;
}
if(flag==1)
{
getchar();
printf("\n=====>Sorry,The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
else
break;
}
strcpy(newinfo.num,num); /*将字符串num拷贝到newinfo.num中*/
stringinput(newinfo.name,15,"Name:");
newinfo.jbgz=numberinput("jbgz:"); /*输入并检验jbgz*/
newinfo.jj=numberinput("jiangjin:"); /*输入并检验jiangjin*/
newinfo.kk=numberinput("koukuan:"); /*输入并检验koukuan*/
newinfo.yfgz=newinfo.jbgz+newinfo.jj-newinfo.kk; /*计算yfgz*/
newinfo.sk=newinfo.yfgz*0.12; /*计算sk*/
newinfo.sfgz=newinfo.yfgz-newinfo.sk;