[求助]查询信息老是提示空表???
#include<stdio.h>#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define N 3
#define NULL 0
typedef struct salary//工资
{
char num[6];//员工号
char name[10];//名字
float courses;//基本工资
float posts;//岗位工资
float ages;//工龄工资
float total;//*****汇总工资*****
}gongzi;
gongzi gz[N];//定义结构数组//
int n=0;//n用于判断写入多少个员工工资信息
float dengji(int me)// 工龄工资计算方式
{
float jiaxin;
if(me>=6)
{
jiaxin=0.15;
}
else
{
switch(me)
{
case 1:jiaxin=0.10;break;
case 2:jiaxin=0.11;break;
case 3:jiaxin=0.12;break;
case 4:jiaxin=0.13;break;
case 5:jiaxin=0.14;break;
default:printf("工龄输入错误,请重新输入:\n");
}
}
return jiaxin;
}
float gonglinggz(int n,int m)// 工龄工资计算方式
{
float zonghe=0;
switch(n)
{
case 1:zonghe=4000+4000*dengji(m);break;
case 2:zonghe=3500+3500*dengji(m);break;
case 3:zonghe=2800+2800*dengji(m);break;
case 4:zonghe=2500+2500*dengji(m);break;
case 5:zonghe=2300+2300*dengji(m);break;
case 6:zonghe=2000+2000*dengji(m);break;
case 7:zonghe=1900+1900*dengji(m);break;
default :printf("等级输入有误,请重新输入:\n");
}
return zonghe;
}
float gwjb(int a)
{
float jb;
switch(a)
{
case 1:jb=4000;break;
case 2:jb=3000;break;
case 3:jb=2000;break;
case 4:jb=1500;break;
case 5:jb=1000;break;
default:break;
}
return jb;
}
/*请假扣款(元/天)*/
float qjkk(int a)
{
float kk;
switch(a)
{
case 1:kk=0;break;
case 2:kk=150;break;
case 3:kk=80;break;
case 4:kk=50;break;
case 5:kk=30;break;
default:break;
}
return kk;
}
/*全勤奖金*/
float qqjj(int a)
{
float jj;
switch(a)
{
case 1:jj=0;break;
case 2:jj=200;break;
case 3:jj=150;break;
case 4:jj=100;break;
case 5:jj=50;break;
default:break;
}
return jj;
}
/*岗位工资计算*/
float gwgz(float a,float b,float c,int d)
{
float gz;
if(d==0)
gz=a+c;
else
gz=a-b*d;
return gz;
}
void save(int m) /*保存文件函数*/
{
int i;
gongzi gz[N];
FILE*fp;
if ((fp=fopen("gongzi.txt","wb"))==NULL)
{
printf ("cannot open file\n");
exit(0);
}
for (i=0;i<m;i++) /*将内存中职工的信息输出到磁盘文件中去*/
if (fwrite(&gz[i],sizeof(struct salary),1,fp)!=1) printf("file write error\n");
fclose(fp);
}
void creat(void)//函数用于建立员工工资信息//
{
int i,m,age,level,days;
int glgz,gwgzs,sum;
int x,y;
gongzi gz[N];
printf("请输入需要创建工资信息的职工人数(1--100):\n");
scanf("%d",&m);
getchar();
for (i=0;i<m;i++)
{
printf("请输入职工号: ");
scanf("%s",&gz[i].num);getchar();
printf("请输入姓名: ");
scanf("%s",gz[i].name);getchar();
printf("请输入工龄: ");
scanf("%d",&age);getchar();
printf("请输入工资等级: ");
scanf("%d",&level);getchar();
printf("请输入请假天数: ");
scanf("%d",&days);getchar();
gz[i].courses=gwjb(level);
printf("基本工资=%.2f\n",gz[i].courses);
glgz=gonglinggz(level,age);
gz[i].ages=glgz;
printf("工龄工资=%.2f\n",gz[i].ages);
gwgzs=gwgz(gwjb(level),qjkk(level),qqjj(days),y);
gz[i].posts=gwgzs;
printf("岗位工资=%.2f\n",gz[i].posts);
gz[i].total=gz[i].courses+gz[i].ages+gz[i].posts;
printf("总工资=%.2f\n",gz[i].total);
system("pause");
printf("\n创建完毕!\n");
}
save(m);
}
int load() /*导入函数*/
{
FILE*fp;
int i=0;
gongzi gz[N];
if((fp=fopen("gongzi.txt","rb"))==NULL)
{
printf ("不能打开文件,请确认磁盘已满或文件已损坏\n");
exit(0);
}
else
{
do
{
fread(&gz[i],sizeof(struct salary),1,fp);
i++;
}
while(feof(fp)==0);
}
fclose(fp);
return(i-1);
}
void search(void)
{
int select;//用于选择工号或者姓名
FILE *fp;
char stringsear[6];//存放员工的工号或者姓名
int a=0;//用于判断员工的工号或姓名是否存在
int i;
if((fp=fopen("gongzi.txt","r"))==NULL){printf("can not open");return;}
if(n==0) {printf("空表\n");return;}
printf("\n =========1 工号查询 =======2 姓名查询\n");
scanf("%d",&select);
getchar();
if(select==1)
{
printf("Please enter the number\n");
scanf("%s",stringsear);
getchar();
puts(stringsear);
for(i=0;i<N;i++)
{
if(strcmp(stringsear,gz[i].num)==0)//
{printf("基本工资=%.2f,岗位工资=%.2f,工龄工资=%.2f,总工资=%.2f",gz[i].courses,gz[i].posts,gz[i].ages,gz[i].total); a=1;system("pause");break;}
}
if(a==0) printf("此工号不存在,重新输入!\n");
}
if(select==2)
{
printf("Please enter the name:\n");
scanf("%s",stringsear);
for(i=0;i<N;i++)
{
if(!strcmp(stringsear,gz[i].name))
{printf("基本工资=%.2f,岗位工资=%.2f,工龄工资=%.2f,总工资=%.2f",gz[i].courses,gz[i].posts,gz[i].ages,gz[i].total); a=1;system("pause");break;}
}
if(a==0) printf("此姓名不存在,重新输入!\n");
}
if(select!=1&&select!=2) printf("输入错误,请输入1或2!\n");
fclose(fp);
}
void main()
{
creat();
system("cls");
search();
}
编译运行都OK,可是结果老是“空表”,求大神指导啊,这是我们的作业,就要交啦!急!!!