具体的程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.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 = 0, c;
FILE *fp = fopen(“kstm.dat”, “a+”);
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 || c > line) {
printf(“请输入要回答的问题数量: “);
scanf(“%d”, &c);
}
ExplainQuestion(start, line);
}
SaveFile(start, fp);/*进行最后的工作*/
fclose(fp);
return 0;
}
/*ListSeek函数确定一个读取答案的位置,len代表要读取的答案数,max代表列表的长度*/
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.dat”, “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’) {/*确保输入的答案是A, B, 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道题目, 得分: %.2f\n\n”, t, i, (float)i / t * 100.00);
}
/*选择菜单*/
int MenuChoice(void)
{
int value;
printf(“1 – 添加选择题\n2 – 回答选择题\n3 – 退出\n”);
scanf(“%d”, &value);
return value;
}
本人从网上下载后发现问题很多,进入TC测试后发现很多错误有高手看不过的帮测试下啊
1)试题录入:可随时增加试题到试题库中,要求题库中不少于50个题目;
2)试题抽取:每次从试题库中可以随机抽出N道题(N由键盘输入);
3)答题:用户可实现输入自己的答案,忽略大小写;
4)自动判卷:系统可根据用户答案与标准答案的对比实现判卷并给出成绩;
5)最后要求能显示出错题目和正确答案。
>> 本文固定链接: http://www.vcgood.com/archives/1929
#include <time.h>
#define MAX_NUM 51
int rand_func()
{
return (int)rand()%MAX_NUM;
}
void main()
{
srand(time(0));
/* put your code here */
/* call func just like this */
int n;
n=rand_func();
}
还有一点,这是关于1-50随即抽题的程序,我测试的时候也有点问题有朋友能帮我把他修改后插到上面的程序里吗 谢谢,非常谢谢了