コード例 #1
0
 /// <summary>
 /// Markov algorithm performs
 /// </summary>
 /// <returns></returns>
 public MyString DoingAlgorithm()
 {
     int i = 0;
     int countPerformedSubstitutions = 0;
     MyString resultLine = new MyString(line);
     while(i < substitutions.Count)
     {
         if(resultLine.Find(substitutions[i].Key) != -1)
         {
             resultLine = resultLine.Replace(substitutions[i].Key, substitutions[i].Value);
             i = 0;
             if (countPerformedSubstitutions < N)
             {
                 countPerformedSubstitutions++;
             }
             else
             {
                 throw new InvalidOperationException("Substitutions performed too long.");
             }
         }
         else
         {
             i++;
         }
     }
     return resultLine;
 }
コード例 #2
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestCopyConstructor()
 {
     MyString a = new MyString();
     a.GetLine("inA.txt");
     MyString b = new MyString(a);
     Assert.IsTrue(a == b);
 }
コード例 #3
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodIndex2()
 {
     MyString a = new MyString();
     a.GetLine("inA.txt");
     a[0] = '0';
     Assert.AreEqual(a.ToString(), "0werty123");
 }
コード例 #4
0
 /// <summary>
 /// copy constructor
 /// </summary>
 /// <param name="someMarkovAlgorithmForString">variable of type MarkovAlgorithmForMyString</param>
 public MarkovAlgorithmForMyString(MarkovAlgorithmForMyString someMarkovAlgorithmForMyString)
 {
     for (int i = 0; i < someMarkovAlgorithmForMyString.substitutions.Count; i++)
     {
         this.substitutions.Add(someMarkovAlgorithmForMyString.substitutions[i]);
     }
     this.line = new MyString(someMarkovAlgorithmForMyString.line);
 }
コード例 #5
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodFind2()
 {
     MyString a = new MyString();
     MyString b = new MyString();
     a.GetLine("inA.txt");
     b.GetLine("inB.txt");
     Assert.AreEqual(a.Find(b), -1);
 }
コード例 #6
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodEquel2()
 {
     MyString a = new MyString();
     MyString b = new MyString();
     a.GetLine("inA.txt");
     b.GetLine("inB.txt");
     Assert.IsFalse(a == b);
 }
コード例 #7
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodEquel1()
 {
     MyString a = new MyString();
     MyString b = new MyString();
     a.GetLine("inA.txt");
     b.GetLine("inC.txt");
     Assert.IsTrue(a == b);
 }
コード例 #8
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodConcatenation()
 {
     MyString a = new MyString();
     MyString b = new MyString();
     a.GetLine("inA.txt");
     b.GetLine("inB.txt");
     Assert.AreEqual((a+b).ToString(), "qwerty1234567890qwerty");
 }
コード例 #9
0
 /// <summary>
 /// constructor with parameters
 /// </summary>
 /// <param name="someSubstitutions">list substitutions</param>
 /// <param name="someLine">line</param>
 public MarkovAlgorithmForMyString(List<KeyValuePair<MyString, MyString>> someSubstitutions, MyString someLine)
 {
     for (int i = 0; i < someSubstitutions.Count; i++)
     {
         this.substitutions.Add(someSubstitutions[i]);
     }
     this.line = new MyString(someLine);
 }
コード例 #10
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodDoingAlgorithmForMyString50()
 {
     MarkovAlgorithmForMyString m = new MarkovAlgorithmForMyString();
     m.ReadData("in50.txt");
     MyString ms = new MyString(m.DoingAlgorithm());
     string a = string.Empty;
     for (int i = 0; i < 2500; i++) a += "1";
     MyString msForCheck = new MyString(a);
     Assert.IsTrue(ms == msForCheck);
 }
コード例 #11
0
ファイル: MyString.cs プロジェクト: molochiy/Education
 /// <summary>
 /// copy constructor
 /// </summary>
 /// <param name="newMyString">variable of type MyString</param>
 public MyString(MyString newMyString)
 {
     if (this.mystring != null)
     {
         Array.Clear(this.mystring, 0, this.mystring.Length);
     }
     this.mystring = new char[newMyString.mystring.Length];
     for (int i = 0; i < newMyString.mystring.Length; i++)
     {
         this.mystring[i] = newMyString.mystring[i];
     }
 }
コード例 #12
0
ファイル: MyString.cs プロジェクト: molochiy/Education
 /// <summary>
 /// replace substring1 into substring2
 /// </summary>
 /// <param name="subString1">substring that will be replaced</param>
 /// <param name="subString2">substring that will be inserted</param>
 /// <returns>string after replacing</returns>
 public MyString Replace(MyString subString1, MyString subString2)
 {
     char[] resultAfterReplace = new char[this.mystring.Length - subString1.mystring.Length + subString2.mystring.Length];
     int findPosition = this.Find(subString1);
     if ( findPosition == -1)
     {
         throw new ArgumentException("Specified substring not exist");
     }
     else
     {
         int index = 0;
         for (int i = 0; i < findPosition; i++)
         {
             resultAfterReplace[index] = this.mystring[i];
             index++;
         }
         for (int i = 0; i < subString2.mystring.Length; i++)
         {
             resultAfterReplace[index] = subString2.mystring[i];
             index++;
         }
         for (int i = findPosition + subString1.mystring.Length; i < this.mystring.Length; i++)
         {
             resultAfterReplace[index] = this.mystring[i];
             index++;
         }
     }
     return new MyString(resultAfterReplace);
 }
コード例 #13
0
ファイル: MyString.cs プロジェクト: molochiy/Education
        /// <summary>
        /// find substring in string
        /// </summary>
        /// <param name="subString">substring that need find</param>
        /// <returns>-1 if didn't find, position from that begin substring in string otherwise</returns>
        public int Find(MyString subString)
        {
            int position = -1;
            for (int i = 0; i <= this.mystring.Length - subString.mystring.Length; i++)
            {
                if (this.mystring[i] == subString.mystring[0])
                {
                    bool ok = true;
                    for (int j = 0; j < subString.mystring.Length; j++)
                    {
                        if (subString.mystring[j] != this.mystring[i + j])
                        {
                            ok = false;
                        }
                    }
                    if (ok)
                    {
                        position = i;
                        break;
                    }
                }
            }
            /*/// index elements of subString
            int j = 1;

            /// index elements of string
            int i = 0;

            int position = -1;

            bool flag = false;
            while(i <= this.mystring.Length - subString.mystring.Length && !flag)
            {
                if (this.mystring[i] == subString.mystring[0])
                {
                    position = i;
                    flag = true;
                    i++;
                    while (j < subString.mystring.Length && flag)
                    {
                        if (this.mystring[i] == subString.mystring[j])
                        {
                            i++;
                            j++;
                        }
                        else
                        {
                            j = 1;
                            flag = false;
                            position = -1;
                        }
                    }
                }
                else
                {
                    i++;
                }
            }*/
            return position;
        }
コード例 #14
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodIndex1()
 {
     MyString a = new MyString();
     a.GetLine("inA.txt");
     Assert.AreEqual(a[0], 'q');
 }
コード例 #15
0
ファイル: UnitTest1.cs プロジェクト: molochiy/Education
 public void TestMethodReplace3()
 {
     MyString a = new MyString();
     MyString b = new MyString();
     MyString c = new MyString();
     a.GetLine("inA.txt");
     b.GetLine("inD.txt");
     c.GetLine("inG.txt");
     Assert.AreEqual(a.Replace(b, c).ToString(), "qwer-2-103");
 }
コード例 #16
0
 /// <summary>
 /// read data from file
 /// </summary>
 /// <param name="path">path to file</param>
 public void ReadData(string path)
 {
     string readLine = string.Empty;
     System.IO.StreamReader file = new System.IO.StreamReader(@path);
     if ((readLine = file.ReadLine()) == null)
     {
         throw new ArgumentNullException("File empty or not exist");
     }
     this.line = new MyString(readLine);
     while ((readLine = file.ReadLine()) != null)
     {
         this.substitutions.Add(new KeyValuePair<MyString, MyString>(new MyString(readLine.Split(' ')[0]), new MyString(readLine.Split(' ')[1])));
     }
 }
コード例 #17
0
 /// <summary>
 /// default constructor
 /// </summary>
 public MarkovAlgorithmForMyString()
 {
     this.substitutions = new List<KeyValuePair<MyString, MyString>>();
     this.line = new MyString();
 }