#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
typedef struct bike
{
	char name[20]; //用户的姓名
	char tele[12]; //用户的电话
	char sex[7];   //用户的性别
	int biknum;	   //自行车编号
	struct bike *next;
} bikeinformation, *Link;
struct bike *pHead = NULL;
void swap_int();
void save1();
void key();	 //账号密码管理;
void menu(); //登录界面;
Link cre();	 //录入自行车的信息
Link mod();	 //修改自行车的信息
void del();	 //删除自行车的信息
void sea();	 //查询自行车的信息
void pri();	 //打印自行车的信息
void sor();	 //排序自行车的信息
void ins();	 //插入自行车的信息
void save(); //保存信息
void save2();
void swap_str();
int count = 0;
int icount = 0;
int main(void)
{
	system("title 计科2005 -JYS");
	menu(); //登陆界面
	return 0;
}
void key()
{ //创建密码
	char key[20], n[20], key1[20], kz[20], kz1[20];
	FILE *m;
	m = fopen("key.txt", "r");
	if (m == NULL)
	{
		m = fopen("key.txt", "wt");
		printf("请创建账号:");
		scanf("%s", kz[20]);
		printf("请您创建管理密码:\n");
		scanf("%s", key);
		fprintf(m, "%s", key);
		printf("请再次确认密码:\n");
		scanf("%s", key1);
		if (strcmp(key1, key) == 0)
		{
			printf("恭喜您创建成功!\n");
			getchar();
			printf("按任意键继续\n");
			getchar();
			return;
		}
	}
	else
	{
		fscanf(m, "%s", n);
		fclose(m);
		printf("请输入账号:");
		scanf("%s", kz1);
		printf("请输入管理密码:");
		//scanf("%s", key);
		int i=0;
while( i<9&&(key[i]=getch())&&key[i]!='\r'){
	if(key[i]=='\b'){//对密码进行掩饰
	printf("\b \b");
	i--;
	}
	else {
	printf("*");
	i++;}
}
key[i]=0;
		if (strcmp(n, key) == 0)
		{
			printf("\n密码正确!\n");
			getchar();
			printf("按任意键继续");
			getchar();
			return;
		}
		else
		{
			printf("密码错误!\n");
			getchar();
			printf("按任意键继续");
			getchar();
			return;
		}
	}
}
void menu()
{
	//system("cls");
	key();
	int s = 1;
	while (s)
	{
		printf("\t*****欢迎使用自行车管理系统*****\n");
		printf("\t-----请选择功能列表-----\n");
		printf("\t-输入1.[录入自行车的信息]\n");
		printf("\t*输入2.[修改自行车的信息]\n");
		printf("\t-输入3.[删除自行车的信息]\n");
		printf("\t*输入4.[查询自行车的信息]\n");
		printf("\t-输入5.[打印自行车的信息]\n");
		printf("\t*输入6.[自行车信息的排序]\n");
		printf("\t-输入7.[插入自行车的信息]\n");
		printf("\t*输入0.[退出界面退出界面]\n");
		scanf("%d", &s);

		switch (s)
		{
		case 1:
			cre();
			break;
		case 2:
			mod();
			break;
		case 3:
			del();
			break;
		case 4:
			sea();
			break;
		case 5:
			pri();
			break;
		case 6:
			sor();
			break;
		case 7:
			ins();
			break;
		case 0:
			break;
		default:
			exit(1);
			break;
		}
	}
}
Link cre()
{
	struct bike *pEnd, *pNew;
	struct bike *p;
	pHead = pEnd = pNew = (struct bike *)malloc(sizeof(struct bike));
	pNew->next = NULL;
	pEnd->next = NULL;
	pHead->next = NULL;
	p->next = NULL;
	int f0 = 1, f1, f2;
	int n;
	printf("请输入存放自行车的信息,若退出请按0:\n");
	struct bike *pTemp;
	scanf("%d", &n);
	if (n == 0)
		return NULL;
	printf("请输入自行车的编号:");
	scanf("%d", &pNew->biknum);
	n = pNew->biknum;
	while (1)
	{
		if (n == 0)
		{
			f0 = 1;
			break;
		}
		else
		{
			pTemp = pHead;
			f1 = 1;
			if (f1 == 0)
			{ //录入自行车的编号且编号不能重复。
				printf("1请重新输入,自行车编号%d存在,退出请按0.):\n", pNew->biknum);
				// printf("1输入自行车的编号");
				scanf("%d", &n);
				if (n)
					pNew->biknum = n;
				// printf("退出请按0");
				f0 = 1;
			}

			if (f1 == 1)
			{
				f0 = 0;
			}
		}
		if (f0 != 1)
		{
			printf("请输入姓名:");
			scanf("%s", pNew->name);
			while (1)
			{
				printf("请输入性别(man/woman):");
				scanf("%s", pNew->sex);
				if (strcmp(pNew->sex, "man") == 0 || strcmp(pNew->sex, "woman") == 0)
					break;
				else
					printf("性别输入不规范请重新输入!");
			} //性别必须输入 man(男)woman(女);
			while (1)
			{
				printf("请输入11位数的话号码(+86):"); //电话号码必须为11位,中国大陆+86;
				scanf("%s", pNew->tele);
				if (strlen(pNew->tele) == 11)
					break;
				else
					printf("必须输入11位数的电话号码:");
			}
		}
		save(pNew);
		icount++;
		return (pNew);
		free(pNew);
	}
}
Link mod()
{
	printf("*=*修改自行车的信息*=*\n");
	int n, f0 = 1, f1;
	int num;
	Link head = NULL, p = NULL, t = NULL;
	Link L, q;
	head = (bikeinformation *)malloc(sizeof(bikeinformation));
	head->next = NULL;
	t = head;
	FILE *fp = NULL;
	fp = fopen("D:/code.c/data.txt", "w");
	if (fp == NULL)
	{
		printf("打开文件失败,按任意键退出");
		getch();
		return 0;
	}
	while (!feof(fp))
	{
		p = (bikeinformation *)malloc(sizeof(bikeinformation));
		fscanf(fp, "%s ", p->name);
		fscanf(fp, "%s ", p->sex);
		fscanf(fp, "%s ", p->tele);
		fscanf(fp, "%d ", &p->biknum);
		t->next = p;
		t = p;
	}
	t->next = NULL;
	fclose(fp);
	//for(p = head->next; p; p = p->next) puts(p->biknum);
	printf("请输入你要修改的自行车的编号pwq:\n");
	scanf("%d", &num);
	while (f0)
	{
		for (p = head->next; p; p = p->next)
		{

			if (p->biknum == num)
			{
				printf("将要修改的编号%d具体的信息owo:\n", num);
				printf("poq:姓名:%s  性别:%s  电话:%s 自行车编号:%d \n", p->name, p->sex, p->tele, p->biknum);
				f0 = 0;
			}
			t = p;
		}
		if (!p)
			break;
	}
	if (f0 == 1)
		printf("您要修改的编号%d不存在yyy\n", num);
	else
	{
		printf("确认修改请按6,放弃修改请按1 ...\n");
		scanf("%d", &n);
		while (n == 6)
		{
			printf("请选择 1.姓名、2.性别、3.电话号码、4.自行车的编码、5.修改全部\n");
			scanf("%d", &f1);
			if (f1 == 1)
			{
				printf("输入新的姓名:");
				scanf("%s", t->name);
				printf("修改完成,请按回车键退出。\n");
			}
			else if (f1 == 2)
			{
				printf("输入新的性别:\n");
				scanf("%s", t->sex);
				printf("修改完成,请按回车键推出。\n");
			}
			else if (f1 == 3)
			{
				while (1)
				{
					printf("输入新的电话:\n");
					scanf("%s", t->tele);
					if (strlen(t->tele) == 11)
					{
						break;
					}
					else
						printf("输入的电话不规范,必须为11位数\n");
				}

				printf("修改完毕,按回车键退出\n");
			}
			else if (f1 == 4)
			{
				printf("请输入新的编号wqw:\n");
				scanf("%d", &t->biknum);
				printf("修改完成,按回车键退出\n");
			}
			else if (f1 == 5)
			{
				printf("输入新的姓名:\n");
				scanf("%s", t->name);
				while (1)
				{
					printf("输入新的性别:\n");
					scanf("%s", t->sex);
					if (strcmp(t->sex, "man") == 0 || strcmp(t->sex, "woman") == 0)
						break;
					else
						printf("性别输入不规范请重新输入!");
				}
				while (1)
				{
					printf("输入新的电话:\n");
					scanf("%s", t->tele);
					if (strlen(t->tele) == 11)
						break;
					else
						printf("输入的电话不规范,必须为11位数\n");
				}
				printf("请输入新的编号wqw:\n");
				scanf("%d", &t->biknum);
			}
			break;
		}
		printf("修改成功,按回车键退出\n");
		getch();
	}
	save2(head);
}
void del()
{
	printf("***删除自行车的信息qwq\n");
	int i = 0;
	int f0 = 1;
	int num;
	char name[20];
	Link head = NULL, p = NULL, t = NULL;
	Link L, q;
	head = (bikeinformation *)malloc(sizeof(bikeinformation));
	head->next = NULL;
	t = head;
	FILE *fp = NULL;
	fp = fopen("D:/code.c/data.txt", "r");
	if (fp == NULL)
	{
		printf("打开文件失败,按任意键退出");
		getch();
		return;
	}
	while (!feof(fp))
	{
		p = (bikeinformation *)malloc(sizeof(bikeinformation));
		fscanf(fp, "%s ", p->name);
		fscanf(fp, "%s ", p->sex);
		fscanf(fp, "%s ", p->tele);
		fscanf(fp, "%d ", &p->biknum);
		t->next = p;
		t = p;
	}
	t->next = NULL;
	fclose(fp);
	int s = 1;
	while (s)
	{
		printf("按自行车编号删除请按1\n");
		printf("退出请按0\n");
		printf("做出你的选择:");
		scanf("%d", &s);
		if (s == 0)
			return;
		else if (s == 1)

			break;
		else
			printf("输入1或者0:\n");
	}
	if (s == 1)
	{

		int num;
		int i, f0 = 1;
		int opt;
		int dex = 0;
		printf("请输入需要删除的自行车编号:");
		scanf("%d", &num);
		for (q = head->next, L = head; q; q = q->next, L = L->next)
		{
			if (q->biknum==num)
			{
				printf("编号为%d的车子找到了,具体信息如下:\n", num);
				printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", q->name, q->sex, q->tele, q->biknum);
				f0 = 0;
				printf("确认删除请输1,否则按任意数字键退出\n");
				scanf("%d", &opt);
				break;
			}
		}
		if (f0 == 1)
		{
			printf("+-+编号%d不存在的-+-\n", num);
			return;
		}
		if (opt == 1)
		{
			L->next = q->next;
			free(q);
			printf("操作成功按回车键退出poq:");
			getch();
		}
		else
			printf("删除失败按任意键退出");
	}
	save1(head);
	getch();
}

void sea()
{
	printf("*-*查询关于自行车的信息*-*\n");
	int i = 0;
	int f0 = 1;
	Link head = NULL, p = NULL, t = NULL;
	Link L, q;
	head = (bikeinformation *)malloc(sizeof(bikeinformation));
	head->next = NULL;
	t = head;
	FILE *fp = NULL;
	fp = fopen("D:/code.c/data.txt", "r");
	if (fp == NULL)
	{
		printf("打开文件失败,按任意键退出");
		getch();
		return;
	}
	while (!feof(fp))
	{
		p = (bikeinformation *)malloc(sizeof(bikeinformation));//连续申请内存空间
		fscanf(fp, "%s ", p->name);
		fscanf(fp, "%s ", p->sex);
		fscanf(fp, "%s ", p->tele);
		fscanf(fp, "%d ", &p->biknum);
		t->next = p;
		t = p;
	}
	t->next = NULL;
	fclose(fp);
	int s = 1;
	while (s)
	{
		printf("按 姓名 查询 请按1\n");
		printf("按自行车编号查询请按2\n");
		printf("想退出请按0\n");
		printf("你的选择是:");
		scanf("%d", &s);

		int num;
		char name[20];
		if (s == 1)
		{
			printf("请输入你要查找的用户姓名:\n");
			scanf("%s", name);

			for (p = head->next; p; p = p->next)
			{
				if (strcmp(p->name, name) == 0)
				{
					printf("找到了。具体信息如下:\n");
					printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
					f0 = 0;
				}
			}

			if (f0 == 1)
			{
				printf("姓名%s不存在\n", name);
			}

			printf("按回车键返回上一级\n");
			getch();
		}

		if (s == 2)
		{
			printf("请输入你要查找的自行车编号:\n");
			scanf("%d", &num);

			for (p = head->next; p; p = p->next)
			{
				if (p->biknum==num)
				{
					printf("编号为%d的车子找到了,具体信息如下:\n", num);
					printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
					f0 = 0;
				}
			}

			if (f0 == 1)
			{
				printf("编号%s不存在\n", num);
			}
			printf("按回车键返回上一级\n");
			getch();
		}
	}
}

void pri()
{
	printf("**打印自行车的信息**\n");
	printf("*本名单中自行车信息,如下:\n");
	int i = 0;
	Link head = NULL, p = NULL, t = NULL;
	Link L, q;
	head = (bikeinformation *)malloc(sizeof(bikeinformation));
	head->next = NULL;
	t = head;
	FILE *fp = NULL;
	fp = fopen("D:/code.c/data.txt", "r");
	if (fp == NULL)
	{
		printf("打开文件失败,按任意键退出");
		getch();
		return;
	}
	while (!feof(fp))
	{
		p = (bikeinformation *)malloc(sizeof(bikeinformation));
		fscanf(fp, "%s ", p->name);
		fscanf(fp, "%s ", p->sex);
		fscanf(fp, "%s ", p->tele);
		fscanf(fp, "%d ", &p->biknum);
		t->next = p;
		t = p;
	}
	t->next = NULL;
	fclose(fp);

	for (p = head->next; p; p = p->next)
	{
		printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
	}

	printf("*按回车键返回上一级pwq*\n");
	getch();
}

void sor()
{
	int s;
	printf("\tqwq 给自行车的信息排序 ewe\n");

	struct bike *pStra, *pMidd, *pTemp;
	Link head = NULL, p = NULL, t = NULL;
	Link L, q;
	head = (bikeinformation *)malloc(sizeof(bikeinformation));
	head->next = NULL;
	t = head;
	FILE *fp = NULL;
	fp = fopen("D:/code.c/data.txt", "r");
	if (fp == NULL)
	{
		printf("打开文件失败,按任意键退出");
		getch();
		return;
	}
	while (!feof(fp))
	{
		p = (bikeinformation *)malloc(sizeof(bikeinformation));
		fscanf(fp, "%s ", p->name);
		fscanf(fp, "%s ", p->sex);
		fscanf(fp, "%s ", p->tele);
		fscanf(fp, "%d ", &p->biknum);
		t->next = p;
		t = p;
	}
	t->next = NULL;
	fclose(fp);
	printf("\t**-** 选择怎样排序 --*--\n");
	printf("\t 输入1:按自行车编号升序排序\n");
	printf("\t 输入2:按自行车编号降序排序\n");
	printf("\t 输入3:按用户姓名升序排序\n");
	printf("\t 输入4:按用户姓名降序排序\n");
	printf("\t 输入0:退出页面\n");
	printf("\t是时候做出你的选择了!\n");
	scanf("%d", &s);
	if (s == 1)
	{
		for (p = head->next; p->next; p = p->next)
		{
			for (t = p->next; t; t = t->next)
			{
				if (p->biknum>t->biknum)
				{
					swap_int(&p->biknum, &t->biknum);
					swap_str(p->name, t->name);
					swap_str(p->sex, t->sex);
					swap_str(p->tele, t->tele);
				}
			}
		}
		for (p = head->next; p; p = p->next)
		{
			printf("排序好了信息如下:\n");
			printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
		}
	}
	if (s == 2)
	{
		for (p = head->next; p->next; p = p->next)
		{
			for (t = p->next; t; t = t->next)
			{
				if (p->biknum < t->biknum)
				{
					swap_int(&p->biknum, &t->biknum);
					swap_str(p->name, t->name);
					swap_str(p->sex, t->sex);
					swap_str(p->tele, t->tele);
				}
			}
		}
		for (p = head->next; p; p = p->next)
		{
			printf("排序好了信息如下:\n");
			printf("姓名:%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
		}
	}
	if (s == 3)
	{
		for (p = head->next; p->next; p = p->next)
		{
			for (t = p->next; t; t = t->next)
			{
				if (strcmp(p->name, t->name) < 0)
				{
					swap_str(p->name, t->name);
					swap_int(&p->biknum, &t->biknum);
					swap_str(p->name, t->name);
					swap_str(p->sex, t->sex);
					swap_str(p->tele, t->tele);
				}
			}
		}
		for (p = head->next; p; p = p->next)
		{
			printf("排序好了信息如下:\n");
			printf("姓名%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
		}
	}
	if (s == 4)
	{

		for (p = head->next; p->next; p = p->next)
		{
			for (t = p->next; t; t = t->next)
			{
				if (strcmp(p->name, t->name) > 0)
				{
					swap_int(&p->biknum, &t->biknum);
					swap_str(p->name, t->name);
					swap_str(p->sex, t->sex);
					swap_str(p->tele, t->tele);
				}
			}
		}
		for (p = head->next; p; p = p->next)
		{
			printf("排序好了信息如下:\n");
			printf("姓名%s 性别:%s 电话:%s 编号:%d\n", p->name, p->sex, p->tele, p->biknum);
		}
	}
	if (s == 0)
	{
		return;
	}
}

void ins()
{
	printf("owo 插入自行车的信息 yoy\n");
	cre();
	printf("ToT 插入信息成功,按回车键回到上一级哦ioi\n");
	getch();
}

void swap_int(int *a, int *b)
{
	int c;
	c = *a;
	*a = *b;
	*b = c;
}
void swap_str(char *str, char *str1)
{
	char str2[20];
	strcpy(str2, str1);
	strcpy(str1, str);
	strcpy(str, str2);
}

void save1(Link pHead)
{
	Link r;
	FILE *fp;
	fp = fopen("d:\\code.c\\data.txt", "wt");
	for (r = pHead->next; r; r = r->next)
	{
		fprintf(fp, "%s %s %s %d\n", r->name, r->sex, r->tele, r->biknum);
	}
	printf("保存成功AoA");
	fclose(fp);
}

void save(Link pHead)
{
	Link r;
	FILE *fp;
	fp = fopen("d:\\code.c\\data.txt", "a");//打开文件
	for (r = pHead; r; r = r->next)//运用循环保存信息
	{
		fprintf(fp, "%s %s %s %d\n", r->name, r->sex, r->tele, r->biknum);
		printf("保存成功AoA");
	}
	fclose(fp);//关闭文件
}

void save2(Link pHead)
{
	Link r;
	FILE *fp;
	fp = fopen("d:\\code.c\\data.txt", "w");
	for (r = pHead->next; r; r = r->next)
	{
		fprintf(fp, "%s %s %s %d\n", r->name, r->sex, r->tele, r->biknum);
		printf("保存成功AoA");
	}
	fclose(fp);
}