ボーリングのスコア計算(C言語で)
10/30 の課題:
ボーリングの点数計算を行うプログラムを作成する。
1投ごとに倒数を入力し、全て入力し終えたらトータルスコアを表示する。
スペア、ストライクによって点数計算が変わる。
スペアの場合は、次の倒数を加算する。
ストライクの場合は、次とその次の倒数を加算する。
10フレーム目は、2投した段階で、スペア、ストライクであれば、3投目を行うことができる。
10フレーム目は総倒数がそのまま点数となる。
#include <stdio.h>
int main(void){
int th1,th2,th3;
int sc=0, sc2=0;
int total=0;
int fr;
for(fr=1 ; fr<=9 ; fr++){
printf("[%d]\n",fr);
printf("1投目:");
scanf("%d",&th1);
/* error */
if(th1<0 || th1>10){
printf("error!!!\n\n");
fr--;
continue;
}
if(th1 != 10){
printf("2投目:");
scanf("%d",&th2);
/* error */
if(th2<0 || th1+th2>10){
printf("error!!!\n\n");
fr--;
continue;
}
}
if(sc != 0){
total += th1;
sc--;
if(th1!=10 && sc != 0){
total += th2;
sc--;
}
}
if(sc2 != 0){
total += th1;
sc2--;
if(th1!=10 && sc2 != 0){
total += th2;
sc2--;
}
}
total += th1;
if(th1 != 10){
total += th2;
}
if(th1!=10 && (th1+th2 == 10)){
sc=1;
}else if(th1 == 10){
if(sc == 1){
sc2 = 2;
}
if(sc2 != 2){
sc =2;
}
}
}
/* 10 times */
while(1){
printf("[10]\n");
printf("1投目:");
scanf("%d",&th1);
/* error */
if(th1<0 || th1>10){
printf("error!!!\n\n");
continue;
}
printf("2投目:");
scanf("%d",&th2);
/* error */
if(th1<10 && (th2<0 || th1+th2>10)){
printf("error!!!\n\n");
continue;
}else if(th1==10 && (th2<0 || th2>10)){
printf("error!!!\n\n");
continue;
}
if(th1+th2>=10){
printf("3投目:");
scanf("%d",&th3);
/* error */
if((th2==10 || th1+th2==10) && (th3<0 || th3>10)){
printf("error!!!\n\n");
continue;
}else if(th2<10 && (th3<0 || th2+th3>10)){
printf("error!!!\n\n");
continue;
}
}
if(sc != 0){
total += th1;
sc--;
if(sc != 0){
total += th2;
sc--;
}
}
if(sc2 != 0){
total += th1;
sc2--;
if(sc2 != 0){
total += th2;
sc2--;
}
}
total += (th1+th2);
if(th1+th2>=10){
total += th3;
}
break;
}
printf("\ntotal:%d\n",total);
return 0;
}
PR