Created
June 21, 2014 09:58
-
-
Save kaleocheng/ccedf0e365987eb464a9 to your computer and use it in GitHub Desktop.
SeqList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//***********顺序表实现的电话簿程序*************************// | |
/* | |
*本程序为练习顺序表所写,仅实现了最基本的几个功能:添加,删除,查找联系人. | |
* 很多细节并未实现,如检验姓名,和手机号码是否合法,对重复姓名联系人的处理,按时间先后排序,按字母先后排序等. | |
* 程旭 | |
* [email protected] | |
*/ | |
#include<stdio.h> | |
#include<string.h> | |
#define MAX 20 | |
#define MAX_ID_NUM 15 | |
#define MAX_NAME 20 | |
#define MAX_MOBILE_NUM 15 | |
#define MAX_TEL_NUM 15 | |
//*******储存学生信息的结点************// | |
typedef struct | |
{ | |
char idNum[MAX_ID_NUM]; | |
char userName[MAX_NAME]; | |
char mobileNum[MAX_MOBILE_NUM]; | |
char telNum[MAX_TEL_NUM]; | |
}Node; | |
//********顺序表的构建****************// | |
typedef struct | |
{ | |
int length; | |
Node data[MAX]; | |
}SeqList; | |
//######### 函数申明 ###############// | |
void print(SeqList aSeqList); | |
int find(char userName[],SeqList *aSeqList); | |
int add(char idNum[],char userName[],char mobileNum[],char telNum[],SeqList *aSeqList); | |
int del(char userName[],SeqList *aSeqList); | |
int insert(char idNum[],char userName[],char mobileNum[],char telNum[],int insertNum,SeqList *aSeqList) | |
//##################################// | |
//***print() 遍历顺序表并打印出学生信息****// | |
void print(SeqList aSeqList) | |
{ | |
int i = 0; | |
if(i == aSeqList.length) | |
{ | |
printf("Empty\n"); | |
}else{ | |
printf("IdNum UserName MobileNum TelNum \n"); | |
for(;i <= aSeqList.length-1; i++) | |
{ | |
printf("%-15s%-20s%-15s%-15s\n", | |
aSeqList.data[i].idNum,aSeqList.data[i].userName,aSeqList.data[i].mobileNum,aSeqList.data[i].telNum); | |
} | |
} | |
} | |
//*****find() 查找学生在表中的位置*************// | |
int find(char userName[],SeqList *aSeqList) | |
{ | |
int i = 0; | |
while( strcmp(aSeqList->data[i].userName,userName) && i<= aSeqList->length-1) | |
{ | |
i++; | |
} | |
if(i > aSeqList->length-1) | |
{ | |
printf("Can't find %s\n",userName); | |
return 0; | |
} | |
return i+1; | |
} | |
//*****add() 向表中添加新的学生信息*********// | |
int add(char idNum[],char userName[],char mobileNum[],char telNum[],SeqList *aSeqList) | |
{ | |
aSeqList->length++; | |
strcpy(aSeqList->data[aSeqList->length-1].idNum,idNum); | |
strcpy(aSeqList->data[aSeqList->length-1].userName,userName); | |
strcpy(aSeqList->data[aSeqList->length-1].mobileNum,mobileNum); | |
strcpy(aSeqList->data[aSeqList->length-1].telNum,telNum); | |
return 1; | |
} | |
//*****del() 删除表中指定学生的信息*********// | |
int del(char userName[],SeqList *aSeqList) | |
{ | |
int i =( find(userName,aSeqList)); | |
if( !i ) | |
return 0; | |
printf("you want del the %s ?\n",userName); | |
if(i == aSeqList->length) | |
{ | |
aSeqList->length--; | |
return 1; | |
}else | |
{ | |
for(;i <= aSeqList->length;i++) | |
{ | |
strcpy(aSeqList->data[i-1].idNum,aSeqList->data[i].idNum); | |
strcpy(aSeqList->data[i-1].userName,aSeqList->data[i].userName); | |
strcpy(aSeqList->data[i-1].mobileNum,aSeqList->data[i].mobileNum); | |
strcpy(aSeqList->data[i-1].telNum,aSeqList->data[i].telNum); | |
} | |
aSeqList->length--; | |
return 1; | |
} | |
} | |
//******insert() 在表中指定位置加入新的学生信息*******// | |
int insert(char idNum[],char userName[],char mobileNum[],char telNum[],int insertNum,SeqList *aSeqList) | |
{ | |
int i; | |
#if 0 | |
if(insertNum > aSeqList->length+1 || insertNum <= 0) | |
{ | |
printf("You can not insert there\n"); | |
return 0; | |
} | |
#endif | |
aSeqList->length++; | |
for(i = aSeqList->length-1;i > insertNum-1;i--) | |
{ | |
strcpy(aSeqList->data[i].idNum,aSeqList->data[i-1].idNum); | |
strcpy(aSeqList->data[i].userName,aSeqList->data[i-1].userName); | |
strcpy(aSeqList->data[i].mobileNum,aSeqList->data[i-1].mobileNum); | |
strcpy(aSeqList->data[i].telNum,aSeqList->data[i-1].telNum); | |
} | |
strcpy(aSeqList->data[insertNum-1].idNum,idNum); | |
strcpy(aSeqList->data[insertNum-1].userName,userName); | |
strcpy(aSeqList->data[insertNum-1].mobileNum,mobileNum); | |
strcpy(aSeqList->data[insertNum-1].telNum,telNum); | |
return 1; | |
} | |
//********main() 打印相应的菜单界面*********// | |
int main() | |
{ | |
SeqList TheSeqList={0}; | |
char idNum[MAX_ID_NUM],userName[MAX_NAME],mobileNum[MAX_MOBILE_NUM],telNum[MAX_TEL_NUM]; | |
int option,i,insertNum; | |
printf("#########电话本#########\n"); | |
printf("请选择相应的操作:\n"); | |
printf("1.显示当前联系人\n"); | |
printf("2.新加联系人\n"); | |
printf("3.删除联系人\n"); | |
printf("4.搜索联系人\n"); | |
printf("5.插入联系人\n"); | |
printf("6.打印菜单\n"); | |
printf("######################\n"); | |
while(1) | |
{ | |
switch(option = getchar()) | |
{ | |
case '1' : | |
print(TheSeqList); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
break; | |
case '2' : | |
printf("\n请输入学生学号:"); | |
scanf("%s",idNum); | |
printf("\n请输入学生姓名:"); | |
scanf("%s",userName); | |
printf("\n请输入学生的手机号码:"); | |
scanf("%s",mobileNum); | |
printf("\n请输入学生的固定电话号码:"); | |
scanf("%s",telNum); | |
if(add(idNum,userName,mobileNum,telNum,&TheSeqList)) | |
printf("添加 %s 至通讯录 \n",userName); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
break; | |
case '3' : | |
printf("\n请输入要删除的联系人姓名\n"); | |
scanf("%s",userName); | |
if(del(userName,&TheSeqList)) | |
printf("删除成功\n"); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
break; | |
case '4' : | |
printf("\n请输入要查找的联系人姓名:\n"); | |
scanf("%s",userName); | |
if( i = find(userName,&TheSeqList)) | |
printf("您要查找的信息:\n 学号:%s 姓名:%s 手机:%s 固定电话:%s \n", | |
TheSeqList.data[i-1].idNum,TheSeqList.data[i-1].userName,TheSeqList.data[i-1].mobileNum,TheSeqList.data[i-1].telNum); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
break; | |
case '5' : | |
printf("\n请输入要插入的位置:"); | |
scanf("%d",&insertNum); | |
if(insertNum > TheSeqList.length+1 || insertNum <= 0) | |
{ | |
printf("You can not insert there\n"); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
}else{ | |
printf("\n请输入学生学号:"); | |
scanf("%s",idNum); | |
printf("\n请输入学生姓名:"); | |
scanf("%s",userName); | |
printf("\n请输入学生的手机号码:"); | |
scanf("%s",mobileNum); | |
printf("\n请输入学生的固定电话号码:"); | |
scanf("%s",telNum); | |
if(insert(idNum,userName,mobileNum,telNum,insertNum,&TheSeqList)) | |
printf("添加 %s 至通讯录 \n",userName); | |
printf("\n#请输入要进行的操作(按6打印菜单):"); | |
} | |
break; | |
case '6' : | |
printf("1.显示当前联系人\n"); | |
printf("2.新加联系人\n"); | |
printf("3.删除联系人\n"); | |
printf("4.搜索联系人\n"); | |
printf("5.插入联系人\n"); | |
printf("\n(请输入要进行的操作:)"); | |
break; | |
default : | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment