zoukankan      html  css  js  c++  java
  • C语言实现通讯录

    <span style="font-size:18px;">#include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<conio.h>
    #define LEN sizeof(struct ab)
    #define ZIP 7
    #define PHONE 7
    #define MAX 100
    struct ab
    {
    	char name[10];
    	char addr[10];
    	char zip[ZIP];
    	char phone[PHONE];
    	struct ab *next;
    };
    struct ab *head;
    void search(struct ab *head);
    struct ab *add(struct ab *head);
    struct ab *del(struct ab *head);
    void alter(struct ab *head);
    void print(struct ab *head);
    void thefirst();
    int n;</span>
    <span style="font-size:18px;">#include"head.h"
    void main()
    {
    	n=0;
    	head=(struct ab *)malloc(LEN);
    	thefirst();
    }
    void thefirst()
    {
    	char c;
    	system("CLS");
    	puts("**************************************");
    	puts("                 通讯录               ");
    	puts("                1、查询               ");
    	puts("                2、加入               ");
    	puts("                3、删除               ");
    	puts("                4、改动               ");
    	puts("                5、显示               ");
    	puts("                6、返回               ");
    	puts("**************************************");
    	c=getch();
    	system("CLS");
    	switch(c)
    	{
    		case '1':search(head);break;
    		case '2':head=add(head);thefirst();break;
    		case '3':head=del(head);thefirst();break;
    		case '4':alter(head);break;
    		case '5':print(head);break;
    		case '6':printf("BYE BYE!
    ");
    	}
    }</span>

    <span style="font-size:18px;">#include"head.h"
    void search(struct ab *head)
    {
    	char str[10];
    	struct ab *p;
    	p=head;
    	printf("please input the name you want to search!
    ");
    	scanf("%s",str);
    	if(p==NULL)
    	{
    		printf("please input the information first!press any key to thefirst 
    ");
    		getch();
    		thefirst();
    	}
    	system("CLS");
    	for(;p&&strcmp(str,p->name);p=p->next);
    	if(p)
    		printf("%10s
    %10s
    %10s
    %10s
    ",p->name,p->addr,p->zip,p->phone);
    	else
    		printf("there is no information about the people you want!
    ");
    	getch();
    	thefirst();
    }</span>

    <span style="font-size:18px;">#include"head.h"
    struct ab *add(struct ab *head)
    {
    	struct ab *p1,*p2,*p3;
    	char c[10];
    	printf("please input the name!
    ");
    	scanf("%s",c);
    	p1=head;
    	if(n==0)
    	{
    		strcpy(p1->name,c);
    		printf("please input the address!
    ");
    		scanf("%s",p1->addr);
    		printf("please input the zip!
    ");
    		scanf("%s",p1->zip);
    		printf("please input the phone!
    ");
    		scanf("%s",p1->phone);
    		printf("%s
    ",p1->phone);
    		p1->next=NULL;
    		n++;
    	}
    	else
    	{	
    		p3=(struct ab *)malloc(LEN);
    		strcpy(p3->name,c);
    		printf("please input the address!
    ");
    		scanf("%s",p3->addr);
    		printf("please input the zip!
    ");
    		scanf("%s",p3->zip);
    		printf("please input the phone!
    ");
    		scanf("%s",p3->phone);
    		if(strcmp(c,p1->name)<0)
    		{
    			head=p3;
    			p3->next=p1;
    		}
    		else
    		{
    			for(;p1&&strcmp(c,p1->name)>0;p2=p1,p1=p1->next);
    			if(p1==NULL)
    			{
    				p2->next=p3;
    				p3->next=NULL;
    			}
    			else
    			{
    				p2->next=p3;
    				p3->next=p1;
    			}
    			n++;
    		}
    	}
    	return head;
    }</span>

    <span style="font-size:18px;">#include"head.h"
    struct ab *del(struct ab *head)
    {
    	struct ab *p1,*p2;
    	char c[10];
    	p1=head;
    	printf("please input the name you want to delete
    ");
    	scanf("%s",c);
    	for(;p1&&strcmp(c,p1->name);p2=p1,p1=p1->next);
    	if(p1==NULL)
    	{
    		printf("not find!press any key to thefirst 
    ");
    		getch();
    	}
    	else if(p1==head)
    		head=p1->next;
    	else
    		p2->next=p1->next;
    	return head;
    }</span>

    <span style="font-size:18px;">#include"head.h"
    void alter(struct ab *head)
    {
    	char str[10];
    	struct ab *p;
    	p=head;
    	printf("please input the name you want to search!
    ");
    	scanf("%s",str);
    	for(;p&&strcmp(str,p->name);p=p->next);
    	if(p)
    	{
    		printf("please input the address!
    ");
    		scanf("%s",p->addr);
    		printf("please input the zip!
    ");
    		scanf("%s",p->zip);
    		printf("please input the phone!
    ");
    		scanf("%s",p->phone);
    	}
    	else 
    	{
    		printf("not find!press any key to thefirst 
    ");
    		getch();
    	}
    	thefirst();
    }
    </span>

    <span style="font-size:18px;">#include"head.h"
    void print(struct ab *head)
    {
    	struct ab *p;
    	for(p=head;p;p=p->next)
    	{
    		printf("%10s%10s%10s%10s
    ",p->name,p->addr,p->zip,p->phone);
    	}
    	getch();
    	thefirst();
    }</span>


  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4238584.html
Copyright © 2011-2022 走看看