当前位置:网站首页>案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项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;
}边栏推荐
猜你喜欢
随机推荐
抖音实战~分享模块~生成短视频二维码
Database SQL statement writing
Record of user behavior log in SSO microservice Engineering
NFTGameFi链游系统开发详解方案丨链游系统开发原理解析
Basic and necessary common plug-ins of vscade
JS mobile terminal touch screen event
Determine whether a sequence is a stack pop-up sequence
Summary of several common UML diagrams
深度学习之Numpy篇
Microservice architecture
8VC Venture Cup 2017 - Final Round C. Nikita and stack
8VC Venture Cup 2017 - Final Round C. Nikita and stack
Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
[kubernetes] kubernetes principle analysis and practical application (under update)
问题解决:虚拟机无法复制粘贴文件
Deep learning: numpy
转:实事求是
Development principle analysis and source code of dapp-lp single and dual currency liquidity pledge mining system
Using cache in vuex to solve the problem of data loss in refreshing state
[MySQL series] collection of common working SQL (continuous update)









