mn giúp giùm mình phần chương trình này với mai phải báo cáo r
lỗi: k hiện kq phần đếm
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include<windows.h>
#define MAX 100
int diem;
int vitri[MAX];
int DEM=0;
int cau[MAX];
char trloi[MAX],ten[30];
//---------------------------cau truc cau hoi------------------------------
typedef struct
{int cau;
char nd[1000];
char da;
}cauhoi;
cauhoi dulieu[MAX];
//-----------------------------------khai bao cac ham----------------------
void xuat(int i);
void choi(); // xuat cac cau ngau nhien cho nguoi choi chon
int nap(int linhvuc);
char traloi() ;
void ketqua();
int chon();
//-------------------------nap cau hoi tu tep vao cau truc--------------------------
int napch(int linhvuc)
{
int i=1; //khoi tao i =1 de nhap vaocau thu 1
char c;
int j;
FILE *f;
if(linhvuc==1)
f=fopen("G:/download/cauhoi.txt","r");
else if(linhvuc==2)
f=fopen("e:\\mahoa2.txt","rb");
else
f=fopen("e:\\mahoa3.txt","rb"); /*chon lua cac tap du lieu tuong tung ma nguoi choi chon*/
if(f==NULL)
{ //Kiem Tra Ton Tai FILE//
printf("\n khong mo duoc du lieu ");
getch();
return(1) //Khong mo duoc du lieu tra ve 1//
;}
c=!EOF;
while(c!=EOF) //Xu ly viec nap du lieu tu File//
{
j=0;
dulieu[i].cau=i;
while(c!=EOF&&(c=fgetc(f))!='#') //trong khi chua ket thuc tep va chua gap ki tu #
{
dulieu[i].nd[j]=c; // nap du kieu vao cau thu [i]
j++;
}
c=fgetc(f);dulieu[i].da=(c^'A'); //nap cau tra loi 1 ki tu sau # giai ma//
DEM=i; // so cau hoi =i;
i++;
}
printf("\nSO CAU:%d",(DEM-1));
return(0);
}
//---------------------------------ham chon tep du lieu------------------
int chon()
{
char nhom;
do
{
printf("\t\t\t <<<CHAO MUNG BAN DEN VOI TRO CHOI>>> \t\t\t\n");
printf("Chon goi cau hoi\n");
printf("A.Goi cau hoi 1\t B.Goi cau hoi 2\tC.Goi cau hoi 3\n");
nhom=toupper(getche());
}
while(nhom!='A'&&nhom!='B'&&nhom!='C');
if(nhom=='A')
return(1);
else if(nhom=='B')
return(2);
else
return(3);
}
//---------------------------bat dau choi-------------------------------
void choi() //lay cac cau hoi ngau nhien
{
int MANG[MAX],i; //bat dau choi diem =0
diem=0; //khoi dau so cau hoi k=0
srand((unsigned int)time(NULL));
for ( i = 0; i < DEM-2; i++) // Khoi tao ranseed
{
MANG[i] = i; //KHOI TAO CAC GIA TRI CUA MANG [MAX]
vitri[i]=0; // khoi tao gia tri mang vitri =0 lam co hieu
}
for (i = DEM - 2; i > 0; i--) //lay cac so ngau nhien
{
int r = rand() % (i + 1);
int tam = MANG[i];
MANG[i] = MANG[r]; //loai truong hop r=0
MANG[r] = tam;
}
for(i = 0; i <10; i++) //goi cac cau hoi ngau nhien TRANH GOI 0
{
xuat(MANG[i]+1);
trloi[i]= traloi();
cau[i]=(MANG[i]+1); //luu cac cau tra loi
if(dulieu[MANG[i]+1].da==trloi[i]) //tranhtruong hop vitri[j]=0;
{
diem ++ ;
vitri[i]=1;
}
}
}
//---------------------------xuat cau hoi------------------------------------
void xuat(int i)
{
char c;
printf("\n cau %d",dulieu[i].cau);
printf(" \n%s",dulieu[i].nd);
printf("\n---------------------------------------------------------------------");
}
//----------------------------tra loi------------------------
char traloi()
{
char c;
printf("\n\n\t\tnhap cau tra loi : A hoac B hoac C hoac D:\n\n\t\t\t\t");
do
{
c=toupper(getche());
}while(c!='A'&&c!='B'&&c!='D'&&c!='C');
return(c);
}
//----------------------------ket qua ---------------------------------
void ketqua()
{
int i;
char t;
do
{printf("\n\n\t\tban da hoan tat bai trac nghiem\n");
printf("\n\n\t\tcac cau ban da tra loi\t\tdap an :");
for(i=0;i<10;i++)
{
printf("\n\n\t\t\tcau:%d",cau[i]);
printf("\t%c\t",trloi[i]);
printf("\t%c",dulieu[cau[i]].da);
if(vitri[i]!=0)
printf("\t\t\t dung");
}
printf("\n\n\t\ttong so cau tra loi dung %d",diem);
printf("\n\n\t\tbam phim T de thoat : ");
t=toupper(getche());
}while(t!='T');
}
//---------------------------main------------------------------------
main()
{
int linhvuc ,f;
char c;
system("cls");
do
{
tt:
f=1; //khoi tao co hieu cho f=1;
linhvuc=chon();
f=napch(linhvuc);
if(f==0) //mo duoc du lieu moi co the choi() va co ketqua();
{
choi();
ketqua();
}
system("cls");
printf("\n\n\n\n\n\t\t\t ban muon tiep tuc choi ?(Y/N)");
c=toupper(getche());
if(c!='Y'&&c!='N')
goto tt;
}while( c!='N');
}
Bookmarks