c_cpp 要求在C中实现的问题,使用基于字符的答案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 要求在C中实现的问题,使用基于字符的答案相关的知识,希望对你有一定的参考价值。

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

/**
 * prototyping of question and answer framework
 */

struct question_t {
  const char* text;
  char correct_ans;
  struct _answers {
    const char* marks;
    const char** texts;
    size_t len;
  } answers;
};

void question_init(struct question_t* q, const char* text, const char* marks, const char** atexts, char correct)
{
  q->text = text;
  q->answers.len = strlen(marks);
  q->answers.marks = marks;
  q->answers.texts = atexts;
  q->correct_ans = correct;
}

void question_print(struct question_t* q)
{
  size_t i;
  printf("Q: %s\n", q->text);
  for(i = 0; i < q->answers.len; i++) {
    printf("(%c.) %s\n", q->answers.marks[i], q->answers.texts[i]);
  }
  puts("___________________________");
}

/**
 * prompts user to ask for answer,
 * this is not a retryable asking
 */
int question_ask(struct question_t* q)
{
  char user_answer;
  question_print(q);
  printf("Your answer: ");
  user_answer = fgetc(stdin);
  if(user_answer == q->correct_ans) {
    printf("\nCorrect!\n");
    return 0;
  } else {
    const char* corr;
    size_t j;
    for(j = 0; j < q->answers.len; j++) {
      if(q->answers.marks[j] == q->correct_ans) {
        printf("\nNot correct! The correct answer is %c:\n", q->correct_ans);
        printf("- %s\n", q->answers.texts[j]);
      }
    }
    return 0;
  }
  return 1;
}

int main(int argc, char const *argv[])
{
	struct question_t q1;
  const char* choices[] = {"red", "yellow", "blue"};
  question_init(&q1, "What color is the sky?", "abc", choices, 'c');
  question_ask(&q1);
	return 0;
}
/*
Q: What color is the sky?
(a.) red
(b.) yellow
(c.) blue
___________________________
Your answer: c

Correct!

*/

以上是关于c_cpp 要求在C中实现的问题,使用基于字符的答案的主要内容,如果未能解决你的问题,请参考以下文章

一组数据x[i],对其进行滑动平均滤波,得到在matlab中实现的程序,要求运行完能出对比图的。

C#...vb中replace方法,c#中实现的方法.

DialogFragment 中实现的 videoview 的 MediaController 未正确更新

安装 BPL 以及在其他 BPL 中实现的组件

c_cpp 在c中实现trie

c_cpp 在c中实现sizeof