#include<stdio.h>
#include<string.h>
#define MAX_PARTS 35
#define LEN 15
struct part{
char name[LEN];
char id[LEN];
char email[LEN];
}student[MAX_PARTS];
int i=0;//全局变量i,用于记录录入信息的条数
void Find(void);//函数声明
void Insert(void);
void Delete(void);
void Sort(void);
void Print(void);
int main(void)
{
int command;
for(;;){
printf("Enter command:");//指令调用
scanf("%d",&command);
if(i>34) {
printf("it's full");
}
switch(command){
case 0:Find();
break;//函数调用
case 1:Insert();
break;
case 2:Delete();
break;
case 3:Sort();
break;
case 4:Print();
break;
case 5:return 0;
default: printf("Illegal aommands\n");
}
printf("\n");
}
}
void Find(void){//查找
char c[LEN];
int a=0;
printf("Enter names or students_id or email address:");
scanf("%s",&c);
for(a;a<=i;a++){
if(strcmp(c,student[a].name)==0||strcmp(c,student[a].id)==0||strcmp(c,student[a].email)==0)
{
printf("%-15d",a);
printf("%-15s",student[a].name);
printf("%-15s",student[a].id);
printf("%-15s",student[a].email);
break;
}
}
if(a>i)
printf("No such record");
}
void Insert(void){//录入
printf("Enter name:\n");
scanf("%s",&student[i].name);
printf("Enter student id:\n");
scanf("%s",&student[i].id);
printf("Enter email:\n");
scanf("%s",&student[i].email);
i++;
}
void Delete(void){//删除
int a,b;
printf("Enter index of record to be deleted<0-%d>:",i-1);
scanf("%d",&a);
b=a+1;
for(b;b<=i;b++){
strcpy(student[a].name,student[b].name);//用后面的将前面的覆盖
strcpy(student[a].id,student[b].id);
strcpy(student[a].email,student[b].email);
a++;
}
i--;
}
void Sort(void){//就是它有问题==
int a=0,b=0;
char c[LEN];
for(a=0;a<=i;a++){
strcpy(c,student[0].id);
for(b=0;b<=i-a;b++)
if(strcmp(c,student[b].id)<=0)
strcpy(c,student[b].id);
for(b=0;b<i-a;b++)
if(student[b].id==c)
break;
strcpy(student[b].id,student[i-a].id);
strcpy(student[i-a].id,c);
}
printf("Sort data successful!");
}
void Print(void){//输出
int x=0;
char a[LEN]="Index",b[LEN]="Name",c[LEN]="Student Id",d[LEN]="Email";
printf("%-15s",a);
printf("%-15s",b);
printf("%-15s",c);
printf("%-15s\n",d);
for(x;x<i;x++){
printf("%-15d",x);
printf("%-15s",student[x].name);
printf("%-15s",student[x].id);
printf("%-15s\n",student[x].email);
}
}
[
本帖最后由 黑漆漆 于 2014-12-25 11:13 编辑 ]