当前位置:网站首页>Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -

Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -

2022-06-26 19:27:00 laocooon

Case description : Competition score management system , It is necessary to count the scores of previous champions , And record it in the file , The system has the following requirements :

-   Open the system to have a welcome interface , And display the selectable options
-   Options 1: Record game scores
-   Options 2: Look at past records
-   Options 3: Clear the game record
-   Options 4: Exit the current system

According to the user's different input , Implement different requirements , If other values are entered , It is deemed to clear the screen and reselect

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

/*
 Case description : Competition score management system , It is necessary to count the scores of previous champions , And record it in the file , The system has the following requirements :

-    Open the system to have a welcome interface , And display the selectable options 
-    Options 1: Record game scores 
-    Options 2: Look at past records 
-    Options 3: Clear the game record 
-    Options 4: Exit the current system 

 According to the user's different input , Implement different requirements , If other values are entered , It is deemed to clear the screen and reselect 
*/

int FileIsEmpty = 0; // 1  Represents that the file is empty   0  The file is not empty 

void clearFile()
{
	if (FileIsEmpty)
	{
		printf(" The file record is empty !\n");
		system("pause");
		system("cls");
		return;
	}

	printf(" Make sure to clear ?\n");
	printf("1、 determine !\n");
	printf("2、 return !\n");

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

	if (select == 1)
	{
		FILE* fp = fopen("score.txt", "w");
		fclose(fp);
		printf(" Emptying complete !\n");

		FileIsEmpty = 1;

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

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

	if (fp == NULL)
	{
		printf(" File opening failure \n");
		return;
	}

	char ch =  fgetc(fp);
	
	if (ch == EOF)
	{
		//printf(" The file is empty \n");
		FileIsEmpty = 1;
	}
	else
	{
		//printf(" The file is not empty \n");
		FileIsEmpty = 0;
	}


}

// Record score 
void setScore()
{


	printf(" Please enter a new record score :\n");
	double score = 0;
	int num = scanf("%lf", &score);

	FILE* fp = fopen("score.txt", "a"); //a Represents the way to write a file by appending 
	if (fp == NULL)
	{
		printf(" File opening failure !\n");
		return;
	}

	fprintf(fp, "%lf\n", score);
	printf(" Score recorded successfully !\n");

	FileIsEmpty = 0;

	// Close file 
	fclose(fp);

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

}

void showScore()
{

	if (FileIsEmpty)
	{
		printf(" The file record is empty !\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 The session score is : %.2lf\n", index, score);
		index++;
	}

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


void show_Menu()
{
	printf("*********************************************\n");
	printf("*************   Welcome to this program    *************\n");
	printf("*************  1、 Record game scores   *************\n");
	printf("*************  2、 Look at past records   *************\n");
	printf("*************  3、 Clear the game record   *************\n");
	printf("*************  4、 Exit the current system   *************\n");
	printf("*********************************************\n");
}

int main() 
{
	initFlag();

	int choice = 0; // Used to store the user's selection 
	int num = 0;
	
	while (1)
	{
		show_Menu();
		printf(" Please enter your choice :\n");
		num = scanf("%d", &choice);


		switch (choice)
		{
		case 1:   // Record game scores 
			setScore();
			break;
		case 2:   // Look at past records 
			showScore();
			break;
		case 3:   // Clear the game record 
			clearFile();
			break;
		case 4:   // Exit the current system 
			printf(" Welcome to use... Next time \n");
			system("pause");
			exit(0); // Exit the current system 
			break;
		default:
			system("cls");
			break;
		}
	}


	system("pause");
	return EXIT_SUCCESS;
}

原网站

版权声明
本文为[laocooon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261911120545.html