#include
#include
#include
#include "conio.h"
struct question {
char ask[200];
char answer[4][80];
int right;
struct question *next;
};
int MenuChoice(void);
struct question *InsertList(struct question *fst, const struct question *ad);
struct question *ListSeek(struct question *seek, long len, long max);
void GetQuestion(struct question *src);
void SaveFile(const struct question *ed, FILE *saf);
struct question *LoadFile(struct question *td, FILE *laf);
int GetAnswer(void);
void ExplainQuestion(const struct question *que, int n);
main()
{
struct question *start = NULL, temp;
long choice,line,c;
FILE *fp = fopen("kstm.txt", "a+");
//clrscr();
start = LoadFile(start, fp);
while ((choice = MenuChoice()) != 3)
if (choice == 1) {
GetQuestion(&temp);
start = InsertList(start, &temp);
++line;
}
else if (choice == 2){
c=600;
while(c>500)
{
printf("输入试题数目: ");
scanf("%d",&c);
}
line=c;
ExplainQuestion(start,line);
}
SaveFile(start,fp);
fclose(fp);
return 0;
}
struct question *ListSeek(struct question *seek, long len, long max)
{
int i;
srand(time(NULL));
while (i = rand() % max + len < max)
;
while (i--)
seek = seek->next;
return seek;
}
struct question *InsertList(struct question *fst, const struct question *ad)
{
struct question *newPtr = (struct question *)malloc(sizeof(struct question));
if (newPtr == NULL)
exit(0);
*newPtr = *ad;
newPtr->next = fst;
return newPtr;
}
void GetQuestion(struct question *src)
{
int i = 0;
printf("输入题干:\n");
scanf("%s", src->ask);
while (i < 4) {
printf("输入备选答案 %c:\n", i + 'A');
scanf("%s", src->answer[i++]);
}
src->right = GetAnswer();
}
struct question *LoadFile(struct question *td, FILE *laf)
{
struct question temp;
while (fread(&temp, 1, sizeof(struct question), laf))
td = InsertList(td, &temp);
return td;
}
void SaveFile(const struct question *ed, FILE *saf)
{
fclose(saf);
if ((saf = fopen("kstm.txt", "w")) == NULL)
return ;
while (ed) {
fwrite(ed, 1, sizeof(struct question), saf);
ed = ed->next;
}
}
int GetAnswer(void)
{
int c = 0;
fflush(stdin);
while (c < 'A' || c > 'D') {
printf("输入正确答案: ");
scanf("%c", &c);
}
return c;
}
void ExplainQuestion(const struct question *que, int n)
{
int i = 0, t = n;
char result[1001], *p = result;
for (i = 0;n--; que = que->next) {
printf("%s\nA.%s\nB.%s\nC.%s\nD.%s\n\n", que->ask, que->answer[0], que->answer[1],
que->answer[2], que->answer[3]);
if ((*p = que->right) == (*(p + 1) = GetAnswer()))
++i;
p += 2;
}
*p = '\0';
printf("\n%-13s%-13s%s\n", "正确答案", "你的回答", "评价");
for (p = result; *p != '\0'; p += 2)
printf("%-13c%-13c%s\n", *p, *(p + 1), *p == *(p + 1) ? "正确" : "错误");
printf("\n你回答了 %d 题,正确 %d 题,错误 %d题,最后得分 %f\n", t, i,t-i, (float)i/t*100.00);
}
int MenuChoice(void)
{
int value;
printf("1 - 录入试题\n2 - 回答试题\n3 - 退出\n");
scanf("%d", &value);
return value;
}
我资料里有联系方式
用链表还是数组做?