Пример #1
0
        static public List <MathCharacter> decipherMathFromArea(Bitmap area, bool answerMode = false)
        {
            List <MathCharacter> mathList = new List <MathCharacter>();
            Rectangle            SCANNER_BAR;
            Point upperLeft = new Point(0, 0);
            Size  characterSize;

            if (answerMode)
            {
                SCANNER_BAR   = new Rectangle(new Point(0, 0), new Size(1, ANSWER_CHARACTER_HEIGHT));
                characterSize = new Size(34, ANSWER_CHARACTER_HEIGHT);
            }
            else
            {
                SCANNER_BAR   = new Rectangle(new Point(0, 0), new Size(1, DISPLAY_CHARACTER_HEIGHT)); //0, 410, stretches down 150.
                characterSize = new Size(54, DISPLAY_CHARACTER_HEIGHT);
            }

            Bitmap section;
            int    scannerX = 0;
            bool   whitePixelFound = false, foundOnLastLoop = false;

            while (scannerX < 1080)
            {
                section = area.Clone(SCANNER_BAR, area.PixelFormat);

                //SCREENSHOTTING AT EVERY 100

                /*if (scannerX % 50 == 0 && scannerX > 400)
                 * {
                 *  Console.WriteLine("Do you want to take a screen shot at {0}, {1}? (y / *)", scannerX, 0);
                 *  var pressedKey = Console.ReadKey().KeyChar;
                 *  Console.Read();
                 *  if (pressedKey == 'y')
                 *  {
                 *      string saveStr = string.Format(@"C:\Users\evanc\Desktop\section-{0}.bmp", scannerX);
                 *      section.Save(saveStr);
                 *  }
                 *
                 *  Console.WriteLine("Press any key to continue through the image scanning process.");
                 *  pressedKey = Console.ReadKey().KeyChar;
                 *  if (pressedKey == 'x')
                 *      break;
                 * }
                 */

                foundOnLastLoop = whitePixelFound;
                whitePixelFound = checkIfWhiteInArea(section, invert: answerMode, DEBUG: false);

                if (whitePixelFound && !foundOnLastLoop) //This is the beginning of white pixels!
                {
                    upperLeft.X = scannerX;
                }
                else if (!whitePixelFound && foundOnLastLoop) //This is the edge of the white pixels!
                {
                    //Rectangle completed. Get bitmap from rectangle and scan it for numbers and operators, add the result to the list.

                    Rectangle resultRectangle  = new Rectangle(upperLeft, characterSize);
                    var       characterSection = area.Clone(resultRectangle, area.PixelFormat);

                    MathCharacter foundCharacter = findCharacterInArea(characterSection, answerMode: answerMode);

                    if (foundCharacter.number == "=")
                    {
                        break;
                    }
                    else if (foundCharacter.number != "?")
                    {
                        mathList.Add(foundCharacter);
                    }

                    //CHARACTER CUTTING

                    count++;
                    string saveChar = foundCharacter.number;
                    if (saveChar.Equals("/"))
                    {
                        saveChar = "X";
                    }
                    string savePath = String.Format(CHARACTER_DIRECTORY + "CHARACTER_" + count + ".bmp");
                    characterSection.Save(savePath);

                    foundOnLastLoop = false;
                }

                scannerX += 1; SCANNER_BAR.X = scannerX;
            }

            mathList.Add(new MathCharacter("="));
            return(mathList);
        }
Пример #2
0
        public static int calculateMathFromList(List <MathCharacter> mathList)
        {
            Console.WriteLine("Calculating math. List says: ");
            foreach (MathCharacter character in mathList)
            {
                Console.Write("{0} ", character.number);
            }
            Console.WriteLine();

            List <int>           mathNumbers   = new List <int>();
            List <MathCharacter> mathOperators = new List <MathCharacter>();

            string result = "";

            List <MathCharacter> .Enumerator numerator = mathList.GetEnumerator();
            MathCharacter current;
            MathCharacter previous = new MathCharacter("0");

            //Special case for first character being a negative sign.
            numerator.MoveNext();
            if (numerator.Current.isNegate)
            {
                result += "-";
                numerator.MoveNext();
            }

            //583 / 11 = comes in.

            while (numerator.Current != null) //loop that fills array numbers with 1 FULL number at a time including negative signs. //operators contains operators in order.
            {
                current = numerator.Current;

                if (current.isNumber())
                {
                    result += current.number;
                }

                else
                {
                    if (previous.isOperator() && current.isNegate)  //Like in 743[+-]12
                    {
                        result += "-";                              //-..12 = -12
                    }
                    else
                    {
                        int parsedNumber;
                        try
                        {
                            parsedNumber = int.Parse(result);
                            result       = "";
                            mathNumbers.Add(parsedNumber);
                            mathOperators.Add(current);
                        }
                        catch (FormatException ex)
                        {
                            Console.WriteLine("The processing failed for data {0}", result);
                            result = "";
                        }
                    }
                }

                previous = current;
                numerator.MoveNext();
            }

            //End array creation loop

            //THIS PROGRAM IS SO FAR INCAPABLE OF PERFORMING MULTIPLE OPERATIONS PER LIST

            int number1 = -99999, number2 = -99999;

            if (mathNumbers.Count() == 0)
            {
                Console.WriteLine("There is no math that can be completed for list in calculateMathFromList()");
                return(number1);
            }
            else
            {
                number1 = mathNumbers.ElementAt(0);
            }

            if (mathNumbers.Count() > 1)
            {
                number2 = mathNumbers.ElementAt(1);
            }

            if (mathOperators.ElementAt(0).isEqualSign)
            {
                return(number1);
            }

            if (mathOperators.ElementAt(0).isDivisor)
            {
                return(number1 / number2);
            }

            else if (mathOperators.ElementAt(0).isAdder)
            {
                return(number1 + number2);
            }

            else if (mathOperators.ElementAt(0).isNegate)
            {
                return(number1 - number2);
            }

            return(-99999);
        }