Freedomlater
Freedomlater
2018-01-17 02:38

求大神帮修改,我是菜鸟

20
  • struct
  • 链表
  • 结构
  • switch

#include
#include
#include
#include

//定义试题结构体
typedef struct node
{
int id; //试题编号
char problem; //题干
char optionA; //选项A
char optionB; //选项B
char optionC; //选项C
char optionD; //选项D
char ans; //答案
struct node*pNext; //指向下一个试题的指针
}Node;

void insert() //录入试题
{
void insertNode(int id,char problem[200],char optionA[100],char optionB[100],
char optionC[100],char optionD[100],char ans);
int id;
char problem[200];
char optionA[100];
char optionB[100];
char optionC[100];
char optionD[100];
char ans;
char choose='Y';
while(toupper(choose)=='Y')
{printf("\n**********录入试题**********");
printf("\n请输入试题编号:");
scanf("%d",&id);
getchar();
printf("请输入题干:");
gets(problem);
printf("请输入选项A:");
gets(optionA);
printf("请输入选项B:");
gets(optionB);
printf("请输入选项C:");
gets(optionC);
printf("请输入选项D:");
gets(optionD);
ans=getchar();
getchar();
insertNode(id,problem,optionA,optionB,optionC,optionD,toupper(ans));
printf("\n继续录入(Y\N)?");
scanf("%c",&choose);
getchar();
}

}Node*pHead=NULL; //存放试题的链表的首节点地址
void view() //查看试题
{
Node*tempNode=pHead;
while(tempNode!=NULL)
{printf("%d\n","%s\n","%s\n","&s\n","%s\n","%s\n","%s\n",tempNode->id,tempNode->problem,
tempNode->optionA,tempNode->optionB,tempNode->optionC,tempNode->optionD,tempNode->ans);}
}
void save() //保存试题
{
Node*tempNode=pHead;
freopen("D:\****","w",stdout);
while(tempNode!=NULL)
{printf("%d\n",tempNode->id);
printf("%s\n",tempNode->problem);
printf("%s\n",tempNode->optionA);
printf("%s\n",tempNode->optionB);
printf("%s\n",tempNode->optionC);
printf("%s\n",tempNode->optionD);
printf("%s\n",tempNode->ans);
tempNode=tempNode->pNext;
}
fclose(stdout);
freopen("CON","w",stdout);
}
//申请储存空间
void insertNode(int id,char problem[200],char optionA[100],char optionB[100],char optionC[100],char optionD[100],char ans)
{
Node*pNew=(Node*)malloc(sizeof(Node));
pNew->id=id;
pNew->problem;
pNew->optionA;
pNew->optionB;
pNew->optionC;
pNew->optionD;
pNew->ans;
pNew->pNext=NULL;
if(pHead==NULL)
pHead=pNew;
else
{Node*tempNode=pHead;
pHead->pNext=pNew;
}
}
void mod() //修改试题
{ void view();
int id; //题号
char problem[200]; //题干
char optionA[100]; //选项A
char optionB[100]; //选项B
char optionC[100]; //选项C
char optionD[100]; //选项D
char ans; //答案
char choose;
Node*tempNode=pHead;

Node*previousNode=pHead;
printf("\n请输入要修改的题号:");
scanf("%d",&id);
getchar();

while(tempNode!=0)  //从第一题开始找,直到找到指定的试题
{
if(id==tempNode->id)
    break;
previousNode=tempNode;    
tempNode=tempNode->pNext;  
}
if(tempNode==NULL)
{
    printf("该试题不存在");
    return;
}
//显示试题信息    
printf("%d\n","%s\n","%s\n","&s\n","%s\n","%s\n","%s\n",tempNode->id,tempNode->problem,
tempNode->optionA,tempNode->optionB,tempNode->optionC,tempNode->optionD,tempNode->ans);

printf("请输入修改后的试题信息");
printf("\n请输入试题编号:");
      scanf("%d",&id);
      getchar();
      printf("请输入题干:");
      gets(problem);
      printf("请输入选项A:");
      gets(optionA);
      printf("请输入选项B:");
      gets(optionB);
      printf("请输入选项C:");
      gets(optionC);
      printf("请输入选项D:");
      gets(optionD);
      ans=getchar();
      getchar();
      printf("是否修改(Y\N)?");
      scanf("%c",&choose);
      getchar();
      if(toupper(choose)=='Y')
      {
           insertNode(id,problem,optionA,optionB,optionC,optionD,toupper(ans));
      }
      //显示所有试题
    view();

}
void del() //删除试题
{

int id;
char choose;
Node*tempNode=pHead; //存放试题链表的首地址,即第一道试题(结构体)的地址
Node*previousNode=pHead; //指定试题的上一道试题的地址
printf("\请输入要删除的试题题号:");
scanf("%d",&id);
getchar();
while(tempNode!=0) //从第一题开始找,直到找到指定的试题
{
if(id==tempNode->id)
break;
previousNode=tempNode; //当前节点是下一个节点的前节点
tempNode=tempNode->pNext; //指向下一节点
}
if(tempNode==NULL)
{
printf("该试题不存在");
return;
}
//显示试题信息

printf("%d\n","%s\n","%s\n","&s\n","%s\n","%s\n","%s\n",tempNode->id,tempNode->problem,
tempNode->optionA,tempNode->optionB,tempNode->optionC,tempNode->optionD,tempNode->ans);
printf("要删除该题么(Y\N)?");
scanf("%c",choose);
getchar();
if(toupper(choose)=='Y')
{
//tempNode是要删除的试题的地址
if(tempNode==pHead)
pHead=tempNode->pNext;
else
previousNode->pNext=tempNode->pNext;
free(tempNode);
}

}
void test(); //考试

int main()
{ int a=0;
char choose=' ';
void insert();
void view();
void save();
void mod();

void del();
void test();
while(choose!='0')
{
printf("\n**********主菜单**********");
printf("\n 欢迎进入考试系统");
printf("\n 1:试题管理");
printf("\n 2:开始考试");
printf("\n 3:退出");
printf("\n**************************");
printf("\n请选择:");
choose=getchar();
getchar();
switch(choose)
{
case '1':
printf("*****************************\n");
printf("**********1.新增题目*********\n");
printf("**********2.删除题目*********\n");
printf("**********3.修改题目*********\n");
printf("**********4.查看题目*********\n");
printf("**********5.保存*************\n");
printf("**********6.返回上一级*******\n");
printf("请选择:");
scanf("%d",a);

            switch(a)
            {
            case 1:
                insert();
                break;
            case 2:
                del();
                break;
            case 3:
                mod();
                break;
            case 4:
                view();
                break;
            case 5:
                break;
            case 6:
                break;

            }

            break;
        case '2':
            break;
        case '3':
            break;
        default :
            break;

      }

}

return 0;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐

换一换