c-通讯录

linux下的电话通讯录

在linux下使用c写了一个电话通讯录,整体采用链表存储所有数据(优化可以加数据库使用),没有使用数据库就是导致每次重新启动程序,之前创建的通讯录数据就会消失,保存不下来。使用系统函数例如strcpy,strcmp,strlen等都是通过自我编写函数实现
在这里插入代码片struct student *allfree(struct student *head)
{
while(head->next != NULL)
{
struct Node *ptr = head;
head = head->next;
free(ptr);
}
free(head);
head = NULL;
return head;
}

int create(struct student *head)
{
struct student *p;
p = head;
struct student *newstudent = (struct student *)malloc(sizeof(struct student));
if(NULL == newstudent)
{
return -1;
}
// lu ru xinxi

printf("please input xinxi:");
srand((unsigned)time(NULL));
newstudent->id = rand() % 100;  // create one  suijishu
printf("%d\n",newstudent->id);
printf("name :\n");
scanf("%s",newstudent->name);
printf("phone_number:\n");
scanf("%s",newstudent->phone_number);
printf("home address:\n");
scanf("%s",newstudent->home_address);
printf("company number :\n");
scanf("%s",newstudent->company_number);


newstudent->next = NULL;
while(head->next != NULL)
{
head = head->next;
}
head->next = newstudent;
getchar();

printf("  create!  \n");
head = p;

}

void delete(struct student *head)
{
struct student *p;
p = head;
char name[20];
int count = 0;
printf(“please input your want delete one name:\n”);
scanf("%s",name);

while(head->next != NULL)
{
if(my_strcmp(head->next->name,name) == 0)
{
    count++;
    struct student *ptr = head->next;
    head->next = ptr->next;
    free(ptr);
}
}
if(count == 0)
{
printf("No this one name!\n");
return -1;
}
else
{
printf("delete is ok!\n");
}

head = p;
printf("\n");
return 0;

}

void display(struct student *head)
{
struct student *p;
p = head;

if(head == NULL)
{
printf("No this people:");
return -1;
}
while(head->next != NULL)
{
output(head);
head = head->next;
}
head = p;    //fan hui toujiedian

}

int init(struct student **head)
{
struct student *newnode = (struct student *)malloc(sizeof(struct student));
if(NULL == newnode)
{
return -1;
}
//newnode->value = 0;
newnode->next = NULL;
*head = newnode;
return 0;
}

int land()
{
int i = 3;
char ch[] = {" 211 “};
char input[20] = { 0 };
while(i > 0)
{
printf(“please input your password:\n”);
scanf(”%s",input);
if(my_strcmp(ch,input == 0))
{
return 0;
}
else
{
i–;
printf(“password error!,The remaining number of times is:%d\n”,i);
}
}
if(i == 0)
{
printf(“over!”);
return -1;
}
return 0;
}

int login() //登录界面
{
printf("\033[0;36m++\033[0m\n");
printf("\033[0;36m| |\033[0m\n");
printf("\033[0;36m| 欢迎来到何冬冬的通讯录 |\033[0m\n");
printf("\033[0;36m| |\033[0m\n");
printf("\033[0;36m| 温馨提示:初始密码(211) |\033[0m\n");
printf("\033[0;36m+
+\033[0m\n");

int code = land();
if (code == 0)                        //密码正确,则给出提示
{
    printf("\33[0;35m登录成功了!!你真帅!!!\33[0m\n");

}
else if (code == -1)                  //如果三次输出密码均错误,则给出提示,并返回-1
{
    printf("\33[0;31m输入次数已经用尽,系统将自动退出!\33[0m\n");
    return(-1);
}
return 0;

}

void menu()
{
printf(“\n");
printf("-------1.creat people xinxi--------\n");
printf("-------2.delete people xinxi-------\n");
printf("-------3.seach people xinxi-------\n");
printf("-------4.display people xinxi-------\n");
printf("-------5. exit tongxunlu ! -------\n");
printf("
\n”);
printf(“please input your wants ? :\n”);
}

int my_strcmp(const char *dest, const char *src) //字符串比较
{
int temp = 0;

if(dest==NULL || src==NULL)                //判断两字符串是否存在NULL,若存在则返回NULL
{
    return NULL;
}  

//获得两字符串的首地址
const char *dest_t = dest;
const char *src_t = src;

while(*dest_t!= '\0' && *src_t !='\0')       //进行两字符串的逐个比较
{
    temp = *dest_t-*src_t;
    if(temp != 0)                           //如果出现不同的字母,则退出,并记录两字母ASCII码差值
    {
        break;
    }
    dest_t++;                               
    src_t++;
}

//若temp=0,并且两字符串长度不同,则再进行一次比较
if(((*dest_t!= '\0' && *src_t =='\0') || (*dest_t== '\0' && *src_t !='\0')) && temp == 0)
{
    temp=dest_t-src_t;
}

return temp;

}

void output(struct student *head) //显示全部信息
{
printf(“id = %d\n”,head->next->id);
printf(“name = %s\n”,head->next->name);
printf(“phone_number = %s\n”,head->next->phone_number);
printf(“home address = %s\n”,head->next->home_address);
printf(“company number = %s\n”,head->next->company_number);
}

void search(struct student *head)
{
struct student *p;
p = head;
int count = 0;
char name[30];
printf(“please input you want search name:\n “);
scanf(”%s”,name);
while(head->next != NULL)
{
if(my_strcmp(head->next->name,name) == 0)
{
count++;
output(head);
}
head = head->next;
}
if(count == 0)
{
printf(“No search this people!\n”);
printf(“Search error!\n”);
}
printf("\n");
head = p;

}

char *my_strcpy(char *src, const char dst)
{
int i = 0;
if(src == NULL || dst == NULL)
{
return 0;
}
while(
(dst+i) != ‘\0’)
{
*(src+i) = *(dst+i);
i++;
}
*(src+i) = ‘\0’;
return src;
}

int my_strlen(char *str)
{
if(str == NULL)
{
return 0;
}
//printf(“A\n”);
int count =0;
while(*str++ != ‘\0’)
{
count++;
}
return count;
}

int main()
{
//if(login() == (-1))
//{
// return 0;
// }
struct student *head;
init(&head);
int temp = 0;
char decide = ‘N’;
while(1)
{
temp = 0;
menu();
scanf("%d",&temp);
switch(temp)
{
case 1:
while(1)
{
decide = ‘N’;
create(head);
printf(“is jixu create?(Y/N)\n”);
scanf("%c",&decide);
if(decide == ‘N’)
{
break;
}
}
break;
case 2:
while(1)
{
decide = ‘N’;
delete(head);
printf(“is jixu delete?(Y/N)\n”);
scanf("%c",&decide);
if(decide == ‘N’)
{
break;
}
}
break;
case 3:
while(1)
{
decide = ‘N’;
search(head);
printf(“is jixu search? (Y/N)\n”);
getchar();
scanf("%c",&decide);
if(decide == ‘N’)
{
break;
}
}
break;
case 4:
display(head);
break;
case 5:
exit(-1);
default:
printf(“input is error!\n”);
break;
}

}
struct student *allfree(head);
return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值