C语言ll作业01

问题 答案
这个作业要求在哪里 C语言作业01:通讯录
这个作业的目标 学会用指针数组知识编写通讯录
作业正文 C语言作业01
其他参考文献 学长视频

1.1 需求分析

通讯录容纳50人,需要有添加,排序,删除,修改,退出等功能。

1.2 程序代码

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

void input(); 
void find();
void xiugai();
void cut();
void menu();  
void Error();
void sort();
struct Preson{
	int no;
	char name[10];
	char phone[20];
	struct Preson *next;
}; 
Preson stu[50];
Preson t; 
Preson *s;
Preson t1; 
int sum=50,count=0;
void menu()
{
    printf("\n");
	printf("操作列表:\n");
    printf("1.排序          2.添加          3.删除\n");
    printf("4.修改          5.查找          6.退出程序\n");
    printf("请输入操作: ");
}

int main()
{
    int flag=1,i,j,n;
    while( 1 )
    {
    system("cls");
	printf("========== 通讯录 ==========\n\n\n");
	printf("");
	printf("========== 界面 ==========\n");
	printf("人数: %d 人           | 剩余空间: %d 人\n",count,sum);
	
	for( i=0; i<count; i++ )
	{
		printf("编号:%10d | 名字:%10s | 电话号码:%10s\n",stu[i].no,stu[i].name,stu[i].phone);
	}
	menu();
	scanf("%d",&n);
    switch(n)
		{
			case 1: sort();break;
			case 2: input(); break;
			case 3: cut();break;
			case 4: xiugai();break;
			case 5: find();break;
			case 6: return 0;
			default: Error();break;
	    }
   }
}

void input()
{
	int i,flag=1;
	if(sum==0)
	{
		printf("通讯录已满!\n");
	}
	else if(sum!=0&&flag!=0)
	{
		
		printf("请输入添加位置:");
		scanf("%d",&stu[count].no);
		if(stu[count].no>50||stu[count].no<=0)
		{
			printf("处理编号超过阈值");
			system("pause"); 
		}
		else 
		{
			   for( i=0; i<count; i++)
			   { 
				if( stu[i].no==stu[count].no ) {flag=0;printf("已经有该数据");system("pause");}
			   }
			if( flag!=0 )
		    {
		        printf("请输入联系人姓名:");
	            scanf("%s",stu[count].name);
		        printf("请请输入联系人电话:");
	            scanf("%s",stu[count].phone);
		        count++;
		        sum--; 
		    }
		    if(flag==0)
		    {
		    	void Error();
				printf("已此处已有数据\n");
			}
		}
	}

}

void find()
{
	char name[20];
	int b=0,j=0;
	Preson *p;
	p=stu;
	printf("请输入需要查询人的名字:");
	scanf("%s",name);
	while(j!=count)
	{
		if(strcmp(name,p->name)==0)
		{
			b=1;
			break;
		}
		p++;
		j++;
	}
	if(b==0)
	{
		printf("\n查无此人\n");
		system("pause");
	}
	else
	{
		printf("编号:%10d | 名字:%10s | 电话号码:%10s\n",p->no,p->name,p->phone);
        system("pause");
	}
}

void xiugai()
{
	 int i,j=0,flag=1,number;
	 char name[15]; 
	 char phone[20];
	 if(count==0)
	 {
	 	printf("你还还没有存储信息");
	 	system("pause");
	 }
	 else 
	 {  
	    printf("请输入修改位置:");
		scanf("%d",&number); 
	 	while(j!=count)
	 	{
	 		if(number==stu[j].no) 
	 		{
	 	     printf("请请输入联系人姓名:");
			 scanf("%s",&name);
			 strcpy(stu[j].name,name);
			 printf("请请输入联系人电话:");
			 scanf("%s",&phone);
			 strcpy(stu[j].phone,phone);
	 		 break;
			 }
			 else
			j++; 
		}
		if(j==count)
		{
			printf("此处无数据\n");
			system("pause");
		}
	 }
}

void cut()
{
	Preson *s;
	s=stu;
	int i,j,flag=count;
	int number; 
	if(count==0)
	{
		printf("此处无数据\n");
		system("pause");
	 } 
	 else
	 {
	    printf("请数入该联系人编号:");
		scanf("%d",&number);
		if(number<=50||number>=0)
	   {
		for( i=0; i<count; i++ )
	     {
			if(number==(s->no))
			{
				for( j=i; j<count; j++ )
				{
					stu[j]=stu[j+1];
				}
			    count--;
			    printf("已经删除!\n");
			}
			s++;
		 } 
	    }
	    else
	    {
	    	printf("已经越界,请输入正确数值");
		 } 
	} 
	 if(count==flag)
	 {
	 	printf("没有这个人\n");
	 	system("pause");
	 }
}

void Error()
{
	printf("错误操作:错误操作指令,请重新输入   ");
	system("pause"); 
}

void sort()
{
	int N;
	printf("请选择排序方式: 1)编号排序 2)名字排序");
	scanf("%d",&N);
	if(count==0)
	{
		printf("你还没有输入信息");
		system("pause");
	 }
	else
	{
		switch(N)
	  {
		case 1: printf("抱歉,功能未开发"); 
		case 2: printf("抱歉,功能未开发"); 
	  }
	}
}

1.3 代码测试









1.4 应用集成


链接:
https://gitee.com/zenghao67/zh20199333

posted on 2020-04-30 19:18  酴灵  阅读(738)  评论(0编辑  收藏  举报

导航