其他分享
首页 > 其他分享> > C语言学生运动员信息管理系统

C语言学生运动员信息管理系统

作者:互联网

C语言学生运动员信息管理系统

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<time.h>

#define load sizeof(student)
#define N 1000
void read();
void save();
void open(FILE*);
int count_line(FILE* f);
void find();
int size = 0;
int temp_count;
int line_count;
struct student
{
	char num[10];
	char name[20];
	int scorem, scoreq, scoreg, scorey;
	char ximing[20];
}stu[N], temp;

//计算文件行数,返回文件行数
int count_line(FILE* f)
{
	int count = 0;
	while (!feof(f))
	{

		int flag;
		flag = fgetc(f);
		if (flag == '\n')
			count++;
	}
	fseek(f, 0, SEEK_SET);//将文件指针重新指向文件开头
	return count;//返回行数
}

void input()	//输入原始数据
{
	char ch2;
	int i=0;
	line_count = size;
	system("cls");
	printf("\t*********************输入界面***********************\n");
	
	do {
		printf("请输入运动员号码,姓名,100成绩,铅球成绩,跳高趁机,跳远成绩,系名:\n");
		scanf("%s%s%d%d%d%d%s", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		i++;
		size++;
		printf("继续添加请输入y\n");
		getchar();
		scanf("%c", &ch2);
		//ch2=_getch();
	} while (ch2 == 'y' || ch2 == 'Y');
	/*
	for (int i = 0; i < size; i++)
	{
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	*/
	//save();
}
void  open(FILE* fp)
{
	//FILE* fp;//定义指向文件的指针
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}

}
//读取文件数据,并打印
void read()
{
	FILE* fp = NULL;
	//FILE* fp;//定义指向文件的指针
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	for (int i = 0; i < size; i++)
	{
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	fclose(fp);	//关闭文件
}

void find()//查找函数
{
	system("cls");
	printf("**************查找界面***************\n");
	char fs[20];
	int file_row, count = 0;
	char flag;
	//read();
	FILE* fp;//定义指向文件的指针
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	//统计文件行数 

	file_row = count_line(fp);
	fseek(fp, 0, SEEK_SET);
	printf("输入你要查找运动员的号码或姓名:");
	scanf("%s", &fs);
	getchar();
	int i;
	for ( i = 0; i < file_row; i++)
	{
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		if (strcmp(stu[i].num, fs) == 0)
		{
			printf("你要找的运动员已找到:\n");
			printf("该生的号码,姓名,100米成绩,千秋成绩,跳高成绩,跳远成绩,系名:\n");
			printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
			break;
		}
		else if (strcmp(stu[i].name, fs) == 0)
		{
			printf("你要找的运动员已找到:\n");
			printf("该生的号码,姓名,100米成绩,千秋成绩,跳高成绩,跳远成绩,系名:\n");
			printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
			break;
		}
		else
		{
			
			
		}
	}
	if (i == file_row)
	{
		printf("你要找的运动员未找到:\n");
		printf("运动员缺赛或不存在!");
	}


	//break;
	fclose(fp);
}

void Sort()//sort function
{
	int line;
	FILE* fp;//定义指向文件的指针
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}

	//fscanf(fp, "运动员号码,姓名,100成绩,铅球成绩,跳高成绩,跳远成绩,系名;\n");
	line = count_line(fp);
	for (int i = 0; i < line; i++)
	{
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		//printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	system("cls");
	printf("**************排序界面****************\n");
	printf("对100米的成绩排序:\n");
	for (int i = 0; i < line; i++)
	{
		for (int j = i + 1; j < line; j++)
		{
			if (stu[i].scorem < stu[j].scorem)
			{
				temp = stu[i];
				stu[i] = stu[j];
				stu[j] = temp;
			}
		}
	}
	printf("排序后:\n");
	for (int i = 0; i < line; i++)
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	getchar();
	fclose(fp);
}


void modify()//modify function
{
	system("cls");
	printf("\t***************修改界面******************\n");
	char ch2[20];
	int c;
	int line;
	char num[10], name[20];
	int scorem, scoreq, scoreg, scorey, i;
	char ximing[20];

	FILE* fp = NULL;//定义指向文件的指针
	FILE* fp1;
	FILE* fpw=NULL;
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	fp1 = fp;
	
	printf("原始数据\n");
	size = count_line(fp);
	//fseek(fp, 0, SEEK_SET);
	for(int i=0;i<size;i++)
		fscanf(fp1, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);

	if ((fpw = fopen("F:\\xiao.txt", "w")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}


	//open();
	/*
	for (int i = 0; i < size; i++)
	{
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	*/
	read();
	printf("输入你要修改运动员的姓名:");
	scanf("%s", ch2);
	getchar();
	for (int i = 0; i < size; i++)
	{
		if (strcmp(stu[i].name, ch2) == 0)
		{
			printf("1.修改号码\n2.修改姓名\n3.修改100米成绩\n4.修改铅球成绩\n5。修改跳高成绩\n6.修改跳远成绩\n7.修改系名\n");
			scanf("%d", &c);
			switch (c)
			{
			case 1:
			{
				printf("请输入你要修改的运动员号码");
				scanf("%s", &num);
				strcpy(stu[i].num, num);
				break;
			}
			case 2:
			{
				printf("输入你修改的运动员姓名:\n");
				scanf("%s", &name);
				strcpy(stu[i].name, name);
				break;
			}
			case 3:
			{
				printf("输入你修改的运动员100米成绩");
				scanf("%d", &scorem);
				stu[i].scorem = scorem;
				break;
			}
			case 5:
			{
				printf("输入你修改运动员跳高成绩:");
				scanf("%d", &scoreg);
				stu[i].scoreg = scoreg;
				break;
			}
			case 6:
			{
				printf("输入你修改运动员跳远成绩:");
				scanf("%s", &scorey);
				stu[i].scorey = scorey;
				break;
			}
			case 7:
			{
				printf("输入你修改运动员系名:");
				scanf("%s", &ximing);
				strcpy(stu[i].ximing, ximing);
				break;
			}
			default:
				printf("你的输入有误:\n");
			}
			//fseek(fp, i, SEEK_SET);
			//fprintf(fp, "\n");
			//fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
			printf("修改后的运动员数据:\n");
			for (int j = 0; j < size; j++)
				fprintf(fpw,"%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[j].num, stu[j].name, stu[j].scorem, stu[j].scoreq, stu[j].scoreg, stu[j].scorey, stu[j].ximing);
		}
	}
	//save();
	//exit(0);
	//return;
	getchar();
	fclose(fp);
	fclose(fpw);
}

void mark()//得分函数
{
	system("cls");
	printf("\t********得分界面*********\n");

	int x, y, z, w, o, p, q, i;
	char ch1[20] = "sl", ch2[20] = "jj", ch3[20] = "wy";
	printf("原始数据;\n");

	read();
	printf("各系的得分总排序:\n");
	for (i = 0; i < size; i++)
	{
		if (stu[i].scorem == 1)
			x = 3;
		else if (stu[i].scorem == 2)
			x = 2;
		else if (stu[i].scorem == 3)
			x = 1;
		else x = 0;

		if (stu[i].scoreq == 1)y = 3;
		else if (stu[i].scoreq == 2)y = 2;
		else if (stu[i].scoreq == 3)y = 1;
		else y = 0;

		if (stu[i].scoreg == 1)z = 3;
		else if (stu[i].scoreg == 2)z = 2;
		else if (stu[i].scoreg == 3)z = 1;
		else z = 0;

		if (stu[i].scorey == 1)w = 3;
		else if (stu[i].scorey == 2)w = 2;
		else if (stu[i].scorey == 3)w = 1;
		else w = 0;

	}

	for (int i = 0; i < size; i++)
	{
		if (strcmp(stu[i].ximing, ch1) == 0)
		{
			printf("水利系总得分为:%d\n", x + y + z + w);
		}
		else if (strcmp(stu[i].ximing, ch2) == 0)
		{
			printf("外语系系总得分为:%d\n", x + y + z + w);
		}
		else if (strcmp(stu[i].ximing, ch3) == 0)
		{
			printf("机交系总得分为:%d\n", x + y + z + w);
		}
	}
}

void shanchu()//删除函数
{
	system("cls");
	printf("**************删除界面***************\n");
	char ch1[20];
	printf("原始数据:\n");
	int i;
	FILE* fpw;
	if ((fpw = fopen("F:\\xiao.txt", "r+")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}

	for (int i = 0; i < size; i++)
	{
		fscanf(fpw, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	printf("输入你要删除的运动员的姓名;\n");
	scanf("%s", &ch1);
	for (i = 0; i < size; i++)
	{
		if (strcmp(stu[i].name, ch1) == 0)
		{
			for (int j = i; j < size; j++)
				stu[j] = stu[j + 1];
			size--;
		}
	}
	
	printf("删除后的运动员数据;\n");
	for (int i = 0; i < size; i++)
	{
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	}
	
	save();
}

void charu()//插入函数
{

	system("cls");
	int a;
	char ch;
	printf("\t******插入界面*******\n");
	/*
	printf("请输入要插入的运动员的人数:\n");
	scanf("%d", &a);
	*/
	FILE* fp, * fpw;//定义指向文件的指针
	
	if ((fp = fopen("F:\\xiao.txt", "r+")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	/*
	for (int i = 0; i < size; i++)
	{
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);

	}
	*/
	
	size = count_line(fp);
	for(int i=0;i<size;i++)
		fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", &stu[i].num, &stu[i].name, &stu[i].scorem, &stu[i].scoreq, &stu[i].scoreg, &stu[i].scorey, &stu[i].ximing);
	size--;
	do
	{
		fseek(fp, 0, SEEK_END);
		printf("输入要插入的运动员数据:\n");
		printf("号码");
		scanf("%s", &stu[size].num);
		printf("姓名");
		scanf("%s", &stu[size].name);
		printf("100米成绩");
		scanf("%d", &stu[size].scorem);
		printf("铅球成绩");
		scanf("%d", &stu[size].scoreq);
		printf("跳高成绩");
		scanf("%d", &stu[size].scoreg);
		printf("跳远成绩");
		scanf("%d", &stu[size].scorey);
		printf("系名");
		scanf("%s", &stu[size].ximing);
		fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[size].num, stu[size].name, stu[size].scorem, stu[size].scoreq, stu[size].scoreg, stu[size].scorey, stu[size].ximing);
		size++;
		//fseek(fpw, 0, SEEK_END);
		getchar();
		printf("想要继续操作请按y\n");
		scanf("%c",&ch);
	} while (ch == 'y' || ch == 'Y');

		
		/*
				for(int i=0;i<size;i++)
					for(int j=i+1;j<size+1;j++)
						if (stu[i].scorem < stu[j].scorem)
						{
							temp = stu[i];
							stu[i] = stu[j];
							stu[j] = temp;
						}
						*/


	printf("插入后运动员数据:\n");

	for (int i = 0; i < size; i++)
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);
	//save();
	getchar();
}

void save()//保存数据到文件函数
{
	FILE* fp;
	if ((fp = fopen("F:\\xiao.txt", "w")) == NULL)//为输出打开一个二进制文件,为只写模式
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	//fseek(fp, line_count, SEEK_SET);
	printf("\n保存中...。。。\n");
	for (int i =0; i < size; i++)
		fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%s\n", stu[i].num, stu[i].name, stu[i].scorem, stu[i].scoreq, stu[i].scoreg, stu[i].scorey, stu[i].ximing);

	fclose(fp);
	printf("保存成功....\n");
}



int login(char psw[])//密码校对函数
{
	static char key[10] = "12345";
	if (strcmp(psw, key) == 0)
		return 1;
	else
		return 0;
}

void Logon()
{
	char a;
	printf("\n");
	printf("*************欢迎进入校运动会管理界面**********************\n");
	printf("*********************************************************\n");
	printf("*******************登录请按回车键*************************\n");
	printf("*********************************************************\n");

	printf("\n");
	scanf("%c", &a);
	if (a == '\n')
	{
		FILE* fp;
		char psw[10];
		int count = 0, s;
		printf("您有三次输入机会!\n\n");
		printf("请输入密码:\n");
		scanf("%s", psw);
		s = login(psw);
		if (s == 1)
		{
			fp = fopen("F:\\password.txt", "w");
			fprintf(fp, "%s", psw);
			fclose(fp);
			system("CLS");
			printf("密码正确!\n");
			printf("*********************************************************\n");
			printf("***********************欢迎进入**************************\n");
			printf("*********************************************************\n\n\n");

		}

		if (s == 0)
		{
			count++;
			while (count != 3)
			{
				printf("请再次输入密码:\n");
				scanf("%s", psw);
				s = login(psw);
				if (s == 1)
				{
					fp = fopen("F:\\password.txt", "w");
					fprintf(fp, "%s", psw);
					fclose(fp);
					system("CLS");
					printf("密码正确!\n");
					printf("*********************************************************\n");
					printf("***********************欢迎进入**************************\n");
					printf("*********************************************************\n\n\n");
					return;
				}
				if (s == 0)
					count++;
			}
			if (count == 3)
			{
				printf("你已经错误输入3次,系统退出!\n");
				exit(0);
			}
		}
	}
	else
		exit(1);
}

void menu()
{
	int ch1;
	char ch2;
	struct tm* pt;
	time_t t;
	t = time(NULL);
	pt = localtime(&t);			//读取系统日期并把它放到结构体中

	do {
		printf("\t\t当前的系统日期:%d-%d-%d\n", pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday);//显示当前系统日期
		printf("=======================菜单=========================\n");
		printf("1.输入					2.查找			\n");
		printf("3.排序					4.修改			\n");
		printf("5.得分					6.删除			\n");
		printf("7.插入					8.保存			\n");
		printf("9.显示					0.退出\n");
		printf("===================================================\n");
		printf("请选择功能<1-8>:[ ]\b\b");
		scanf("%d", &ch1);
		switch (ch1)
		{
		case 1:
			input();
			break;
		case 2:
			find();
			break;
		case 3:
			Sort();
			break;
		case 4:
			modify();
			break;
		case 5:
			mark();
			break;
		case 6:
			shanchu();
			break;
		case 7:
			charu();
			break;
		case 8:
			save();
			break;
		case 9:
			read();
			break;
		case 0:
			exit(0);
		default:
			printf("未进行任何操作!");
		}
		printf("\n");
		printf("想要继续操作请按y\n");
		//while(getchar()!='\n')
			//continue;
		 //getchar();
		scanf("\n%2c", &ch2);

	} while (ch2 == 'y' || ch2 == 'Y');//条件判断
	exit(0);
}

void main()
{
	FILE* fp;//定义指向文件的指针
	if ((fp = fopen("F:\\xiao.txt", "r")) == NULL)
	{
		printf("打不开文件\n");
		return;					//若打不开则返回菜单
	}
	size = count_line(fp);//读取文件行数
	fclose(fp);
	system("color 3f");
	//Logon();
	menu();
}



标签:fp,int,C语言,stu,运动员,printf,信息管理系统,line,t%
来源: https://blog.csdn.net/weixin_44735475/article/details/116245366