コード例 #1
0
        //------------------------------------------------------------------------
        public static string convertStrInExpression(string str, MyInterpreator myInterpretor)
        {
            string newStr = str;
            string myLeftArg = "";
            bool isStterdFirst = false;
            bool isChanged = false;
            for (int i = 0; i < str.Length; i++)
            {
                if (isOperation(str[i]) == false)
                {
                    myLeftArg += str[i];
                    isStterdFirst = true;
                }
                else if (isOperation(str[i]) == true)
                {

                    replaceVariableNameWithValue(myInterpretor, myLeftArg, ref newStr);
                    isChanged = true;
                }
                if (i != str.Length - 1 && isChanged && isStterdFirst)
                {
                    myLeftArg = "";
                    isChanged = false;
                    isStterdFirst = false;
                }

            }
            replaceVariableNameWithValue(myInterpretor, myLeftArg, ref newStr);
            newStr = newStr.Replace("\"", String.Empty);
              /*  string tmpStr="";
            for (int i = 0; i < newStr.Length; i++)
            {
                if (newStr.ElementAt(i).Equals("\"")==true)
                {
                    if (i!=0&&newStr.ElementAt(i-1).Equals("\\") == false)
                    {

                    }
                }
                tmpStr += (newStr.ElementAt(i));
            }*/
            return newStr;
        }
コード例 #2
0
        //------------------------------------------------------------------------
        public static void doOperation(string str, MyInterpreator myInterpretor)
        {
            string myStr = str;
            int indexOfEndStr = 0;//need to find exactly row
            string tmpStr = "";//our row
            int lengthOfFirstVaribale = 0;//
            myStr = StringConvector.deleteAllSpacesAndNewLinews(myStr);
            string nameOfFirstVaribale = "";//name of variable which we want to set
            string otherVariables = " ";

            while (myStr.Contains(";") == true)//need fix bug with string concationation;
            {
                indexOfEndStr = myStr.IndexOf(";");
                tmpStr = myStr.Substring(0, indexOfEndStr);

                lengthOfFirstVaribale = tmpStr.IndexOf("=");
                if (isOnlyInit(tmpStr) == true/* && !ifMode && !forMode*/)//just set value and do different math operation
                {   //----
                   /* indexOfEndStr = myStr.IndexOf(";");//its good for init but not for condition
                    tmpStr = myStr.Substring(0, indexOfEndStr);*/

                    //----
                    nameOfFirstVaribale = tmpStr.Substring(0, lengthOfFirstVaribale);//get name of variable
                    otherVariables = tmpStr.Substring(lengthOfFirstVaribale + 1, tmpStr.Length - 1 - lengthOfFirstVaribale);//get other string with variables
                    /*here add calculating sin cos log and differencial*/
                    if (myInterpretor.DoubleCollection.ContainsKey(nameOfFirstVaribale) == true)//is it variable in double collection
                    {
                        string expression = convertStrInExpression(otherVariables, myInterpretor);
                        if (MathOperation.IsMathOperationInStr(expression) == true)
                        {
                            expression = MathOperation.FindSinCosLog(expression,"double");
                        }
                        myInterpretor.DoubleCollection[nameOfFirstVaribale] = MathEvaluator.expresDouble(expression);
                    }
                    else if (myInterpretor.IntCollection.ContainsKey(nameOfFirstVaribale) == true)//is it variable in int collection
                    {   string expression= convertStrInExpression(otherVariables, myInterpretor);
                        if (MathOperation.IsMathOperationInStr(expression) == true)
                        {
                           expression= MathOperation.FindSinCosLog(expression,"int");

                        }
                        myInterpretor.IntCollection[nameOfFirstVaribale] = (int)MathEvaluator.expresInt(expression);
                    }
                    else if (myInterpretor.StrCollection.ContainsKey(nameOfFirstVaribale) == true)//is it variable in str collection
                    {
                        string tmp = convertStrInExpression(otherVariables, myInterpretor);
                        myInterpretor.StrCollection[nameOfFirstVaribale] = tmp;

                    }
                    //need add operation with bool
                    else if (myInterpretor.BoolCollection.ContainsKey(nameOfFirstVaribale) == true)
                    {
                        myInterpretor.BoolCollection[nameOfFirstVaribale] = bool.Parse(convertStrInExpression(otherVariables, myInterpretor));
                    }
                    myStr = myStr.Substring(indexOfEndStr + 1);
                    //new sub str
                }
                //-----------------------------if action-------------------------------------------
                else if (isOnlyIf(tmpStr) == true) //need find end of str bc we will make substring from other start index
                {
                    string condition = ProgramStepByStep.findStringWithCondition(myStr  );
                   // string elseBody = "";
                    int indexOfStartBodyOfCondtion = ProgramStepByStep.endOfCondition(myStr, condition.Length);//additional variable for call fucntion
                    string bodyInCondition = ProgramStepByStep.getBodyAfterCondition(indexOfStartBodyOfCondtion, myStr);//body in codition
                    bool isConditionPerform=ProgramStepByStep.isConditionCorrect(condition,myInterpretor);
                    if (isConditionPerform == true)
                    {
                        myStr = isConditionGood(myStr, condition, bodyInCondition);
                    }
                    else
                    {
                        myStr=isConditionBad(myStr, condition, bodyInCondition);
                    }
                }
                //-----------------------------while action-------------------------------------------
                else if (isOnlyWhile(tmpStr) == true)
                {
                    string condition = ProgramStepByStep.findStringWithWhileCondition(myStr);
                    int indexOfStartBodyOfCondtion = ProgramStepByStep.endOfWhileCondition(myStr, condition.Length);//additional variable for call fucntion
                    string bodyInCondition = ProgramStepByStep.getBodyAfterCondition(indexOfStartBodyOfCondtion, myStr);//body in codition
                    ProgramStepByStep.performOperationInWhileCycle(condition, bodyInCondition,myInterpretor,ref myStr);
                }
            }
        }
コード例 #3
0
 static void performOperationInWhileCycle(string whileCondition,string whileBody,MyInterpreator myInterpretor,ref string myStr)
 {
     while (ProgramStepByStep.isConditionCorrect(whileCondition, myInterpretor) == true)
     {
         ProgramStepByStep.doOperation(whileBody,myInterpretor);
     }
     myStr = isConditionBadForWhile(myStr, whileCondition, whileBody);
 }
コード例 #4
0
 //---replacing name of variable with value;
 //------------------------------------------------------------------------
 public static void replaceVariableNameWithValue(MyInterpreator myInterpretor, string myLeftArg,ref string newStr/*,
     int indexOfFirstChar, int indexOfLastChar*/)
 {
     //need check is this variable alone and its isnt a part of othe variable ,for example name surname
     if (myInterpretor.DoubleCollection.ContainsKey(myLeftArg) == true)//is it variable in double collection
     {
         newStr = newStr.Replace(myLeftArg, myInterpretor.DoubleCollection[myLeftArg].ToString());
     }
     else if (myInterpretor.IntCollection.ContainsKey(myLeftArg) == true)//is it variable in int collection
     {
         newStr = newStr.Replace(myLeftArg, myInterpretor.IntCollection[myLeftArg].ToString());
     }
     else if (myInterpretor.StrCollection.ContainsKey(myLeftArg) == true)//is it variable in str collection
     {
         var regex = new Regex(Regex.Escape(myLeftArg));
         if (newStr.IndexOf(myLeftArg) != 0)
         {
             regex = new Regex(Regex.Escape("+"+myLeftArg));
         }
         var newText = regex.Replace(newStr, myInterpretor.StrCollection[myLeftArg].ToString(), 1);
         //newText = newText.Replace("+", "");
         // newStr = newStr.Replace(myLeftArg, myInterpretor.StrCollection[myLeftArg].ToString());
         newStr = newText;
     }
     else if (myInterpretor.BoolCollection.ContainsKey(myLeftArg) == true)//is it variable in str collection
     {
         newStr = newStr.Replace(myLeftArg, myInterpretor.BoolCollection[myLeftArg].ToString());
     }
 }
コード例 #5
0
        //check is condition true or false
        //------------------------------------------------------------------------
        public static bool isConditionCorrect(string str, MyInterpreator myInterpretor)
        {
            string leftArgument = "";
            string rightArgument = "";
            int indexMiddle = str.IndexOfAny("><=!".ToCharArray());
            int indexOfStartSecondArgument;
            string operation="";
            double a=0,b=0;

            if (str[indexMiddle + 1] == "=".ToCharArray()[0])
            {
                indexOfStartSecondArgument = indexMiddle + 1;
            }
            else
            {
                indexOfStartSecondArgument = indexMiddle;
            }
            operation = str.Substring(indexMiddle, indexOfStartSecondArgument - indexMiddle + 1);
            leftArgument = str.Substring(0, indexMiddle);
            rightArgument = str.Substring(indexOfStartSecondArgument+1, str.Length -1 - indexOfStartSecondArgument);
            leftArgument=convertStrInExpression(leftArgument, myInterpretor);
            rightArgument=convertStrInExpression(rightArgument, myInterpretor);
            object leftObj = (MathEvaluator.expres(leftArgument));
            object rightObj = (MathEvaluator.expres(rightArgument));
            if (rightObj is decimal)
            {
                b = MathEvaluator.expresDouble(rightArgument);
            }
            else if (rightObj is int)
            {
                b = (int)MathEvaluator.expresInt(rightArgument);
            }
            if (leftObj is decimal)
            {
                a = MathEvaluator.expresDouble(leftArgument);
            }
            else if (leftObj is int)
            {
                a = (int)MathEvaluator.expresInt(leftArgument);
            }
            if (String.Equals(operation, "=="))
            {
                if (leftObj.Equals(rightObj))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
               else if (String.Equals(operation, "!="))
               {
                if (leftObj.Equals(rightObj))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
               else if (String.Equals(operation, ">"))
               {
               if (a > b)
               {
                   return true;
               }
               else
               {
                   return false;
               }
               }
               else if (String.Equals(operation, ">="))
               {
               if (a >= b)
               {
                   return true;
               }
               else
               {
                   return false;
               }
               }
               else if (String.Equals(operation, "<"))
               {
               if (a < b)
               {
                   return true;
               }
               else
               {
                   return false;
               }
               }
               else if (String.Equals(operation, "<="))
               {
               if (a <= b)
               {
                   return true;
               }
               else
               {
                   return false;
               }
               }
            return false;
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: sssbohdan/Interpretator
 private void Form1_Load(object sender, EventArgs e)
 {
     interpretor = new MyInterpreator();
 }