孩子上小学,老师要求孩子每天做40到口算题,于是编写了一个口算题生成器,还真的挺好用的!!
其实非常简单,就是制作了一个ExerciseMaker类,
using System;
using System.Collections.Generic;
using System.Text;
namespace MathExercise
{
class ExerciseMaker
{
int ExerciseLevel = 21;//运算难度
List<String> Exam;
//
public bool MakeOne(out String s)
{
//+,-,++,--, +-,-+,生成一个0~5的随机数分别表示运算的类型
Random op_type = new Random();
int op1 = op_type.Next(0, 6);
//根据生成的运算符进行出题,生成响应的题
int opOne = op_type.Next(0, ExerciseLevel);
int opTwo = op_type.Next(0, ExerciseLevel);
int opThree = op_type.Next(0, ExerciseLevel);
int opAdd = opOne + opTwo;
if (opAdd >= ExerciseLevel)
{
opOne = opOne / 2;
opTwo = opTwo / 2;
opAdd = opOne + opTwo;
}
int opMax = Math.Max(opOne, opTwo);
int opMin = Math.Min(opOne, opTwo);
switch (op1)
{
case 0://+
s = opOne.ToString() + "+" + opTwo.ToString() + "=";
return true;
case 1://-
s = opMax.ToString() + "-" + opMin.ToString() + "=";
return true;
case 2://++
if (opOne + opTwo + opThree >= ExerciseLevel)
{
opOne = opOne / 3;
opTwo = opTwo / 3;
opThree = opThree / 3;
}
s = opOne.ToString() + "+" + opTwo.ToString() + "+" + opThree.ToString() +"=";
return true;
case 3://--
//int opThreerange = (ExerciseLevel - opAdd);
opThree = op_type.Next(opAdd,ExerciseLevel);
s = opThree.ToString() + "-" + opTwo.ToString() + "-" + opOne.ToString() + "=";
return true;
case 4://+-
if (opAdd < opThree)
{
opThree = op_type.Next(0, opAdd);
}
s = opOne.ToString() + "+" + opTwo.ToString() + "-"+opThree.ToString() + "=";
return true;
case 5://-+
if (opMax - opMin + opThree >= ExerciseLevel)
{
opThree = op_type.Next(0, ExerciseLevel - opMax + opMin);
}
s = opMax.ToString() + "-" + opMin.ToString() + "+"+opThree.ToString() + "=";
return true;
default:
break;
}
s = "";
return false;
}
public bool MakeExam(UInt16 ExerciseNum)
{
Exam = new List<string>();
int count = 0;
for (UInt16 i = 0; i < ExerciseNum; i++)
{
String ex = new String("???");
if(MakeOne(out ex))
{
Exam.Add(ex);
count++;
}
}
if (count == ExerciseNum)
{
return true;
}
return false;
}
public List<String> GetExam()
{
return Exam;
}
}
}