Code:
# include <conio.h>
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include <stdlib.h>
# define que 15
typedef struct
{
char quest[100];
char ans[4][50];
int result;
}question;
int Login()
{
clrscr();
char user[20],pass[20];
fflush(stdin);
printf("\n\n\n\t\t- Nhap Ten: "); gets(user);
fflush(stdin);
printf("\n\t\t- Nhap Mat khau: "); gets(pass);
if(strcmp(user,"nguyendien")==0 && strcmp(pass,"minhdien")==0)
return 1;
else
{
clrscr();
printf("\n\t\t\tBan khong co quyen nay.");
return 0;
}
}
void write()
{
int i=0;
char ch;
FILE *fp;
question q;
fp=fopen("quest.txt","wb");
if(fp==NULL)
{
clrscr();
printf("\n\n\t\tLoi mo tap tin.");
getch();
return;
}
do
{
clrscr();
++i;
printf("\n\n\t\t\tCau hoi %d:\n",i);
printf("\n+ Nhap cau hoi: ");
fflush(stdin); gets(q.quest);
for(int j=0; j<4; j++)
{
printf("\n\t- Dap an %d: ",j+1);
fflush(stdin);
gets(q.ans[j]);
}
printf("\n\t\tDap an dung: "); fflush(stdin); scanf("%d",&q.result);
printf("\n\n\t\tBan muon luu cau hoi nay (c/k): ");
char c;
fflush(stdin); c=getchar();
if(toupper(c)=='C')
{
clrscr();
fwrite(&q,sizeof(question),1,fp);
printf("\n\n\tLuu Thanh cong.");
}
printf("\n\n\tBan muon tiep tuc (c/k): ");
fflush(stdin);
ch=getchar();
}
while(toupper(ch)=='C');
fclose(fp);
}
void Display()
{
clrscr();
FILE *fp;
question q;
int i=0;
fp=fopen("quest.txt","rb");
if(fp==NULL)
{
clrscr();
printf("\n\tLoi mo tap tin.");
getch();
return;
}
while(fread(&q,sizeof(question),1,fp)>0)
{
printf("\n\t\tCau hoi %d:\n",++i);
printf("\n\nCau Hoi: "); puts(q.quest);
for(int j=0; j<4; j++)
{
printf("\n\t%d) ",j+1);
puts(q.ans[j]);
}
printf("\n\t\tDap an dung: %d",q.result);
printf("\n\t-----------------------------------------------\n");
}
fclose(fp);
getch();
}
void Edit()
{
FILE *fp;
int i;
fp=fopen("quest.txt","r+b");
if(fp==NULL)
{
clrscr();
printf("\n\t\tKhong co du lieu.");
getch();
return;
}
fseek(fp,0,SEEK_END);
int size=sizeof(question);
int len=ftell(fp)/size;
Display();
question q;
printf("\nBan muon sua cau may: "); scanf("%d",&i);
if(i>0 && i<=len)
{
fseek(fp,(i-1)*size,SEEK_SET);
fread(&q,size,1,fp);
printf("\n\tTruoc khi sua:\n");
printf("\n\nCau Hoi: "); puts(q.quest);
for(int j=0; j<4; j++)
{
printf("\n\t%d) ",j+1);
puts(q.ans[j]);
}
printf("\n\t-----------------------------------------------\n");
printf("\n\n\tSua lai:\n");
printf("\n+ Nhap cau hoi: ");
fflush(stdin); gets(q.quest);
for(j=0; j<4; j++)
{
printf("\n\t- Dap an %d: ",j+1);
fflush(stdin);
gets(q.ans[j]);
}
printf("\n\t\tDap an dung: "); fflush(stdin); scanf("%d",&q.result);
printf("\nBan muon thay doi (c/k): ");
fflush(stdin); char ch=getchar();
if(toupper(ch)=='C')
{
fseek(fp,size*(i-1), SEEK_SET);
fwrite(&q,size,1,fp);
clrscr();
printf("\n\n\n\t\tThay doi thanh cong.");
}
}
fclose(fp);
}
void Insert()
{
FILE *fp;
char ch;
question q;
fp=fopen("quest.txt","ab");
if(fp==NULL)
{
clrscr();
printf("\n\n\t\tLoi mo tap tin.");
getch();
return;
}
fseek(fp,0,SEEK_END);
int i=ftell(fp)/sizeof(question);
do
{
clrscr();
++i;
printf("\n\n\t\t\tCau hoi %d:\n",i);
printf("\n+ Nhap cau hoi: ");
fflush(stdin); gets(q.quest);
for(int j=0; j<4; j++)
{
printf("\n\t- Dap an %d: ",j+1);
fflush(stdin);
gets(q.ans[j]);
}
printf("\n\t\tDap an dung: "); fflush(stdin); scanf("%d",&q.result);
printf("\n\n\t\tBan muon luu cau hoi nay (c/k): ");
char c;
fflush(stdin); c=getchar();
if(toupper(c)=='C')
{
clrscr();
fwrite(&q,sizeof(question),1,fp);
printf("\n\n\tLuu Thanh cong.");
}
printf("\n\n\tBan muon tiep tuc (c/k): ");
fflush(stdin);
ch=getchar();
}
while(toupper(ch)=='C');
fclose(fp);
}
void Luat()
{
clrscr();
printf("\n\n\t\tLuat choi.\n");
printf("\n\t- Ban se vuot qua 15 cau hoi.\n");
printf("\n\t- Tra loi dung moi cau ban duoc 10 diem.\n");
printf("\n\t- Ban co 2 quyen tro giup:\n");
printf("\n\n\t\t+ Doi cau hoi.");
printf("\n\n\t\t+ Bo hai phuong an sai.");
printf("\n\t--------------------------------------------------");
printf("\n\n\t\tBam mot phim de tro lai: "); getch();
}
void Game()
{
int Diem=0,kq, chon, amo=0;
int a[que],h=0,ran,stt=0,test1=0,test2=0;
FILE *fp;
question q;
fp=fopen("quest.txt","rb");
if(fp==NULL)
{
clrscr();
printf("\n\n\n\t\t\tChua co du lieu.");
return;
}
fseek(fp,0,SEEK_END);
int amount=ftell(fp)/sizeof(question);
do
{
tt1:
clrscr();
randomize();
int found;
do
{
found=0;
ran=random(que);
for(int j=0; j<h; j++)
if(a[j]==ran) found=1;
if(ran==que) found=1;
}while(found==1);
a[h++]=ran;
if(chon!=5 && chon!=6) stt++;
fseek(fp,ran*sizeof(question),SEEK_SET);
fread(&q,sizeof(question),1,fp);
printf("\n\n\n\t\tCau hoi %d:\n",stt);
printf("\n\nCau Hoi: "); puts(q.quest);
for(int j=0; j<4; j++)
{
printf("\n\t%d) ",j+1);
puts(q.ans[j]);
}
tt:
if(chon==5) test1=1;
if(chon==6) test2=1;
if(test1==0 && test2==0)
{
printf("\n\n\t\tDoi cau hoi - Bam phim 5");
printf("\n\n\t\tBo hai phuong an sai - Bam phim 6");
}
else if(test1==1 && test2==0) printf("\n\n\t\tBo hai phuong an sai - Bam phim 6");
else if(test2==1 && test1==0) printf("\n\n\t\tDoi cau hoi - Bam phim 5");
printf("\n\n\n\t\tHay chon: ");scanf("%d",&chon);
if(chon>=1 && chon <=4)
{
if(chon==q.result)
{
Diem+=10;
kq=0;
}
else
{
clrscr();
printf("\n\n\t\tBan da tra loi sai.\n");
printf("\n\t\tBan duoc %d diem.",Diem);
kq=1;
}
}
else if(chon==5 && test1==0)
{
clrscr();
goto tt1;
}
else if(chon==6 && test2==0)
{
clrscr();
printf("\n\n\n\t\tCau hoi %d:\n",stt);
printf("\n\nCau Hoi: "); puts(q.quest);
int t=0;
for(int j=0; j<4; j++)
{
if((j+1)!=q.result && t<2)
t++;
else
{
printf("\n\t%d) ",j+1);
puts(q.ans[j]);
}
}
goto tt;
}
else goto tt;
++amo;
if(amo==que-1) kq=1;
}
while(kq==0);
if(amo==que-1)
{
clrscr();
printf("\n\n\t\tChuc Mung ban! Ban da chien thang tro choi nay.");
}
getch();
}
void Play()
{
char ch, chon;
do
{
ll:
clrscr();
printf("\n\n\t\t\tCHUONG TRINH TRAC NGHIEM.\n");
printf("\n\n\t\t---------------------------------------------\n");
printf("\n\n\t\t1- Luat Choi.");
printf("\n\n\t\t2- Bat dau choi.");
printf("\n\n\t\t3- Thoat ra chuong trinh chinh.");
printf("\n\n\t\t---------------------------------------------\n");
printf("\n\n\t\tHay chon: "); fflush(stdin);scanf("%c",&chon);
switch(chon)
{
case '1':
Luat();
goto ll;
break;
case '2':
Game();
break;
case '3':
return;
default:
clrscr();
printf("\n\n\tNhap khong dung. Bam 1 phim de tro lai.");
getch();
goto ll;
break;
}
printf("\n\n\tBan muon choi lai(c/k): ");
fflush(stdin);
ch=getchar();
}while(toupper(ch)=='C');
getch();
}
void main()
{
char ch, chon;
do
{
clrscr();
printf("\n\n\t\t\tTRO CHOI TRAC NGHIEM.\n");
printf("\n\t\t\t\t\tNguyen Dien - CNTT - A2\n");
printf("\n\t\t------------------------------------------------");
printf("\n\n\t\t\t1) Nhap cau hoi.");
printf("\n\n\t\t\t2) Hien Thi tat ca cau hoi.");
printf("\n\n\t\t\t3) Sua chua cau hoi.");
printf("\n\n\t\t\t4) Them cau hoi.");
printf("\n\n\t\t\t5) Bat Dau tro choi.");
printf("\n\n\t\t-----------------------------------------------");
printf("\n\n\t\t\tHay Chon: "); fflush(stdin); scanf("%c",&chon);
if(chon=='1' || chon=='2' || chon=='3' || chon=='4')
if(Login()==0) goto kt;
switch(chon)
{
case '1':
write();
clrscr();
break;
case '2':
Display();
clrscr();
break;
case '3':
Edit();
break;
case '4':
Insert();
clrscr();
break;
case '5':
Play();
clrscr();
break;
default:
clrscr();
printf("\n\n\t\tNhap khong dung.");
}
kt:
printf("\n\n\tBan muon tro lai chuong trinh chinh (C/K): ");
fflush(stdin);
ch=getchar();
}
while(toupper(ch)=='C');
getch();
}