Skip to content

Commit

Permalink
[EDIT]
Browse files Browse the repository at this point in the history
1. exp - Main - made default entry an input app, instead of hardcoded result
  • Loading branch information
DMitev11 committed Sep 13, 2022
1 parent 58de219 commit 80a7b6a
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 50 deletions.
37 changes: 21 additions & 16 deletions Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Generator{
private Random _random;


private Generator(Tokenizer tokenizer, int seed) {
private Generator(Tokenizer tokenizer, Random random) {
_tokenizer = tokenizer;
_random = new Random(seed);
_random = random;
}

public double Run() {
Expand Down Expand Up @@ -227,13 +227,18 @@ private double GenerateNumber(in List<GeneratorExpression> que, double min, doub
while(true) {
double def = Math.Floor(min + (max - min) * (_random).NextDouble());
bool found = false;
foreach(GeneratorExpression Generate in que) {
if(Generate.Run(def)){
number = def;
found = true;
break;
};
}
if(que.Count == 0) {
found = true;
number = def;
} else {
foreach(GeneratorExpression Generate in que) {
if(Generate.Run(def)){
number = def;
found = true;
break;
};
}
}
if(found) break;
}
return number;
Expand All @@ -257,21 +262,21 @@ private double GenerateRange() {
_tokenizer.Next();
_tokenizer.GenToken();
}
number = number != null? number : 0d;
number = number != null? number : 0d;
return (double) number;
}

private static double Generate(Tokenizer tokenizer, int seed){
Generator _generator = new Generator(tokenizer, seed);
private static double Generate(Tokenizer tokenizer, Random random){
Generator _generator = new Generator(tokenizer, random);
return _generator.Run();
}

public static double Generate(StringReader reader, int seed){
return Generate(new Tokenizer(reader), seed);
public static double Generate(StringReader reader, Random random){
return Generate(new Tokenizer(reader), random);
}

public static double Generate(string numberString, int seed){
return Generate(new Tokenizer(new StringReader(numberString)), seed);
public static double Generate(string numberString, Random random){
return Generate(new Tokenizer(new StringReader(numberString)), random);
}
}
}
22 changes: 15 additions & 7 deletions GeneratorParser.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Experiment {
class GeneratorParser {

public static List<double> Parse(in string[] set_, int seed) {
List<double> generatedSet = new List<double>{};
public static List<double> Parse(in string[] set_, Random random) {
List<double> generatedSet = new List<double>{};

for(int i = 0; i < set_.Length; i++) {
string builtSet = BuildSet(set_[i], generatedSet);
generatedSet.Add(Generator.Generate(builtSet, seed));
generatedSet.Add(Generator.Generate(builtSet, random));
}
return generatedSet;
}
Expand All @@ -20,14 +20,22 @@ public static string BuildSet(string set, in List<double> numberList, bool ignor
return set;
}
for(int i = 1; i <= indexClose - indexOpen -1; i++) {
index = index * 10 + (set[indexOpen + i] - '0');

}
if(set[indexOpen + i] == '-') {
index = -1 * (set[indexOpen + i + 1] - '0');
i++;
} else {
index = index * 10 + (set[indexOpen + i] - '0');
}
}
if(index >= numberList.Count) {
Console.WriteLine("There are only {0} numbers generated. Request for {1} number index failed", numberList.Count);
throw new Exception("Invalid character");
}
set = set.Substring(0, indexOpen) + numberList[index] + set.Substring(indexClose + 1);
if(index >= 0) {
set = set.Substring(0, indexOpen) + numberList[index] + set.Substring(indexClose + 1);
} else {
set = set.Substring(0, indexOpen) + numberList[numberList.Count + index] + set.Substring(indexClose + 1);
}
}
}
}
Expand Down
204 changes: 177 additions & 27 deletions exp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,186 @@

namespace Experiment {
public static class Program {

public static int Main(string[] args) {
// ex.
// "0:9",
// "10:99%({0})",
// "0:9"
Random random = new Random(Guid.NewGuid().GetHashCode());
List<string> set = new List<string>();
List<double> generatedSet = new List<double>();
Console.WriteLine("Please write several lines of number generation expressions");
while(true) {
string? read = Console.ReadLine();
if(read != null && read != ""){
try {
List<string> test = set;
test.Add(read);
generatedSet = GeneratorParser.Parse(test.ToArray(), random);
set.Add(read);
} catch(Exception e) {
Console.WriteLine(e);
}
} else break;
}

// ex.
// {2} * ({1} / {0})
// {4} ( the previous expression ) * {2} / {0}
List<string> expressions = new List<string>();
Console.WriteLine("Please write several lines of calculatory expressions");
while(true) {
string? read = Console.ReadLine();
if(read != null && read != ""){
try {
string builtExpression = GeneratorParser.BuildSet(read, generatedSet, true);
double expressionResult = ParserNode.Parse(builtExpression).Eval();
generatedSet.Add(expressionResult);
expressions.Add(read);
} catch (Exception e) {
Console.WriteLine(e);
}
} else break;
}
// ex.
// same
string resultExpression = "";
Console.WriteLine("Please write the result expression");
while(true) {
string? read = Console.ReadLine();
if(read != null && read != ""){
try {
string builtExpression = GeneratorParser.BuildSet(read, generatedSet, true);
double expressionResult = ParserNode.Parse(builtExpression).Eval();
resultExpression = read;
break;
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
// ex.
// " Мъж купува {0} топки и {2} кукли. Всички играчки струват {3}. {2}-те кукли струват {4} лв. Колко струва 1 топка?"
string title = "";
Console.WriteLine("Please write the title");
while(true) {
string? read = Console.ReadLine();
if(read != null && read != ""){
try {
string builtExpression = GeneratorParser.BuildSet(read, generatedSet, true);
title = read;
break;
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
int times = 5;
Console.WriteLine("How many times would you like the excercise to be generated");
while(true) {
string? read = Console.ReadLine();
if(read != null){
times = Int32.Parse(read);
break;
}
}

Console.WriteLine("Generating....");
for(int i = 0; i < times; i++) {
generatedSet = GeneratorParser.Parse(set.ToArray(), random);
foreach(string expression in expressions){
string builtExpression = GeneratorParser.BuildSet(expression, generatedSet, true);
double expressionResult = ParserNode.Parse(builtExpression).Eval();
generatedSet.Add(expressionResult);
}
string titleBuilt = GeneratorParser.BuildSet(title, generatedSet, true);
string resultBuilt = GeneratorParser.BuildSet(resultExpression, generatedSet, true);
double result = ParserNode.Parse(resultBuilt).Eval();
double[] answers = generateAnswers(result);

for(int j = 0; j < set.Count; j++) {
Console.WriteLine("Number set {0} generated number: {1}", set[j], generatedSet[j]);
}
for(int j = 0; j < expressions.Count; j++) {
Console.WriteLine("Expression set {0} generated number: {1}", expressions[j], generatedSet[j + set.Count]);
}
Console.WriteLine("Generated title is: {0}", titleBuilt);
Console.WriteLine("Result expression {0} generated: {1}", resultExpression, resultBuilt);
Console.WriteLine("Result is: {0}", result);
foreach(double answer in answers) {
Console.WriteLine("Generated answer: {0}", answer);
}
}
Console.ReadLine();
return 0;
}

public static double[] generateAnswers(double correctAnswer) {
Random random = new Random(Guid.NewGuid().GetHashCode() * (int) DateTimeOffset.Now.ToUnixTimeSeconds());
double coeff = 0.15;
if (correctAnswer <= 10) {
coeff = 4;
} else if (correctAnswer <= 20) {
coeff = 1;
}
int min = (int) Math.Round(correctAnswer == 0 ? -10 : Math.Round(correctAnswer - correctAnswer * coeff));
int max = (int) Math.Round(correctAnswer == 0 ? 10 : Math.Round(correctAnswer + correctAnswer * coeff));
List<double> answers = new List<double> {correctAnswer};

public static int Main(string[] args) {
string[] set = new[]{
"-999:999%(9||4&&8)==2",
"0:999%(4&&3&&2)",
"0:9 <= (4&&7&&1)",
"0:99 <= ({1})", "{0, 15, 24, 36}"};
const string expression = "{0} - {1} + {4} / {3}";
int seed = Guid.NewGuid().GetHashCode();
Random randomGenerator = new Random(seed);
for(uint i = 0; i < 5; i++ ) {
List<double> generatedSet = GeneratorParser.Parse(set, seed);
Console.WriteLine("Set `{0}` generated: {1}", set[0], generatedSet[0]);
Console.WriteLine("Set `{0}` generated: {1}", set[1], generatedSet[1]);
Console.WriteLine("Set `{0}` generated: {1}", set[2], generatedSet[2]);
Console.WriteLine("Set `{0}` generated: {1}", set[3], generatedSet[3]);
Console.WriteLine("Set `{0}` generated: {1}", set[4], generatedSet[4]);
string expressionBuilt = GeneratorParser.BuildSet(expression, generatedSet, true);
Console.WriteLine("Expression {0} would be: {1}", expression, expressionBuilt);
double number = ParserNode.Parse(expressionBuilt).Eval();
double answer1 = number + (randomGenerator.NextDouble() *2 - 1) * 15;
double answer2 = number + (randomGenerator.NextDouble() *2 - 1) * 15;
double answer3 = number + (randomGenerator.NextDouble() *2 - 1) * 15;
Console.WriteLine("Option 0 is: {0}", answer1);
Console.WriteLine("Option 1 is: {0}", answer2);
Console.WriteLine("Option 2 is: {0}", answer3);
Console.WriteLine("Result is: {0}", number);
Console.WriteLine();
while (answers.Count != 4) {
double incorrectAnswer = Math.Round(min + (max - min) * random.NextDouble());
bool set = true;
foreach(double answer in answers) {
if(answer == incorrectAnswer) {set = false; break;}
}
if(set) answers.Add(incorrectAnswer);
}
return shuffleArray(answers).ToArray();
}

public static List<double> shuffleArray(List<double> array) {
Random random = new Random(Guid.NewGuid().GetHashCode() * (int) DateTimeOffset.Now.ToUnixTimeSeconds());
for (int i = array.Count - 1; i > 0; i--) {
int j = (int) Math.Floor(random.NextDouble() * (i + 1));
double temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}

public static int Example(string[] args) {
string[] set = {
"0:9",
"10:99%({0})",
"0:9"
};
string[] expression = {"{2} * ({1} / {0})"};
const string title = " {1} / ? = {-1} ?";
Random randomGenerator = new Random(Guid.NewGuid().GetHashCode() * (int) DateTimeOffset.Now.ToUnixTimeSeconds());
for(uint i = 0; i < 5; i++ ) {
List<double> generatedSet = GeneratorParser.Parse(set, randomGenerator);
Console.WriteLine("Set `{0}` generated: {1}", set[0], generatedSet[0]);
Console.WriteLine("Set `{0}` generated: {1}", set[1], generatedSet[1]);
Console.WriteLine("Set `{0}` generated: {1}", set[2], generatedSet[2]);
// Console.WriteLine("Set `{0}` generated: {1}", set[3], generatedSet[3]);
// Console.WriteLine("Set `{0}` generated: {1}", set[4], generatedSet[4]);
string expressionBuilt = GeneratorParser.BuildSet(expression[0], generatedSet, true);
Console.WriteLine("Expression {0} would be: {1}", expression[0], expressionBuilt);
double number = ParserNode.Parse(expressionBuilt).Eval();
Console.WriteLine("Result is: {0}", number);
generatedSet.Add(number);
string titleBuilt = GeneratorParser.BuildSet(title, generatedSet, true);
Console.WriteLine("Title {0} would be: {1}", title, titleBuilt);
double answer1 = number + Math.Round((randomGenerator.NextDouble() *2 - 1) * 15);
double answer2 = number + Math.Round((randomGenerator.NextDouble() *2 - 1) * 15);
double answer3 = number + Math.Round((randomGenerator.NextDouble() *2 - 1) * 15);
Console.WriteLine("Option 0 is: {0}", answer1);
Console.WriteLine("Option 1 is: {0}", answer2);
Console.WriteLine("Option 2 is: {0}", answer3);
Console.WriteLine();
}
return 0;
}

Expand Down

0 comments on commit 80a7b6a

Please sign in to comment.