基于C语言的历史学习答题软件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//最大题目数量
#define MAX_QUESTIONS 10
//最大答案长度
#define MAX_ANSWER_LEN 50

//题目结构体
typedef struct{
    char question[MAX_ANSWER_LEN];
    char answer[MAX_ANSWER_LEN];
}Question;

//添加一个题目
void add_question(Question *question,int *num_questions,char *question,char *answer)
{
    if(*num_question >= MAX_QUESTIONS)
    {
        printf("题目数量已经达到上限.\n");
        return;
    }
    Question new_question;
    strncpy(new_question.answer,answer,MAX_ANSWER_LEN);
    question[*num_quetions]=new_quesiton;
    (*num_questions)++;
    printf("题目已经添加成功.\n");
}

//删除一个题目
void delete_question(Question *questions,int *num_quesitons,int index)
{
    if(index <0 || index >= *num_questions)
    {
        printf("无效题目索引.\n");
        return;
    }
    for(int i = index; i< *num_questions-1;i++)
    {
        questions[i]=question[i+1];
        
    }
    (*num_questions)--;}
    printf("题目已经添加成功.\n");
 }

//显示所有题目
void display_qustions(Question *questions,int num_questions)
{
    if(num_questions == 0)
    {
        printf("没有题目.\n");
        return;
    }
    printf("所有题目:\n");
    for(int i = 0; i < num_questions;i++)
    {
        printf("%d. %s\n",i+1,questions[i].question);
    }
}

//判断答案是否正确
void check_answer(Question *questions,int num_questions,int index,char *answer)
{
    if(index <0 || index >= num_questions)
    {
        printf("无效的题目索引.\n");
        return;
    }
    if(strcmp(questions[index].answer) == 0)
    {
        printf("答案正确.\n");
    }else
    {
        printf("答案错误,正确答案是:%s\n",questions[i].answer);
    }
}

int main()
{
    Question questions[MAX_QUESTIONS];
    int num_questions = 0;
    while(1)
    {
        printf("\n\n1.添加题目\n");
        printf("2.删除题目\n");
        printf("3.显示所有题目\n");
        printf("4.回答问题\n");
        printf("5.退出\n");
        
        int choice;
        printf("请输入选择项:");
        scanf("%d",&choice);
        
        switch(choice)
        {
            case 1:
            {
                char quesitons[MAX_ANSWER_LEN];
                char answer[MAX_ANSWER_LEN];

                printf("请输入问题:");
                scanf("%s",question);
                printf("请输入答案:");
                scanf("%s",answer); 
                add_question(questions,&num_questions,question,answer);
                break;                 
            }
            case 2:
            {
                int index;
                printf("请输入要删除的题目索引:");
                scanf("%d",&index);
                delete_question(questions,&num_questions,index-1);
                break;
            }                
            case 3:
            {
                display_questions(questions,num_questions);
                break;
            }
            case 4:
            {
                int index;
                char answer[MAX_ANSWER_LEN];
                printf("请输入要回答的题目索引:");
                scanf("%d",&index);
                printf("请输入答案:");
                scanf("%s",answer);
                check_answer(questions,num_questions,index-1,answer);
                break;
            }
            case 5:
            {
                printf("谢谢使用!\n");
                exit(0);
            }
            default:
            {
                printf("无效的选项.\n");
                break;
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值