Создать n наборов тестов - C#

Узнай цену своей работы

Формулировка задачи:

"Тесты". Преподавателю необходимо создать n наборов тестов. В каждом наборе должны быть представлены вопросы разной тематики, по возможности равномерно. У преподавателя составлен общий список вопросов, упорядоченный по их названиям. Помогите преподавателю распределить вопросы по тестам. Указание: тематику вопросов следует задать перечислением. Следует определить класс Question, среди полей которого будет поле theme, заданное перечислением.

Решение задачи: «Создать n наборов тестов»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestCreator
  6. {
  7.     class Program
  8.     {
  9.         private static Random rand = new Random();
  10.         private static List<Question> questions = new List<Question>();
  11.         private static List<Test> tests = new List<Test>();
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             CreateQuestion(Question.Themes.Логика, "1");
  16.             CreateQuestion(Question.Themes.Логика, "2");
  17.             CreateQuestion(Question.Themes.Логика, "3");
  18.             CreateQuestion(Question.Themes.Логика, "4");
  19.             CreateQuestion(Question.Themes.Логика, "5");
  20.             CreateQuestion(Question.Themes.Логика, "6");
  21.  
  22.             CreateQuestion(Question.Themes.МатАнализ, "1");
  23.             CreateQuestion(Question.Themes.МатАнализ, "2");
  24.             CreateQuestion(Question.Themes.МатАнализ, "3");
  25.             CreateQuestion(Question.Themes.МатАнализ, "4");
  26.             CreateQuestion(Question.Themes.МатАнализ, "5");
  27.             CreateQuestion(Question.Themes.МатАнализ, "6");
  28.  
  29.             CreateQuestion(Question.Themes.ДискретМат, "1");
  30.             CreateQuestion(Question.Themes.ДискретМат, "2");
  31.             CreateQuestion(Question.Themes.ДискретМат, "3");
  32.             CreateQuestion(Question.Themes.ДискретМат, "4");
  33.             CreateQuestion(Question.Themes.ДискретМат, "5");
  34.             CreateQuestion(Question.Themes.ДискретМат, "6");
  35.  
  36.  
  37.             Console.Write("Сколько тестов создать? ");
  38.             int count = int.Parse(Console.ReadLine());
  39.  
  40.             Console.Write("Количество вопросов в тесте: ");
  41.             int qCount = int.Parse(Console.ReadLine());
  42.  
  43.             for (int i = 0; i < count; i++)
  44.                 CreateTest(qCount);
  45.  
  46.             int j = 0;
  47.             foreach(var test in tests)
  48.             {
  49.                 Console.WriteLine($"======Тест №{++j}=======");
  50.                 foreach (var question in test.questions)
  51.                     Console.WriteLine($"Тема вопроса: {question.theme.ToString()}. Вопрос: {question.text}");
  52.             }
  53.  
  54.             Console.ReadKey();
  55.         }
  56.  
  57.         static void CreateQuestion(Question.Themes theme, string text)
  58.         {
  59.             var question = new Question();
  60.             question.theme = theme;
  61.             question.text = text;
  62.  
  63.             questions.Add(question);
  64.         }
  65.  
  66.         static void CreateTest(int qCountInTest)
  67.         {
  68.             int qCount = qCountInTest / (int)Question.Themes.Макс;
  69.             var test = new Test();
  70.  
  71.             int theme = (int)Question.Themes.Логика;
  72.             for (int i = 0, temp = 0; i < qCountInTest; i++, temp++)
  73.             {
  74.                 if (temp % qCount == 0 && temp > 0
  75.                     && theme + 1 < (int)Question.Themes.Макс)
  76.                     theme++;
  77.  
  78.                 var question = GetRandomQuestionByTheme(test, (Question.Themes)theme);
  79.                 if (question == null)
  80.                 {
  81.                     theme++;
  82.                     continue;
  83.                 }
  84.  
  85.                 test.questions.Add(question);
  86.             }
  87.  
  88.             tests.Add(test);
  89.         }
  90.  
  91.         static Question GetRandomQuestionByTheme(Test test, Question.Themes theme)
  92.         {
  93.             var qArray = questions.FindAll(x => x.theme == theme);
  94.             if (qArray.Count == 0)
  95.                 return null;
  96.  
  97.             HashSet<Question> sQuestions = new HashSet<Question>();
  98.             foreach (var question in qArray)
  99.                 sQuestions.Add(question);
  100.  
  101.             sQuestions.ExceptWith(test.questions.ToArray());
  102.             if (sQuestions.Count == 0)
  103.                 return null;
  104.  
  105.             return sQuestions.ElementAt(rand.Next(0, sQuestions.Count - 1));
  106.         }
  107.     }
  108.  
  109.     public class Test
  110.     {
  111.         public HashSet<Question> questions = new HashSet<Question>();
  112.     }
  113.  
  114.     public class Question : IComparable<Question>
  115.     {
  116.         public Themes theme { get; set; }
  117.  
  118.         public string text { get; set; }
  119.  
  120.         public int CompareTo(Question other)
  121.         {
  122.             return 1;
  123.         }
  124.  
  125.         public enum Themes : int
  126.         {
  127.             Логика = 0,
  128.             МатАнализ,
  129.             ДискретМат,
  130.             Макс
  131.         };
  132.     }
  133. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.867 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы