2020-11-28增删改查基础

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

//定义结构体类型
struct Customer
{
	int id;
	int age;
	char name[10];
	char gender;
	char phone[16];
	char email[20];
};

int loop = 1;
char key;

int customerNum = 1;
struct Customer customers[100];

void getCustomerInfo(struct Customer *customer)
{
	printf("\n%d\t%s\t%c\t%d\t%s\t\t%s",(*customer).id,(*customer).name,(*customer).gender,(*customer).age,(*customer).phone,(*customer).email);
}

void listCustomers()
{ 
	int i;
	printf("\n------------------------------客户列表------------------------------");
	printf("\n编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
	for(i=0;i<customerNum;i++)
	{
		getCustomerInfo(&customers[i]);
	}
}

void addCustomer()
{
	customers[customerNum].id = customerNum + 1;
	printf("\n------------------------------添加用户------------------------------");
	printf("\n姓名:");
	scanf("%s",customers[customerNum].name);
	getchar();
	printf("\n性别:");
	scanf("%c",&(customers[customerNum].gender));
	getchar();
	printf("\n年龄:");
	scanf("%d",&(customers[customerNum].age));
	getchar();
	printf("\n电话:");
	scanf("%s",customers[customerNum].phone);
	getchar();
	printf("\n邮箱:");
	scanf("%s",customers[customerNum].email);
	getchar();
	printf("\n------------------------------添加完成------------------------------\n");
	customerNum++;
}
//根据输入的ID,返回index,如果没有,返回-1
int findIndex(int id)
{
	int i;
	for(i=0;i<customerNum;i++)
	{
		if(customers[i].id==id)
		{
			return i;
		}
	}
	return -1;
}

//如果返回1,表示删除成功,如果是0,删除失败
int delCustomer(int id)
{
	int i = 0;
	int index = findIndex(id);//如果没找到则返回-1,赋值给index

	if(index==-1)
		return 0;    //返回0表示没找到对应的编号,删除失败.
	//如果有这个ID,则需要移除,分2步;
	//1.数组从index+1开始,整体前移
	for(i=index;i<customerNum;i++)
	{
		customers[i]= customers[i+1];
	}
	//2.操作完后customerNum-1
	customerNum--;
	return 1;
}

void delView()
{
	int id;
	char choice = ' ';
	int loop = 1;
	printf("\n---------------------删除用户---------------------");
	do
	{		
		printf("\n请输入要删除客户的编号(-1 退出)");
		scanf("%d",&id);
		getchar();

		if(id==-1)
		{
			printf("\n ---------------------你放弃了删除---------------------\n");
			return;
		}	
		printf("\n 确认是否删除(Y/N)?");
		scanf("%c",&choice);
		getchar();
		if(choice == 'Y')
		{
			if(delCustomer(id)==0)
			{
				printf("\n---------------------删除失败,id不存在---------------------\n");
			}else
			{
				printf("\n---------------------删除成功---------------------\n");
				loop = 0;
			}
		}
	}while(loop);
}

int user_exit()
{
	char choice = ' ';
	do
	{
		printf("\n你确定要退出吗? Y/N:");
			scanf("%c",&choice);
			getchar();
			if(choice == 'Y')
			{
				return 0;
			}
			else if(choice == 'N')
			{
				return 1;
			}else
			{
				printf("\n你输入有误,请重新输入....\n");
			}
	}while(1);
	

}

void data_xiugai(int index)
{
	char choice = ' ';
	do
	{
		printf("\n你确定要修改吗? Y/N:");
			scanf("%c",&choice);
			getchar();
			if(choice == 'Y')
			{
				printf("\n姓名:");
				scanf("%s",customers[index].name);
				getchar();
				printf("\n性别:");
				scanf("%c",&(customers[index].gender));
				getchar();
				printf("\n年龄:");
				scanf("%d",&(customers[index].age));
				getchar();
				printf("\n电话:");
				scanf("%s",customers[index].phone);
				getchar();
				printf("\n邮箱:");
				scanf("%s",customers[index].email);
				getchar();
				printf("\n------------------------------修改完成------------------------------\n");
				return ;
			}
			else if(choice == 'N')
			{
				printf("\n你放弃了修改....\n");
				return ;
			}else
			{
				printf("\n你输入有误,请重新输入....\n");
			}
	}while(1);
}

void user_xiugai()
{
	int enter_id;
	int index;
	do
	{
		printf("\n请输入需要修改用户的编号....-1表示退出!");
		scanf("%d",&enter_id);
		getchar();
		if(enter_id== -1)
		{
			return;
		}
		index = findIndex(enter_id);
		if(index==-1)
		{
			printf("\n你输入编号不存在,请重新输入....\n");
		}else
		{
			printf("\n编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
			getCustomerInfo(&customers[index]);
			data_xiugai(index);
			return;
		}
	}while(1);
}
void mainMenu()
{
	do
	{
		printf("\n-----------------客户信息管理软件-----------------\n");
		printf("\n                   1、添加用户");
		printf("\n                   2、修改用户");
		printf("\n                   3、删除用户");
		printf("\n                   4、客户列表");
		printf("\n                   5、退  出\n");

		printf("\n请选择(1-5):");
		scanf("%c",&key);
		getchar();

		switch(key)
		{
			case '1':
				addCustomer();
				break;
			case '2':
				printf("修改用户");
				user_xiugai();
				break;
			case '3':
				delView();
				break;
			case '4':
				listCustomers();
				break;
			case '5':
				loop = user_exit();
				break;
			default:
				printf("\n你输入有误,请重新输入...\n");
		}

	}while(loop);
	printf("\n你已退出了系统...\n");
	getchar();
}

void main()
{
	customers[0].age=1;
	strcpy(customers[0].email,"aa@bb.com");
	customers[0].gender='f';
	customers[0].id =1;
	strcpy(customers[0].name,"kkk");
	strcpy(customers[0].phone,"1231231");
	mainMenu();
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页