乒乓球单打比赛程序模拟

  1. // pingpangcompete.cpp : Defines the entry point for the console application.
  2. //VS2008
  3. //编程题目
  4. //乒乓球单打比赛程序模拟。比赛的双方简称A和B,他们的比赛规则采用:
  5. //1,比赛胜负采用7局4胜制:谁先取得4局胜利就赢得比赛;
  6. //2,每局比分采用11分制: 
  7. //      A、如果比赛双方谁获得11分并且领先对方2分或2分以上,则该局比赛结束,获得11分一方获胜;
  8. //      B、如果一方获得11分,但领先对手的分数不足2分,则还要继续比赛,直至一方分数领先另一方2分以上,
  9. //          该局比赛才结束,领先对手2分以上的一方获胜;
  10. //3,发球规则:
  11. //      在比分10比10之前,每人可以连续发球两次,后换对手发球两次;在比分10比10之后,每人发1次球后换发;
  12. //4,得分方法:
  13. //      每次发球后得分的信息由函数int  Shot()获得,如shot返回1则表示发球的一方得分,如返回0则表示接球的一方得分;
  14. //5,首局比赛由A先发球,次局比赛由B先发球,依次顺序循环;
  15. //
  16. //程序要求:
  17. //1,请按照上面的规则编写程序,并调用函数Output将结果输出。
  18. //      其中函数Shot和Output,以及结构体Score_t由题目提供,不得修改。
  19. //2,程序要求逻辑清楚、结构良好,符合编码规范要求,否则扣分,最多扣除题目分数的三分之一。
  20. //3,程序能够运行,运行结果正确。
  21. //4,题意理解不正确扣分。  
  22. //5,输出文件默认为d:/ scores.txt
  23. //输入文件D:/book1.txt模板如下
  24. //1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
  25. //1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
  26. //1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
  27. //1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
  28. //1 1 0 0 1 1 0 0 1 1 0
  29. //0 0 1 1 0 0 1 1 0 0 1
  30. //1 1 0 0 1 1 0 0 1 1 0
  31. //0 0 1 1 0 0 1 1 0 0 1
  32. //1 1 0 0 1 1 0 0 1 1 0
  33. //0 0 1 1 0 0 1 1 0 0 1
  34. //1 1 0 0 1 1 0 0 1 1 0
  35. //0 0 1 1 0 0 1 1 0 0 1
  36. //此时输出文件d:/scores.txt内容为:
  37. //================A vs B Result====================
  38. //A vs B = 4 : 2
  39. //1st Match Scores is 19 : 17
  40. //2st Match Scores is 17 : 19
  41. //3st Match Scores is 12 : 10
  42. //4st Match Scores is 10 : 12
  43. //5st Match Scores is 11 : 0
  44. //6st Match Scores is 11 : 0
  45. #include "stdafx.h"
  46. #include <stdio.h>
  47. #include <iostream>
  48. using namespace std;
  49. //使用的数据结构:
  50. typedef struct {
  51.     char scoreA;    // A选手局得分
  52.     char scoreB;    // B选手局得分
  53. }Match_t;           //局比分信息
  54. typedef struct {
  55.     char An;            // A选手赢的局数
  56.     char Bn;            // B选手赢的局数
  57.     Match_t  Match[7];  //最多7局比分
  58. }Score_t;
  59. //从文件读取得分
  60. //如shot返回1则表示发球的一方得分,如shot返回0则表示接球的一方得分;
  61. int shot()
  62. {
  63.     FILE* fp; 
  64.     static int nIndex = 0;
  65.     static int inputData[200] = {-1};
  66.     static int isfirst = 1;
  67.     int i;
  68.     if (isfirst)
  69.     {
  70.         fp = fopen("D://book1.txt""r");
  71.         for (i = 0; i < 200; i++)
  72.         {
  73.             if (fscanf(fp, "%d,", &inputData[i]) == EOF) 
  74.                 break;
  75.         }
  76.         isfirst = 0;
  77.         fclose(fp);
  78.     }
  79.     if (nIndex == 200) 
  80.         return -1;
  81.     return inputData[nIndex++];
  82. }
  83. //关于void Output(Score_t *p)函数;
  84. //输出比分到文件
  85. void Output(Score_t *p)
  86. {
  87.     FILE* fp = fopen("d://scores.txt""w");
  88.     int i;
  89.     fprintf(fp, "================A vs B Result====================/n");
  90.     fprintf(fp, "A vs B = %d : %d/n", p->An, p->Bn);
  91.     for (i = 0; i < p->An + p->Bn; i++)
  92.     {
  93.         fprintf(fp, "%dst Match Scores is %d : %d/n", i+1, p->Match[i].scoreA, p->Match[i].scoreB);
  94.     }
  95.     fprintf(fp, "/n");
  96.     fclose(fp);
  97. }
  98. //处理比赛过程并输出比赛结果
  99. void Compete()
  100. {
  101.     Score_t stScore;
  102.     bool bSucceed = true;   //判断比赛是否正常结束
  103.     //初始化比分
  104.     stScore.An = 0;
  105.     stScore.Bn = 0;
  106.     for(int i = 0; i < 7; i++)
  107.     {
  108.         stScore.Match[i].scoreA = 0;
  109.         stScore.Match[i].scoreB = 0;
  110.     }
  111.     int iRound = 0;         //比赛的局数
  112.     int iAGetBall = 2;      //判断发球人,0 A接球,2 A第一次发球,1 A第二次发球
  113.     int iBGetBall = 0;      //判断发球人,0 B接球,2 B第一次发球,1 B第二次发球
  114.     int iCount = 0;         //回合数
  115.     while (stScore.An < 4 && stScore.Bn < 4)
  116.     {
  117.         ++iCount;
  118.         int iResult = shot();   //获得本回合得分结果
  119.         //接球方得分
  120.         if (0 == iResult)
  121.         {
  122.             if (iAGetBall)
  123.             {
  124.                 ++(stScore.Match[iRound].scoreB);   //B得分
  125.                 cout << "第" << iCount << "回合:" << "/tiResult=" << iResult <<"/t/t/tB得分:" << (int)stScore.Match[iRound].scoreB << endl;
  126.             }
  127.             else
  128.             {
  129.                 ++(stScore.Match[iRound].scoreA);   //A得分
  130.                 cout << "第" << iCount << "回合:" << "/tiResult=" << iResult <<"/tA得分:" << (int)stScore.Match[iRound].scoreA << endl;
  131.             }
  132.         }
  133.         //发球方得分
  134.         else if (1 == iResult)
  135.         {
  136.             if (iAGetBall)
  137.             {
  138.                 ++(stScore.Match[iRound].scoreA);   //A得分
  139.                 cout << "第" << iCount << "回合:" << "/tiResult=" << iResult <<"/tA得分:" << (int)stScore.Match[iRound].scoreA << endl;
  140.             }
  141.             else
  142.             {
  143.                 ++(stScore.Match[iRound].scoreB);   //B得分
  144.                 cout << "第" << iCount << "回合:" << "/tiResult=" << iResult <<"/t/t/tB得分:" << (int)stScore.Match[iRound].scoreB << endl;
  145.             }
  146.         }
  147.         //发球数达到200,中止比赛
  148.         else if (-1 == iResult)
  149.         {
  150.             cout << "发球数达到200,运动员被累死,比赛中止!" << endl;
  151.             bSucceed = false;
  152.             break;
  153.         }
  154.         //非法数据
  155.         else
  156.         {
  157.             cout << "输入数据非法!" << endl;
  158.             bSucceed = false;
  159.             break;
  160.         }
  161.         //判断本局是否有人胜利,如已有人胜利则开始下一局
  162.         if ((abs(stScore.Match[iRound].scoreA - stScore.Match[iRound].scoreB) >= 2)
  163.             && (stScore.Match[iRound].scoreA >= 11 || stScore.Match[iRound].scoreB >= 11))
  164.         {
  165.             if (stScore.Match[iRound].scoreA > stScore.Match[iRound].scoreB)
  166.             {
  167.                 ++(stScore.An);     //A赢一局
  168.                 cout << "A赢一局=========================================================" << endl;
  169.             }
  170.             else
  171.             {
  172.                 ++(stScore.Bn);     //B赢一局
  173.                 cout << "B赢一局=========================================================" << endl;
  174.             }
  175.             ++iRound;       //开始下一局
  176.             //首局比赛由A先发球,次局比赛由B先发球,依次顺序循环;
  177.             if (0 == iRound % 2)
  178.             {
  179.                 iAGetBall = 2;
  180.                 iBGetBall = 0;
  181.             }
  182.             else
  183.             {
  184.                 iAGetBall = 0;
  185.                 iBGetBall = 2;
  186.             }
  187.         }
  188.         else    //如本局未决出胜者,本局比赛继续
  189.         {
  190.             //在比分10比10之后,每人发1次球后换发
  191.             if (stScore.Match[iRound].scoreA >= 10 && stScore.Match[iRound].scoreB >= 10)
  192.             {
  193.                 if (1 == iAGetBall)
  194.                 {
  195.                     iAGetBall = 0;
  196.                     iBGetBall = 1;
  197.                 }
  198.                 else if (2 == iAGetBall)
  199.                 {
  200.                     iAGetBall = 1;
  201.                 }
  202.                 else if (1 == iBGetBall)
  203.                 {
  204.                     iAGetBall = 1;
  205.                     iBGetBall = 0;
  206.                 }
  207.                 else if (2 == iBGetBall)
  208.                 {
  209.                     iBGetBall = 1;
  210.                 }
  211.             }
  212.             else        //在比分10比10之前,每人可以连续发球两次,后换对手发球两次;
  213.             {
  214.                 if (1 == iAGetBall)
  215.                 {
  216.                     iAGetBall = 0;
  217.                     iBGetBall = 2;
  218.                 }
  219.                 else if (2 == iAGetBall)
  220.                 {
  221.                     iAGetBall = 1;
  222.                 }
  223.                 else if (1 == iBGetBall)
  224.                 {
  225.                     iAGetBall = 2;
  226.                     iBGetBall = 0;
  227.                 }
  228.                 else if (2 == iBGetBall)
  229.                 {
  230.                     iBGetBall = 1;
  231.                 }
  232.             }
  233.         }
  234.     }
  235.     //输出最终结果
  236.     if (bSucceed)
  237.     {
  238.         cout << "比赛成功结束,请查看得分文件!" << endl;
  239.         Output(&stScore);
  240.     }
  241.     else
  242.     {
  243.         cout << "比赛异常中止,请查看得分文件!" << endl;
  244.         Output(&stScore);
  245.     }
  246. }
  247. //主函数
  248. int _tmain(int argc, _TCHAR* argv[])
  249. {
  250.     //处理比赛过程并输出比赛结果
  251.     Compete();
  252.     return 0;
  253. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值