因为自己的操作系统是linux,windows除了迫不得已一般不会开机,而且电脑显卡坏了,windows就更没有开的必要了,但是网上的相关教程中往往是windows环境的,很多头文件linux下没有,而且很多还是指定TC。一怒之下,就自己写了个通讯录。若有错误,欢迎指正。
/*******************************************************
> File Name: tongxunlu.c
> Author: heathcliff
> Mail: ----------------
> Created Time: Wed 23 Mar 2016 02:21:27 PM EDT
*******************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 50
/*define name,tele number and address of contacts
*/
typedef struct
{
char name[20];
char tele_number[11];
char address[30];
}Address;
int contents();
int Enter(Address t[]);
void list(Address t[],int n);
void search(Address t[],int n);
int Delete(Address t[],int n);
int Save(Address t[],int n);
int Add(Address t[],int n);
int Load(Address t[]);
int Find(char name[20],Address t[],int n);
void Exit();
void sort(Address t[],int n);
int main(void)
{
int length; //记录工作长度
Address adr[M];
char name[20];
int j;
for(;;){//无线循环switch
switch( contents() ){
case 0:length = Enter(adr);break;
case 1:list(adr,length);break;
case 2:search(adr,length);break;
case 3:length = Delete(adr,length);break;
case 4:length = Add(adr,length);break;
case 5:length = Save(adr,length);break;
case 6:length = Load(adr);break;
case 7:sort(adr,length);break;
case 8:Exit();break;
default:Exit();break;
}
}
return 0;
}
int contents()
{
printf("**********************************************\n");
printf("********** 0.Enter record ********************\n");
printf("********** 1.List the File *******************\n");
printf("********** 2.Search record on name ***********\n");
printf("********** 3.Delete a record *****************\n");
printf("********** 4.add record **********************\n");
printf("********** 5.Save the file *******************\n");
printf("********** 6.Load the file *******************\n");
printf("********** 7.sort to make new file ***********\n");
printf("********** 8.Quit ***************************\n");
printf("**********************************************\n");
int n;
do{
printf("please input the key you what to select....\n");
scanf("%d",&n);
}while(n < 0 || n > 11);
return n;
}
int Enter(Address t[])
{
int i=0;
int number; //定义记录条数
printf("Please input num:\n");
scanf("%d",&number);
printf("Please input record:\n");
printf("nameaddress tele_number\n");
printf("-----------begin---------------\n");
for(i;i<number;i++){
scanf("%s%s%s",t[i].name,t[i].address,t[i].tele_number);
printf("-----------end--------------------\n");
}
return number;
}
void list(Address t[],int n)
{
int i;
printf("**************************\n");
printf("name address tele_number\n");
printf("----------beginlist----------------------------------------\n");
for(i=0;i<n;i++){
printf("%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
}
printf("---------endlist-------------------------------------------\n");
}
void search(Address t[],int n)
{
char name[20];
int i;
printf("Please input name you want to search: \n");
scanf("%s",name);
i = Find(name,t,n); //问题,除第0个参数外,其他均为-1。,尚未解决
printf("%d\n",i);
if(i >= 0){
printf("----------beginsearch----------------------\n");
printf("%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
printf("---------endsearch-------------------------\n");
}else printf("Not found\n");
}
void print(Address temp)
{
printf("-----------------hajime------------------------------------\n");
printf("%-20s%-30s%-10s\n",temp.name,temp.address,temp.tele_number);
printf("-------------------end-------------------------------------\n");
}
int Delete(Address t[],int n)
{
char name[20];
int i,j;
printf("Please input name you want to delete: \n");
scanf("%s",name);
for(i=0;i<n;i++){
if(strcmp(t[i].name,name) == 0){
printf("-----------------------------------------------------------\n");
printf("%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
printf("-----------------------------------------------------------\n");
for(j=i+1;j<n;j++){ //data front and cover origin data
strcpy(t[j-1].name ,t[j].name);
strcpy(t[j-1].address ,t[j].address);
strcpy(t[j-1].tele_number ,t[j].tele_number);
}
n--;
printf("The data has been deleted\n");
}
else{
printf("Not found,please try again\n");
}
}
return n;
}
int Save(Address t[],int n)
{
FILE *fp;
int i;
/* judge document */
if((fp = fopen("tongxunlu","wb")) == NULL){
printf("Cannot open file!,please try again \n");
exit(1);
}
fprintf(fp,"写入的名字数目为:");
fprintf(fp,"%d\n",n); //写入记录数
printf("This is the file you want to save\n");
for(i=0;i<n;i++){
printf("-----------------------------------------------------------\n");
printf("%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
printf("-----------------------------------------------------------\n");
}
fprintf(fp,"--------------------------------------------------------\n");
for(i = 0;i<n;i++){
fprintf(fp,"%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
fprintf(fp,"--------------------------------------------------------\n");
}
fclose(fp);//close File
printf("\n\n************save success*********\n");
return n;
}
void Exit()
{
exit(1);
}
int Add(Address t[],int n)
{
Address temp;
int i,j;
char s[20];
printf("Please input record: \n");
printf("------------------------------------------\n");
printf("Nameaddress tele_number\n");
printf("-------------------------------------------\n");
scanf("%s%s%s",temp.name,temp.address,temp.tele_number);
printf("-------------------------------------------\n");
printf("please input locate name:\n");//设定在哪里输入新名字
scanf("%s",s);
/*显示输入的名字的相关信息*/
for(i=0;i<n;i++){ //在i的位置进行插入
if(strcmp(t[i].name,s) == 0){
printf("-----------------------------------------------------------\n");
printf("%-20s%-30s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
printf("-----------------------------------------------------------\n");
}
}
for(j=n-1;j>=i;j--){ //从插入位置向后移动一格
strcpy(t[j+1].name , t[j].name);
strcpy(t[j+1].address , t[j].address);
strcpy(t[j+1].tele_number , t[j].tele_number);
}
/*为i处赋值*/
strcpy(t[i].name,temp.name);
strcpy(t[i].address,temp.address);
strcpy(t[i].tele_number,temp.tele_number);
n++;//向后移动
return n;
}
int Load(Address t[]) //此函数因未知原因不能发挥作用,这里的函数名应该改为int Load(Address t[],int n,这样就可以将save中的n传入
{
int i,n;
FILE *fp;
if((fp = fopen("tongxunlu","rb")) == NULL){ //open File
printf("Cannot open file,try again\n");
exit(0);
}
fscanf(fp,"%d",&n); //读入记录数,根据调试显示
printf("%d\n",n);
for(i=0;i<n;i++){
fscanf(fp,"%s%s%s",t[i].name,t[i].address,t[i].tele_number);
}
fclose(fp);
printf("your file load success\n");
printf("your file is as follow list\n");
for(i=0;i<n;i++){
printf("-----------beginLoad--------------------\n");
printf("%-30s%-20s%-10s\n",t[i].name,t[i].address,t[i].tele_number);
printf("-----------endLoad--------------------\n");
}
return n;
}
void sort(Address t[],int n) //排序
{
int i,j;
int flag = 1;
Address temp;
while(flag == 1){
for(i = 0; i<n-1;i++)
if((strcmp(t[i].name ,t[i+1].name)) > 0){ //比较大小
strcpy(temp.name , t[i].name);
strcpy(t[i].name , t[i+1].name);
strcpy(t[i+1].name , temp.name);
strcpy(temp.address , t[i].address);
strcpy(t[i].address , t[i+1].address);
strcpy(t[i+1].address , temp.address);
strcpy(temp.tele_number , t[i].tele_number);
strcpy(t[i].tele_number , t[i+1].tele_number);
strcpy(t[i+1].tele_number , temp.tele_number);
}
flag = 0; //执行排序完成后中断
}
printf("************sort success*************\n");
printf("\nThis is the new sort after sort\n");
for(j=0;j<n;j++){
printf("-----------begin--------------------\n");
printf("%-30s%-20s%-10s\n",t[j].name,t[j].address,t[j].tele_number);
printf("-----------end--------------------\n");
}
}
int Find(char nme[20],Address t[],int n)
{
int i;
for(i=0;i<n;i++){
if(strcmp(t[i].name,nme) == 0){
return i;
}
}
return -1;
}