01/10/2018, 12:00
Cho nhận xét về cách dùng boolean và rút gọn chương trình
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int so_bimat=0,so_hientai=0,count=0,mode=1,nguoichoi=1,MAX=0,capdo=0;
const int MIN = 1;
printf("===CHON MODE===
");
printf("1 nguoi choi bam phim 1
");
printf("2 nguoi choi bam phim 2
");
scanf("%d",&mode);
int tieptuc_game = 1;
while(tieptuc_game)
{
do
{
printf("Xin moi ban chon cap do choi
");
printf("de
");
printf("trung binh
");
printf("kho
");
scanf("%d",&capdo);
switch(capdo)
{
case 1:
MAX = 100;
break;
case 2:
MAX *=10;
break;
case 3:
MAX *=100;
break;
default:
printf("Xin moi ban chon cap do choi
");
break;
}
}
while (capdo>3);
srand(time(NULL));
so_bimat = (rand()%(MAX - MIN + 1) + MIN);
if (mode == 1)
{
do
{
printf("So can tim la bao nhieu?");
scanf("%d",&so_hientai);
count++;
if (so_hientai > so_bimat)
printf("nho hon !
");
else if (so_hientai < so_bimat)
printf("lon hon !
");
else
printf("Chuc mung! ban da tim so bi mat trong %d lan doan
",count);
}
while (so_bimat != so_hientai );
}
else
{
do
{
printf("So can tim la bao nhieu?
");
switch (nguoichoi)
{
case 1:
printf("nguoi choi thu %d:",nguoichoi);
scanf("%d",&so_hientai);
if (so_hientai > so_bimat)
printf("nho hon !
");
else if (so_hientai < so_bimat)
printf("lon hon !
");
else
printf("Chuc mung! nguoi choi thu %d da thang
",nguoichoi);
nguoichoi++;
break;
case 2 :
printf("nguoi choi thu %d:",nguoichoi);
scanf("%d",&so_hientai);
if (so_hientai > so_bimat)
printf("nho hon !
");
else if (so_hientai < so_bimat)
printf("lon hon !
");
else
printf("Chuc mung! nguoi choi thu %d da thang
",nguoichoi);
nguoichoi--;
break;
}
}
while (so_bimat != so_hientai );
}
printf("Ban co muon tiep tuc game?
");
printf("Co / bam phim 1
");
printf("Khong / bam phim 0
");
scanf("%d",&tieptuc_game);
}
return 0;
Bài liên quan