第一行还是可以对齐的,到第二行在我执行完插入的代码后,第二行便无法对齐了,我该怎么解决?
#include"stdio.h" /*标准输入输出函数库*/
#include"stdlib.h" /*标准函数库*/
#include"string.h" /*字符串函数库*/
#include"conio.h" /*屏幕操作函数库*/
#define HEADER1 " ------------------------------------------------------------------------------------JOB----------------------------------------------------------------------------------------\n"
#define HEADER2 " | num | name | gender | idnum | college | department | unitofemployment | phonenum | \n"
#define HEADER3 " |-----------------|--------------|----------------|-------------------------|-----------------|--------------------|-------------------------------|----------------------------| \n"
#define FORMAT " | %-10s| %-10s | %-10s | %-20s | %-10s | %-10s | %-15s | %-20s | \n"
#define DATA p->num,p->name,p->gender,p->idnum,p->college,p->department,p->unitofemployment,p->phonenum
#define END " --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
#define N 100
int saveflag=0; /*是否需要存盘的标志变量*/
/*定义与就业系统有关的数据结构*/
typedef struct job /*标记为job*/
{
int num[10]; /*编号*/
char name[10]; /*姓名*/
char gender[10]; /*性别*/
char idnum[20]; /*身份证号*/
char college[20]; /*学院*/
char department[20]; /*系别*/
char unitofemployment[20]; /*就业单位*/
char phonenum[20]; /*电话号码*/
} JOB;
void menu() /*主菜单*/
{
system("cls"); /*调用DOS命令,清屏.与clrscr()功能相同*/
printf(" College Employment Management System\n");
printf(" ***************************Menu**************************\n");
printf(" * 1 input record 2 display record *\n");
printf(" * 3 delete record 4 search record *\n");
printf(" * 5 modify record 6 insert record *\n");
printf(" * 7 sort record 8 save record *\n");
printf(" * 0 quit system *\n");
printf(" *********************************************************\n");
}
/*格式化输出表头*/
void printheader()
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
/*格式化输出表中数据*/
void printdata(JOB pp)
{
JOB* p;
p=&pp;
printf(FORMAT,DATA);
}
void Wrong();
void Disp(JOB temp[],int n);
int Add(JOB temp[],int n);
int Del(JOB temp[],int n);
int Insert(JOB temp[],int n);
void Modify(JOB temp[],int n);
void SelectSort(JOB temp[],int n);
void Save(JOB temp[],int n);
void Qur(JOB temp[],int n);
void main()
{
JOB jb[N]; /*定义JOB结构体*/
int select; /*保存选择结果变量*/
char ch; /*保存(y,Y,n,N)*/
int count=0; /*保存文件中的记录条数(或元素个数)*/
getchar();
menu();
while(1)
{
system("cls");
menu();
printf("\n please Enter your choice(0~8):");
scanf("%d",&select);
if(select==0)
{
if(saveflag==1) /*若对数组的数据有修改且未进行存盘操作,则此标志为1*/
{getchar();
printf("\n==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(jb,count);
}
printf("\n===>thank you for useness!");
getchar();
break;
}
switch(select)
{
case 1:count=Add(jb,count);break; /*增加就业记录*/
case 2:system("cls");Disp(jb,count);getchar();break; /*显示就业记录*/
case 3:count=Del(jb,count);break; /*删除就业记录*/
case 4:Qur(jb,count);break; /*查询就业记录*/
case 5:Modify(jb,count);break; /*修改就业记录*/
case 6:count=Insert(jb,count);break; /*插入就业记录*/
case 7:SelectSort(jb,count);break; /*排序就业记录*/
case 8:Save(jb,count);break; /*保存就业记录*/
default:Wrong();getchar();break; /*按键有误,必须为数值0-9*/
}
}
}
/*显示*/
void Disp(JOB temp[],int n)
{
int i;
if(n==0)
{
printf("\n=====>Not job record!\n");
getchar();
return;
}
printf("\n\n");
printheader();
i=0;
while(i<n)
{
printdata(temp[i]);
i++;
printf("HEADER3");
}
getchar();
}
void Wrong()
{
printf("\n\n\n\n\n***********Error:innput has wrong! press any key to continue***********\n");
getchar();}
void Nofind()
{printf("\n=====>Not find this job record!\n");}
/*作用:用于定位数组中符合要求的记录*/
int Locate(JOB temp[],int n,char findmess[],char nameorphonenum[])
{
int i=0;
if(strcmp(nameorphonenum,"phonenum")==0) /*按电话号码查询*/
{
while(i<n)
{
if(strcmp(temp[i].phonenum,findmess)==0)
return i;
i++;}}
else if(strcmp(nameorphonenum,"name")==0)
{
while(i<n)
{
if(strcmp(temp[i].name,findmess)==0)
return i;
i++;}}
return -1;
}
/*输入字符串,并进行长度验证(长度<lens)*/
void stringinput(char *t,int lens,char *notice)
{
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中*/
}
/*增加工作记录*/
int Add(JOB temp[],int n)
{
char ch,num[20];
int i,flag=0;
system("cls");
Disp(temp,n);
while(1)
{
while(1)
{
stringinput(num,10,"intput number(press '0'return menu):");
flag=0;
if(strcmp(num,"0")==0)
{return n;}
i=0;
while(i<n)
{if(strcmp(temp[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(temp[n].num,num);
stringinput(temp[n].name,15,"Name:");
stringinput(temp[n].gender,15,"Gender:");
stringinput(temp[n].idnum,15,"Idnum:");
stringinput(temp[n].college,15,"College:");
stringinput(temp[n].department,15,"Department:");
stringinput(temp[n].unitofemployment,15,"Unitofemployment:");
stringinput(temp[n].phonenum,15,"Phonenum:");
saveflag=1;
n++;}
return n;}
/*删除工作记录*/
int Del(JOB temp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{ system("cls");
printf("\n=====>No job record!\n");
getchar();
return n;}
system("cls");
Disp(temp,n);
printf("\n =====>1 Delete by name =====>2 Delete by phone number\n");
printf(" Please choice[1,2]:");
scanf("%d",&sel);
if(sel==1){
stringinput(findmess,10,"input the existing name:");
p=Locate(temp,n,findmess,"name");
getchar();
if(p!=-1){
for(i=p+1;i<n;i++){
strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].gender,temp[i].gender);
strcpy(temp[i-1].idnum,temp[i].idnum);
strcpy(temp[i-1].college,temp[i].college);
strcpy(temp[i-1].department,temp[i].department);
strcpy(temp[i-1].unitofemployment,temp[i].unitofemployment);
strcpy(temp[i-1].phonenum,temp[i].phonenum);}
printf("\n==>delete success!\n");
n--;
getchar();
saveflag=1;}
else
Nofind();
getchar();}
else if(sel==2){
stringinput(findmess,15,"input the existing phone number:");
p=Locate(temp,n,findmess,"phonenum");
getchar();
if(p!=-1){
for(i=p+1;i<n;i++) {
strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].gender,temp[i].gender);
strcpy(temp[i-1].idnum,temp[i].idnum);
strcpy(temp[i-1].college,temp[i].college);
strcpy(temp[i-1].department,temp[i].department);
strcpy(temp[i-1].unitofemployment,temp[i].unitofemployment);
strcpy(temp[i-1].phonenum,temp[i].phonenum);}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;}
else
Nofind();
getchar();}
return n;}
/*按编号或姓名,查询工作记录*/
void Qur(JOB temp[],int n){
int select;
char searchinput[20];
int p=0;
if(n<=0){
system("cls");
printf("\n=====>No job record!\n");
getchar();
return;}
system("cls");
printf("\n =====>1 Search by name =====>2 Search by telephone number\n");
printf(" please choice[1,2]");
scanf("%d",&select);
if(select==1){
stringinput(searchinput,10,"input the existing name:");
p=Locate(temp,n,searchinput,"name");
if(p!=-1){
printheader();
printdata(temp[p]);
printf(END);
printf("press any key to return");
getchar();}
else
Nofind();
getchar();}
else if(select==2)
{
stringinput(searchinput,15,"input the existing telephone number");
p=Locate(temp,n,searchinput,"phonenum");
if(p!=-1)
{
printheader();
printdata(temp[p]);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else
Wrong();
getchar();
}
/*修改工作记录*/
void Modify(JOB temp[],int n){
char findmess[20];
int p=0;
if(n<=0)
{ system("cls");
printf("\n=====>No job record!\n");
getchar();
return ;}
system("cls");
printf("modify job recorder");
Disp(temp,n);
stringinput(findmess,10,"input the existing name:");
p=Locate(temp,n,findmess,"name");
if(p!=-1){
printf("Number:%s,\n",temp[p].num);
printf("Name:%s,\n",temp[p].name);
stringinput(temp[p].name,15,"input new name:");
printf("Name:%s,",temp[p].gender);
stringinput(temp[p].gender,15,"input new gender:");
printf("Name:%s,",temp[p].idnum);
stringinput(temp[p].idnum,20,"input new idnum:");
printf("Name:%s,",temp[p].college);
stringinput(temp[p].college,20,"input new college:");
printf("Name:%s,",temp[p].department);
stringinput(temp[p].department,30,"input new department:");
printf("Name:%s,",temp[p].unitofemployment);
stringinput(temp[p].unitofemployment,30,"input new unitofemployment:");
printf("Name:%s,",temp[p].phonenum);
stringinput(temp[p].phonenum,15,"input new phonenum:");
printf("\n=====>modify success!\n");
getchar();
Disp(temp,n);
getchar();
saveflag=1;}
else
{Nofind();
getchar();}
return ;}
/*插入记录*/
int Insert(JOB temp[],int n)
{
char ch,num [10],s[10];
JOB newinfo;
int flag=0,i=0,kkk=0;
system("cls");
Disp(temp,n);
while(1)
{ stringinput(s,10,"please input insert location after the Number:");
flag=0;i=0;
while(i<n){
if(strcmp(temp[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;}}}
while(1)
{ stringinput(num,10,"input new Number:");
i=0;flag=0;
while(i<n) {
if(strcmp(temp[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:");
stringinput(newinfo.gender,15,"Gender:");
stringinput(newinfo.idnum,20,"Idnum:");
stringinput(newinfo.college,15,"College:");
stringinput(newinfo.department,15,"Department:");
stringinput(newinfo.unitofemployment,15,"Unitofemployment:");
stringinput(newinfo.phonenum,15,"Phonenum:");
saveflag=1; /*在main()有对该全局变量的判断,若为1,则进行存盘操作*/
for (i=n-1;i>kkk;i--) /*从最后一个组织元素开始向前移一个元素位置*/
{
strcpy(temp[i+1].num,temp[i].num);
strcpy(temp[i+1].name,temp[i].name);
strcpy(temp[i+1].gender,temp[i].gender);
strcpy(temp[i+1].idnum,temp[i].idnum);
strcpy(temp[i+1].college,temp[i].college);
strcpy(temp[i+1].department,temp[i].department);
strcpy(temp[i+1].unitofemployment,temp[i].unitofemployment);
strcpy(temp[i+1].phonenum,temp[i].phonenum);
}
strcpy(temp[kkk+1].num,newinfo.num); /*在kkk的元素位置后插入新纪录*/
strcpy(temp[kkk+1].name,newinfo.name);
strcpy(temp[kkk+1].gender,newinfo.gender);
strcpy(temp[kkk+1].idnum,newinfo.idnum);
strcpy(temp[kkk+1].college,newinfo.college);
strcpy(temp[kkk+1].department,newinfo.department);
strcpy(temp[kkk+1].unitofemployment,newinfo.unitofemployment);
strcpy(temp[kkk+1].phonenum,newinfo.phonenum);
n++;
Disp(temp,n);
printf("\n\n");
getchar();
return n;
}
void SelectSort(JOB temp[],int n)/*利用选择排序法实现数组的按记录编号或姓名的升序排序*/
{
int i=0,j=0,flag=0,indexmin,select;
char charflag[10];
JOB newinfo;
if(n<=0)
{ system("cls");
printf("\n=====>Not job record!\n");
getchar();
return ;}
system("cls");
Disp(temp,n); /*显示排序前的所有记录*/
printf(" ==>1 SORT BY NUMBER ==>2 SORT BY NAME\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按记录编号排序*/
{
for(i=0;i<n-1;i++)
{
flag=32767;indexmin=0;
for(j=i;j<n;j++)
{ if(atoi(temp[j].num)<flag)/*atoi函数的参数为一个数字字符串,返回值是该数字字符串的数值,如atoi("123〞)返回123,.cpp源程序需包含stdlib.h头文件*/
{ flag=atoi(temp[j].num);
indexmin=j;
}
}
strcpy(newinfo.num,temp[i].num); /*利用结构变量newinfo实现数组元素的交换*/
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.gender,temp[i].gender);
strcpy(newinfo.idnum,temp[i].idnum);
strcpy(newinfo.college,temp[i].college);
strcpy(newinfo.department,temp[i].department);
strcpy(newinfo.unitofemployment,temp[i].unitofemployment);
strcpy(newinfo.phonenum,temp[i].phonenum);
strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].gender,temp[indexmin].gender);
strcpy(temp[i].idnum,temp[indexmin].idnum);
strcpy(temp[i].college,temp[indexmin].college);
strcpy(temp[i].department,temp[indexmin].department);
strcpy(temp[i].unitofemployment,temp[indexmin].unitofemployment);
strcpy(temp[i].phonenum,temp[indexmin].phonenum);
strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].gender,newinfo.gender);
strcpy(temp[indexmin].idnum,newinfo.idnum);
strcpy(temp[indexmin].college,newinfo.college);
strcpy(temp[indexmin].department,newinfo.department);
strcpy(temp[indexmin].unitofemployment,newinfo.unitofemployment);
strcpy(temp[indexmin].phonenum,newinfo.phonenum);
}
Disp(temp,n); /*显示排序后的所有记录*/
saveflag=1;
printf("\n =====>sort complete!\n");
getchar();
return;
}
else if(select==2)
{
for(i=0;i<n-1;i++)
{
strcpy(charflag,"");indexmin=0;
for(j=i;j<n;j++)
{ if(strcmp(temp[j].name,charflag)>0)
{ strcpy(charflag,temp[j].name);
indexmin=j;}}
strcpy(newinfo.num,temp[i].num); /*利用数据结构变量newinfo实现数组元素的交换*/
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.gender,temp[i].gender);
strcpy(newinfo.idnum,temp[i].idnum);
strcpy(newinfo.college,temp[i].college);
strcpy(newinfo.department,temp[i].department);
strcpy(newinfo.unitofemployment,temp[i].unitofemployment);
strcpy(newinfo.phonenum,temp[i].phonenum);
strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].gender,temp[indexmin].gender);
strcpy(temp[i].idnum,temp[indexmin].idnum);
strcpy(temp[i].college,temp[indexmin].college);
strcpy(temp[i].department,temp[indexmin].department);
strcpy(temp[i].unitofemployment,temp[indexmin].unitofemployment);
strcpy(temp[i].phonenum,temp[indexmin].phonenum);
strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].gender,newinfo.gender);
strcpy(temp[indexmin].idnum,newinfo.idnum);
strcpy(temp[indexmin].college,newinfo.college);
strcpy(temp[indexmin].department,newinfo.department);
strcpy(temp[indexmin].unitofemployment,newinfo.unitofemployment);
strcpy(temp[indexmin].phonenum,newinfo.phonenum);
}
Disp(temp,n); /*显示排序后的所有记录*/
saveflag=1;
printf("\n =====>sort complete!");
getchar();
return;
}
else
{Wrong();
getchar();
getchar();
return;}
}
/*数据存盘,若用户没有专门进行此操作且对数据有修改,在退出系统时,会提示用户存盘*/
void Save(JOB temp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("c:\\job","w"); /*以只写方式打开文本文件*/
if(fp==NULL) /*打开文件失败*/
{printf("\n=====>open file error!\n");
getchar();
return ;}
for(i=0;i<n;i++){
if(fwrite(&temp[i],sizeof(JOB),1,fp)==1) /*每次写一条记录或一个结构数组元素至文件*/
{ continue;}
else
{break;}}
if(i>0){
getchar();
printf("\n\n=====>save file complete,total saved's record number is:%d\n",i);
getchar();
saveflag=0;}
else
{system("cls");
printf("the current link is empty,no job record is saved!\n");
getchar();}
fclose(fp); /*关闭此文件*/
}