I've been working on a math tutoring program that generates unique math problems pertaining to differing types of math (decimals, variables, exponents, fractions, etc) and nearly any combination of each type of math. I have my class file generate and random amount of operands and populate those with values(decimal or int), then a corresponding amount operators, and assembling that as a string (as a unified data type).

Now here is where I'm looking for advice. After the problem string is generated I use a string builder. If the user is working with variables I take a random operand value and replace it with a variable. I do the same thing for exponents, but I remove that operand, and instert that operand with an exponent attached (EG 5 is removed and 5^3 is inserted in its place). The problem here is if there is more than 1 instance of the operand value 5, they would both be replaced (with either variable or exponent) which I don't want.

Here is my other issue The fractional and parenthetical math are dependent upon each other for computational purposes. A fraction would be split into 4 different problems and assembled as "((prob1)/(prob2)'random operator'(prob3)/(prob4))" for the infix to postfix solver. I would generate and assemble prob1, 2, 3, and 4 seperately, then place them into the fractional format, is there an easier way to do this other than using stringbuilder and appending parentheses as needed? And how can I display fractions to the user like:
prob1 + prob3 (1 and 3 would be underlined, cannot get fractions to display here either :P)
prob2 prob4

For parenthetical math I have a string fully assembled and need to insert a pair (or two, if the problem has over 9 operands) of parentheses in random locations along the problem string, this would also be used to place parentheses inside fractional math problems if the user is working with parenthetical and fractional combined. The issue is being able to read a string know exactly where in the string the operands are located, and insert a '(' just before an operand (while still being a randomized location just not the last operand) and a ')' just after a second operand (still randomized location just has to be an operand after the first operand).
So there wouldn't be a broken problem like 4 ( + 5 - ) 1 / 2
but would make sense to a math student as 4 + (5 - 1) / 2

Here are my current methods for variables and exponents

        //variable replacing operand
        public static string variabReplace(string probValue, string[] operandValStr)
        {
            int symbolValue = rand.Next(1, 4);
            //e for error
            string variabValue = "E";
            switch (symbolValue)
            {
                case 1: variabValue = "x";
                    break;
                case 2: variabValue = "y";
                    break;
                case 3: variabValue = "z";
                    break;

            }
            //replaced a random operand with variable
            string variabReplaceKey = operandValStr[rand.Next(1, operandValStr.Length)];
            StringBuilder variabBuild = new StringBuilder(probValue);
            variabBuild.Replace(variabReplaceKey, variabValue);
            return variabBuild.ToString();
        }

        //exponent attachment to operand or paren
        public static string exponInsert(string probValue, string[] operandValStr)
        {
            //would replace multiples if random makes same number twice
            //value for exponent
            int exponValue = rand.Next(1, 5);
            //replace a random operand 
            int exponReplaceValue = rand.Next(1, operandValStr.Length);
            //create a new string including value and exponent
            string exponInsertStr = operandValStr[exponReplaceValue] + "^" + exponValue.ToString();
            string exponReplaceStr = operandValStr[exponReplaceValue];

            StringBuilder exponInsertBuild = new StringBuilder(probValue);
            exponInsertBuild.Replace(exponReplaceStr, exponInsertStr);
            return exponInsertBuild.ToString();
        }

I've been reading up on how to use tokenizers(even though c# doesn't have tokenizer), but am looking for advice from others. Any input on how to complete these tasks correctly is appreciated, especially the insertion of parentheses.

I got the insertion of parentheses working by calling a different method to assemble the math problem string

        //assemble math problems for parenthetical math
        public static string parenAssembly(string[] operandValStr, char[] operatorVal)
        {
            string problemStr;
            //define parentheses position
            int[] parenPos = new int[2];
            parenPos[0] = rand.Next(0, operandValStr.Length - 1);
            parenPos[1] = rand.Next(parenPos[0] + 1, operandValStr.Length);

            StringBuilder mathBuild = new StringBuilder();

            for (int ctr = 0; ctr < operandValStr.Length; ctr++)
            {
                if (parenPos[0] == ctr)
                {
                    mathBuild.Append("(").Append(operandValStr[ctr]);
                }
                else if (parenPos[1] == ctr)
                {
                    mathBuild.Append(operandValStr[ctr]).Append(")");
                }
                else
                {
                    mathBuild.Append(operandValStr[ctr]);
                }
                mathBuild.Append(operatorVal[ctr]);
            }

            return problemStr = mathBuild.ToString();
        }

Still working on exponent insertion. The tree design would be used in the infix to postfix solver, which I would parse after the unique math problem is created.

This article has been dead for over six months. Start a new discussion instead.