当前位置:网站首页>案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届

案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届

2022-06-26 19:11:00 laocooon

案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:

-   打开系统有欢迎界面,并显示可选择的选项
-   选项1:记录比赛得分
-   选项2:查看往届记录
-   选项3:清空比赛记录
-   选项4:退出当前系统

根据用户不同的输入,实现不同的需求,若输入为其他值,视为清空屏幕重新选择

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

/*
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:

-   打开系统有欢迎界面,并显示可选择的选项
-   选项1:记录比赛得分
-   选项2:查看往届记录
-   选项3:清空比赛记录
-   选项4:退出当前系统

根据用户不同的输入,实现不同的需求,若输入为其他值,视为清空屏幕重新选择
*/

int FileIsEmpty = 0; // 1 代表文件为空  0 文件不为空

void clearFile()
{
	if (FileIsEmpty)
	{
		printf("文件记录为空!\n");
		system("pause");
		system("cls");
		return;
	}

	printf("确认清空?\n");
	printf("1、确定!\n");
	printf("2、返回!\n");

	int select = 0;
	int num = scanf("%d", &select);

	if (select == 1)
	{
		FILE* fp = fopen("score.txt", "w");
		fclose(fp);
		printf("清空完毕!\n");

		FileIsEmpty = 1;

	}
	system("pause");
	system("cls");
}

void initFlag()
{
	FILE* fp = fopen("score.txt", "r");

	if (fp == NULL)
	{
		printf("文件打开失败\n");
		return;
	}

	char ch =  fgetc(fp);
	
	if (ch == EOF)
	{
		//printf("文件为空\n");
		FileIsEmpty = 1;
	}
	else
	{
		//printf("文件不为空\n");
		FileIsEmpty = 0;
	}


}

//记录分数
void setScore()
{


	printf("请输入新记录分数:\n");
	double score = 0;
	int num = scanf("%lf", &score);

	FILE* fp = fopen("score.txt", "a"); //a代表追加的方式写文件
	if (fp == NULL)
	{
		printf("文件打开失败!\n");
		return;
	}

	fprintf(fp, "%lf\n", score);
	printf("分数记录成功!\n");

	FileIsEmpty = 0;

	//关闭文件
	fclose(fp);

	system("pause");
	system("cls");

}

void showScore()
{

	if (FileIsEmpty)
	{
		printf("文件记录为空!\n");
		system("pause");
		system("cls");
		return;
	}
	
	FILE* fp = fopen("score.txt", "r");

	int index = 1;
	while (!feof(fp))
	{
		double score;
		int ret = fscanf(fp, "%lf", &score);
		//if (feof(fp))
		if(ret == -1)
		{
			break;
		}

		printf("%d届分数为: %.2lf\n", index, score);
		index++;
	}

	fclose(fp);
	system("pause");
	system("cls");
}


void show_Menu()
{
	printf("*********************************************\n");
	printf("*************  欢迎使用本程序   *************\n");
	printf("*************  1、记录比赛得分  *************\n");
	printf("*************  2、查看往届记录  *************\n");
	printf("*************  3、清空比赛记录  *************\n");
	printf("*************  4、退出当前系统  *************\n");
	printf("*********************************************\n");
}

int main() 
{
	initFlag();

	int choice = 0; //用来存储用户的选择
	int num = 0;
	
	while (1)
	{
		show_Menu();
		printf("请输入您的选择:\n");
		num = scanf("%d", &choice);


		switch (choice)
		{
		case 1:   //记录比赛得分
			setScore();
			break;
		case 2:   //查看往届记录
			showScore();
			break;
		case 3:   //清空比赛记录
			clearFile();
			break;
		case 4:   //退出当前系统
			printf("欢迎您下次使用\n");
			system("pause");
			exit(0); //退出当前系统
			break;
		default:
			system("cls");
			break;
		}
	}


	system("pause");
	return EXIT_SUCCESS;
}

原网站

版权声明
本文为[laocooon]所创,转载请带上原文链接,感谢
https://laocooon.blog.csdn.net/article/details/125466878