#---- TableTennis.h ----#
typedef struct
{
int aCount;
int aStatusFlag;
int aWinFourgamesFlag;
int aCompetentFlag;
}aScoreStruct;
extern aScoreStruct aScoreS;
typedef struct
{
int bCount;
int bStatusFlag;
int bWinFourgamesFlag;
int bCompetentFlag;
}bScoreStruct;
extern bScoreStruct bScoreS;
#---- TableTennis.c ----#
#include <stdio.h>
#include "TableTennis.h"
aScoreStruct aScoreS;
bScoreStruct bScoreS; // 结构体声明,引用,理解为: 结构体 被 bScoreS 继承
#define ScoreZero 0 // 定义常量变量,理解为 普通变量 后面是赋值 0
#define ScoreFour 4
#define ScoreTen 10
#define ScoreEleven 11
#define ScoreTwelve 12
static int VictoryExitFlag = 0; // static 静态变量 其数值 为0 不被更改,一般在函数内:被其他方法赋值后,在其他if 或者 for 等方法中 不希望保留数值,而是每次都是 0 进行使用
static int abTieFlag = 0;
void Counting_Score(aScoreStruct *_aScoreS,bScoreStruct *_bScoreS,int inputData[2])
{ // 结构体 赋值给 指针,main 函数调用本函数需要传递内存地址
if( inputData[0] == 1 )
{
_aScoreS->aStatusFlag = 1;
_aScoreS->aCount++;
}
else
{
_bScoreS->bStatusFlag = 1;
_bScoreS->bCount++;
}
if(_aScoreS->aCount == ScoreFour && _bScoreS->bCount == ScoreZero)
{
VictoryExitFlag = 1;
aScoreS.aWinFourgamesFlag = 1;
printf("\n a 胜利,连胜 %d 局 \n",ScoreFour);
}
if(_aScoreS->aCount == ScoreZero && _bScoreS->bCount == ScoreFour)
{
VictoryExitFlag = 1;
bScoreS.bWinFourgamesFlag = 1;
printf("\n b 胜利,连胜 %d 局 \n",ScoreFour);
}
if( _bScoreS->bCount == ScoreTen && _aScoreS->aCount == _bScoreS->bCount )
{
printf("\n a b 在 %d : %d 中平局,需要加局赛 \n",ScoreTen,ScoreTen );
abTieFlag = 1;
}
if( _aScoreS->aCount == ScoreTwelve && abTieFlag == 1)
{
VictoryExitFlag = 1;
printf("\n a 胜利,得分: %d ; 来自: %d : %d 平局加局赛 \n",_aScoreS->aCount, ScoreTen,ScoreTen);
}
if( _bScoreS->bCount == ScoreTwelve && abTieFlag == 1)
{
VictoryExitFlag = 1;
printf("\n b 胜利,得分: %d ; 来自: %d : %d 平局加局赛 \n",_bScoreS->bCount, ScoreTen,ScoreTen);
}
if( _aScoreS->aCount == ScoreEleven && abTieFlag == 0)
{
VictoryExitFlag = 1;
printf("\n a 胜利,得分: %d ; 来自:先得 11 分\n",_aScoreS->aCount);
}
if( _bScoreS->bCount == ScoreEleven && abTieFlag == 0)
{
VictoryExitFlag = 1;
printf("\n b 胜利,得分: %d ; 来自:先得 11 分\n",_bScoreS->bCount);
}
}
int TableTennis(void)
{
for(;;)
{
int inputData[2];
if( VictoryExitFlag == 1 )
{
printf("\n 统计结束 \n");
break;
}
printf("\n 输入 A 的分数,然后回车输入B的分数 \n");
for(int i=0;i<2;i++)
{
scanf("%d",&inputData[i]);
}
Counting_Score(&aScoreS,&bScoreS,inputData);
printf("\n aScoreS.aCount = %d,aScoreS.aWinFourgamesFlag = %d,\n bScoreS.aCount = %d,bScoreS.bWinFourgamesFlag = %d",aScoreS.aCount,aScoreS.aWinFourgamesFlag,bScoreS.bCount,bScoreS.bWinFourgamesFlag);
}
}
int main()
{
TableTennis();
}
##################### shell 脚本 方式 ################################
#!/bin/bash
#----- 5 --------#
# 乒乓球规则:
# 在一局比赛中,先得11分的一方为胜方;
# 10 比 10 平手后,先多得 2 分的一方为 胜方;
# 一场比赛中,先赢 4 局 的人为整场比赛胜方;
# 现在需要一个程序实现乒乓球比赛的记分表:
# 1. 每次输入 0/1 代表该次赢球获胜方( 0/1 表示输入 0 或者 1 )
# 2. 足够长的比赛赢球输入后,当整场比赛的最终胜方出现时,返回该结果
Acont=0;Astatus=0;Aok=0; Aliansheng=0;
Bcont=0;Bstatus=0;Bok=0; Bliansheng=0;
PingJu=0;
main(){
for((;;))
do
# read -p "请输入本次的分数, a b 两位参赛者顺序输入 0 1: " tongji
# tjList=( $tongji )
# if [ ${tjList[0]} = 1 ]
# then
# ((Acont++))
# ((Aliansheng++))
# else
# ((Bcont++))
# ((Bliansheng++))
# if
read -p "请输入本次的分数, a b 两位参赛者顺序输入 0 1: " tongji
tjList=( $tongji )
ajia=${tjList[0]}
if [ $ajia = 1 ]
then
((Acont++))
((Aliansheng++))
else
((Bcont++))
((Bliansheng++))
fi
if [[ $Aliansheng = 4 ]] && [[ $Bliansheng = 0 ]]
then
Aok=1;
echo "A 胜利, 连胜 4 场"
exit;
else
echo ""
fi
if [[ $Bliansheng = 4 ]] && [[ $Aliansheng = 0 ]]
then
Bok=1;
echo "B 胜利, 连胜 4 场"
exit
else
echo ""
fi
#---------------------------- 平局 与 正常加分 -------------------#
if [ $Acont -eq 10 ]
then
if [ $Acont = $Bcont ]
then
PingJu=1;
fi
fi
if [ $Bcont -eq 10 ]
then
if [ $Acont = $Bcont ]
then
PingJu=1;
fi
fi
if [[ $Acont = 12 ]] && [[ $PingJu = 1 ]]
then
Aok=1
echo "A 胜利, 平局加局赛"
exit
fi
if [[ $Bcont = 12 ]] && [[ $PingJu = 1 ]]
then
Bok=1;
echo "B 胜利, 平局加局赛"
exit
fi
#---- ok ----
if [ $PingJu = 0 ]
then
if [ $Acont = 11 ]
then
Aok=1;
echo "A 胜利 先得分 11 分"
exit
fi
if [ $Bcont = 11 ]
then
Bok=1;
echo "B 胜利 先得分 11 分"
# exit
fi
fi
echo " Acont = $Acont; Bcont = $Bcont ; Aliansheng = $Aliansheng; Bliansheng = $Bliansheng; PingJu = $PingJu"
done
}
mainPowered by ddoss.cn 12.0
©2015 - 2025 ddoss
渝公网安备50011302222260号
渝ICP备2024035333号
【实验平台安全承诺书】
小绿叶技术社区,优化网络中,点击查看配置信息
主机监控系统: 安全防火墙已开启检查cc攻击-下载文件完成后等待10s 恢复访问,检查连接数低于峰值恢复访问
您的IP:216.73.216.110,2025-12-01 14:35:02,Processed in 0.01512 second(s).