Создать n наборов тестов - C#
Формулировка задачи:
"Тесты". Преподавателю необходимо создать n наборов тестов. В каждом наборе
должны быть представлены вопросы разной тематики, по возможности равномерно. У
преподавателя составлен общий список вопросов, упорядоченный по их названиям.
Помогите преподавателю распределить вопросы по тестам.
Указание: тематику вопросов следует задать перечислением. Следует определить класс
Question, среди полей которого будет поле theme, заданное перечислением.
Решение задачи: «Создать n наборов тестов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestCreator
{
class Program
{
private static Random rand = new Random();
private static List<Question> questions = new List<Question>();
private static List<Test> tests = new List<Test>();
static void Main(string[] args)
{
CreateQuestion(Question.Themes.Логика, "1");
CreateQuestion(Question.Themes.Логика, "2");
CreateQuestion(Question.Themes.Логика, "3");
CreateQuestion(Question.Themes.Логика, "4");
CreateQuestion(Question.Themes.Логика, "5");
CreateQuestion(Question.Themes.Логика, "6");
CreateQuestion(Question.Themes.МатАнализ, "1");
CreateQuestion(Question.Themes.МатАнализ, "2");
CreateQuestion(Question.Themes.МатАнализ, "3");
CreateQuestion(Question.Themes.МатАнализ, "4");
CreateQuestion(Question.Themes.МатАнализ, "5");
CreateQuestion(Question.Themes.МатАнализ, "6");
CreateQuestion(Question.Themes.ДискретМат, "1");
CreateQuestion(Question.Themes.ДискретМат, "2");
CreateQuestion(Question.Themes.ДискретМат, "3");
CreateQuestion(Question.Themes.ДискретМат, "4");
CreateQuestion(Question.Themes.ДискретМат, "5");
CreateQuestion(Question.Themes.ДискретМат, "6");
Console.Write("Сколько тестов создать? ");
int count = int.Parse(Console.ReadLine());
Console.Write("Количество вопросов в тесте: ");
int qCount = int.Parse(Console.ReadLine());
for (int i = 0; i < count; i++)
CreateTest(qCount);
int j = 0;
foreach(var test in tests)
{
Console.WriteLine($"======Тест №{++j}=======");
foreach (var question in test.questions)
Console.WriteLine($"Тема вопроса: {question.theme.ToString()}. Вопрос: {question.text}");
}
Console.ReadKey();
}
static void CreateQuestion(Question.Themes theme, string text)
{
var question = new Question();
question.theme = theme;
question.text = text;
questions.Add(question);
}
static void CreateTest(int qCountInTest)
{
int qCount = qCountInTest / (int)Question.Themes.Макс;
var test = new Test();
int theme = (int)Question.Themes.Логика;
for (int i = 0, temp = 0; i < qCountInTest; i++, temp++)
{
if (temp % qCount == 0 && temp > 0
&& theme + 1 < (int)Question.Themes.Макс)
theme++;
var question = GetRandomQuestionByTheme(test, (Question.Themes)theme);
if (question == null)
{
theme++;
continue;
}
test.questions.Add(question);
}
tests.Add(test);
}
static Question GetRandomQuestionByTheme(Test test, Question.Themes theme)
{
var qArray = questions.FindAll(x => x.theme == theme);
if (qArray.Count == 0)
return null;
HashSet<Question> sQuestions = new HashSet<Question>();
foreach (var question in qArray)
sQuestions.Add(question);
sQuestions.ExceptWith(test.questions.ToArray());
if (sQuestions.Count == 0)
return null;
return sQuestions.ElementAt(rand.Next(0, sQuestions.Count - 1));
}
}
public class Test
{
public HashSet<Question> questions = new HashSet<Question>();
}
public class Question : IComparable<Question>
{
public Themes theme { get; set; }
public string text { get; set; }
public int CompareTo(Question other)
{
return 1;
}
public enum Themes : int
{
Логика = 0,
МатАнализ,
ДискретМат,
Макс
};
}
}