📄 run_examination.c
字号:
#include<stdio.h>
struct examination
{
int acctNum;
int type;
int knowledge;
char content[100];
char answer[10];
};
int enterChoice(void);
void textFile(FILE *);
void updateRecord(FILE *);
void newRecord(FILE *);
void deleteRecord(FILE *);
void newBuilding(void);
main()
{
FILE * cfPtr;
int choice;
if((cfPtr=fopen("data.dat","r+")) == NULL)
{
printf("File could not be opened. ");
getch();
}
else
{
while((choice=enterchoice())!=5)
{
swich(choice)
{
case 1: textFile(cfPtr);break;
case 2: updateRecord(cfPtr);break;
case 3: newRecord(cfPtr);break;
case 4: deleteRecord(cfPtr);break;
}
}
}
fclose(cfPtr);
return 0;
}
void newBuilding(void)
{
int i;
int j = 0;
FILE * cfPtr;
if((cfPtr = fopen("newBuilding","w")) == NULL)
{
printf("file \"newBuilding\" could not be opened.");
getch();
}
else
{
for(i=1;i<=100;i++)
fwrite(&j,sizeof(int),1,cfPtr);
fclose(cfPtr);
}
return 0;
}
void textFile(FILE *readPtr_data)
{
int i;
int acctNum1[100];
int newbuilding = 0;
int acctNum = 0;
int type = 0;
int knowledge = 0;
char y_n;
struct examination ex1;
FILE * writePtr_content;
FILE * writePtr_answer;
FILE * readPtr_newbuilding;
newBuilding();
if((writePtr_content = fopen("content.dat","a+")) == NULL)
{
printf("file could not be opened .");return 1;
}
if((writePtr_answer = fopen("answer.dat","a+")) == NULL)
{
printf("file could not be opened .");return 1;
}
if((readPtr_newbuilding = fopen("newbuilding.dat","r+")) == NULL)
{
printf("file could not be opened .");return 1;
}
mark1: printf("是否显示所有试题(Y/N)? ");
mark2: y_n = getch();
printf("%c",y_n);
if(y_n == 'y')
{
rewind(readPtr_data);
printf("全部试题如下: \n");
printf("题目号 试题内容 答案 类型 知识点 \n");
while(!feof(readPtr_data))
{
fread(&ex1,sizeof(struct examination),1,readPtr_data);
if(ex1.acctNum != 0)
printf("%d%-3s%-3s%-3d%-3d \n",ex1.acctNum,ex1.content,ex1.answer,ex1.type,ex1.knowledge);
}
printf("是否再显示所有试题 ?");
goto mark2;
}
else
{
if(y_n == 'n') goto mark3;
else goto mark1;
}
mark3:
printf("please enter 题型 (1~3) ?");
type = getch();
printf("%d",type);
while(type!=1||type!=2||type!=3)
{
printf("题型 输入有误,请重新输入:");
type = getch();
printf("%d",type);
}
printf("please enter 知识点 (1~15) ?");
scanf("%d",&knowledge);
while(knowledge<1||knowledge>15)
{
printf("知识点 输入有误,请重新输入:");
scanf("%d",&knowledge);
}
printf("以下是所有与您输入的参数有关的试题 :");
printf("题目号 试题内容 答案 类型 知识点 \n");
mark6:
rewind(readPtr_data);//从这以下为显示信息
rewind(readPtr_newbuilding);
for(i = 0;i<100;i++) acctNum1[i] = 0; i = 0;//对试题编号数组初始化
while(!feof(readPtr_data))
{
fread(%ex1,sizeof(struct examination),1,readPtr_data);
if(ex1.acctNum!=0 && ex1.type == type && ex1.knowledge == knowledge)
{
fseek(readPtr_newbuilding,(ex1.acctNum-1)*sizeof(int),SEEK_SET);
fread(&newbuilding,sizeof(int),1,readPtr_newbuilding);
if(newbuilding != 0)
break;
else
{
printf("%d%-3s%-3s%-3d%-3d \n",ex1.acctNum,ex1.content,ex1.answer,ex1.type,ex1.knowledge);
acctNum1[i] = ex1.acctNum;
i++;
}
}
}
printf("请输入您想出的试题编号: ?");
mark5:
scanf("%d",&acctNum);
while(acctNum<1||acctNum>100)
{
printf("您输入的编号不在题库范围内,请考虑好再输入: ?");
scanf("%d",&acctNum);
}
for(i=0;i<100;i++)
{
if(acctNum1[i] == acctNum) goto mark4;
}
printf("您输入的编号不在上述结果中,请看清楚再输入: ?");
goto mark5;
mark4:
fseek(readPtr_data,(acctNum - 1)*sizeof(struct examination),SEEK_SET);
fread(&ex1,sizeof(struct examination),1,readPtr_data);
fprintf(writePtr_content,"%d%-3s \n",ex1.acctNum,ex1.content);
fprintf(writePtr_answer,"%-3s%-3d%-3d \n",ex1.answer,ex1.type,ex1.knowledge);
fseek(readPtr_newbuilding,(ex1.acctNum-1)*sizeof(int),SEEK_SET);
newbuilding = 1;
fwrite(&newbuilding,sizeof(int),1,readPtr_newbuilding);
printf("试题 %d 添加成功!",acctNum);
printf("是否还出这种同题型,同只是点的试题?(Y/N) ");
mark7:
y_n = getch();
printf("%c",y_n);
if(y_n = 'y') goto mark6;
else
{
if(y_n = 'n')
{
printf("是否继续出题?(Y/N) ");
mark8:
y_n = getch();
printf("%c",y_n);
if(y_n =='y') goto mark1;
else
if(y_n == 'n')
{
fclose(writePtr_content);
fclose(writePtr_answer);
fclose(readPtr_newbuilding);
return 0;
}
else goto mark8;
}
else goto mark7;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -