gpt4 book ai didi

c# - 匿名函数列表或数千个类

转载 作者:行者123 更新时间:2023-11-30 14:53:47 25 4
gpt4 key购买 nike

我希望创建一个在线测验,可以从数千个编程问题中提出任何问题。每个问题都是通过一个函数创建的,该函数被赋予一个 int 数组,其值确定显示的确切问题。我将每个问题作为一个类:

public class AddingTwoDigitNumbers : IQuestion
{
public string QName() { return "Adding Two-Digit Numbers" };
public int[] QParams() { return int[]() {Random(10, 99), Random(10, 99) };
public void Question(int[] values) {
Console.WriteLine(string.Format("What is {1} + {2}?", values[0], values[1]);
}
public void Answer(int[] values) {
Console.WriteLine(values[0] + values[1]).ToString());
}
}

QParams 创建 int 数组(以准确确定所创建的问题),该数组同时提供给 QuestionAnswer 以创建问答。

我想要一个 List 的问题,可通过 QName 搜索,但我宁愿不必创建(和命名)数千个全部实现 IQuestion 的类.

这是我的第二个解决方案:

public class Question
{
public string QName { get; set; }
public Func<int[]> QParams { get; set; }
public Action<int[]> Question { get; set; }
public Action<int[]> Answer { get; set; }
}

public class QuestionRepository
{
public static Dictionary<string, Question> Questions = new Dictionary<string, Question>();
public static void AddQuestions(Question[] qs) {
foreach (Question q in qs) Questions.Add(q.QName, q);
}
}

public class FirstSetOfQuestions
{
static void AddQuestions()
{
QuestionRepository.AddQuestions(new Question[]
{
new Question()
{
QName = "Adding Two-Digit Numbers",
QParams = () => int[]() {Random(10, 99), Random(10, 99) },
Question = (v) => {Console.WriteLine(string.Format("What is {1} + {2}?", v[0], v[1]);},
Answer = (v) => {Console.WriteLine(values[0] + values[1]).ToString());}
},
new Question()
{
QName = "Subtracting Three-Digit Numbers",
QParams = () => int[]() {Random(100, 999), Random(100, 999) },
Question = (v) => {Console.WriteLine(string.Format("What is {1} - {2}?", v[0], v[1]);},
Answer = (v) => {Console.WriteLine(values[0] - values[1]).ToString());}
}
}
}
}

所以我的问题是哪个更好?我是否创建了数千个类,必须为每个类提供一个名称,或者我是否创建了数千个匿名函数和一个使用(我假设)委托(delegate)来存储这些函数的类?如果我有成千上万的问题,或者甚至有更好的方法来做这件事,第二种解决方案是否有问题?

(显然我想提出的问题比这里显示的要复杂得多,涉及分数、代数等)

最佳答案

只是为了让您开始使用流畅的语法,并在其中加入一些 stub 和想法。

class Question
{
public string Name { get; set; }
public string QuestionFormat { get; set; }
public List<Range> Args { get; set; }
public Expression<Func<int[], int>> ValExp { get; set; }

public Question(string name, string questionFormat)
{
this.Name = name;
this.QuestionFormat = questionFormat;
this.Args = new List<Range>();
}

public Question Rand(int min, int max)
{
this.Args.Add(new Range(min, max));
return this;
}

public void Val(Expression<Func<int[], int>> exp)
{
this.ValExp = exp;
}

public CompiledQuestion Compile()
{
// Generate args in the appropriate ranges
// Evaluate the result with the ValExp
// Return a new CompiledQuestion with the information -
// basically just replacing Args, ValExp with RealArgs, Val
}

public ICoolDataObject Save()
{
}

public static Question Load(ICoolDataObject hmm)
{
}
}

class Range
{
public int Min { get; set; }
public int Max { get; set; }

public Range(int min, int max)
{
this.Min = min;
this.Max = max;
}
}

这几乎很有趣,现在可以提出问题了:

new Question("simple addition",
"whats {0} + {1}?")
.Rand(10, 99)
.Rand(10, 99)
.Val(v => v[0] + v[1]);

很明显,您可以添加一些验证检查以避免由于工作到很晚而导致的参数数量错误,并使用 doubledecimal 而不是 int 任何地方。

关于c# - 匿名函数列表或数千个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com