Bowling Scores

Bowling, as you may know, consists of ten frames. In each frame, there is a chance to knock down ten pins by throwing one or two balls down the bowling alley. To start, you roll the first ball of a given frame. If all ten pins are knocked down, a strike is achieved for that frame, and you earn a frame score of 10 plus the total number of pins knocked down on the next two rolls. If any pins remain standing, you roll a second ball for the frame. After this roll, if all pins are down, you have a spare, and a frame score of 10 plus the number of pins knocked down on the next roll. If any pins remain standing after the second roll, then the result is an open frame, and the frame score obtained is given by the total number of pins knocked down on the two rolls of the frame. A strike or spare occurring on the last (the tenth), results in two or one additional rolls, respectively. The total score for a game is the sum of all the individual frame scores.
Getting twelve strikes in a row results in the maximum attainable score of 300. In this case, twelve rolls are made. The least number of rolls possible is eleven and the greatest is twenty-one. In fact there is a correlation between the number of rolls in a game and the game score. Many rolls correspond to low scores and few rolls to high scores. The better a bowler you are, the fewer rolls you get!
The problem is to print the total score for each of a series of games, as well as the minimum, average, and maximum scores. Assume the input is given as a series of roll scores for each game and includes at least one game.
Solution:
#include <iostream>
using namespace std;
 
int gamecalc();
//calculates gamescore for the current game
void framecalc(int *pgamescore,int *pframe,int *prollscore,int *pr1);
//calculates the framescore for the current game,adds it to gamescore
//increases frame by 1 and updates rollscore and r1
void update(int gamescore,int *pnumber,int *pmin,int *pmax,int *ptotal);
//prints gamescore,and updates number,min,max and total to reflect gamescore
 
void main()
{
    int gamescore;//score for the current game
    int number=0;//current number of games played
    int max=0,min=300;//current maximum and minimum game score
    int total=0;//accumulated total of all game scores
    float average;//average of all game scores
    int newgame=1;
   
    while(newgame){
       gamescore=gamecalc();
       update(gamescore,&number,&min,&max,&total);
       cout<<"If you want to bowl another game,enter a 1."<<endl;
       cout<<"If you wish to stop,enter a 0."<<endl;
       cin>>newgame;
    }
   
    average=((float)total)/number;
    cout<<"Minimum game score "<<min<<endl;
    cout<<"Average game score "<<average<<endl;
    cout<<"Maximum game score "<<max<<endl;
    cout<<"Number of games "<<number<<endl;
}
 
int gamecalc()
{
    int gamescore=0;//score for the current game
    int frame=1;//the current frame
    int rollscore;//first roll score of the current frame
    int r1;//succeeding roll score after rollscore
    cout<<"Please enter the number of pins down on this roll"<<endl;
    cin>>rollscore;
    cout<<"Please enter the number of pins down on this roll"<<endl;
    cin>>r1;
    while(frame<=10){
       framecalc(&gamescore,&frame,&rollscore,&r1);
    }
    return gamescore;
}
 
void framecalc(int *pgamescore,int *pframe,int *prollscore,int *pr1)
{
    int framescore;//score for current frame
    int r2;//rollscore after r1
    if(*prollscore==10){
       cout<<"Please enter the number of pins down on this roll"<<endl;
       cin>>r2;
       framescore=10 + *pr1 + r2;
       *prollscore=*pr1;
       *pr1=r2;
    }
    else if((*prollscore + *pr1)==10){
       cout<<"Please enter the number of pins down on this roll"<<endl;
       cin>>r2;
       framescore=10+r2;
       *prollscore=r2;
       if(*pframe<10){
           cout<<"Please enter the number of pins down on this roll"<<endl;
           cin>>(*pr1);
       }
    }
    else{
       framescore=*prollscore + *pr1;
       if(*pframe < 10){
           cout<<"Please enter the number of pins down on this roll"<<endl;
           cin>>(*prollscore);
           cout<<"Please enter the number of pins down on this roll"<<endl;
           cin>>(*pr1);
       }
    }
    *pgamescore += framescore;
    *pframe += 1;
}
 
void update(int gamescore,int *pnumber,int *pmin,int *pmax,int *ptotal)
{
    cout<<"gamescore "<<gamescore<<endl;
    *pnumber += 1;
    *ptotal += gamescore;
    if(gamescore < *pmin)
       *pmin = gamescore;
    if(gamescore > *pmax)
       *pmax = gamescore;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值