Статус:
Offline
Реєстрація: 16.04.2009
Повідом.: 76
Реєстрація: 16.04.2009
Повідом.: 76
C# Алгоритм Карпа-Рабина
В общем не могу понять почему не подсчитывает кол-во вхождений подстроки в строку.
В общем не могу понять почему не подсчитывает кол-во вхождений подстроки в строку.
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace lab_44
{
public partial class Form1 : Form
{
public int Text_func (string required_text, string all_text)
{
char[] temp0 = required_text.ToCharArray();
string temp1 = "";
char[] temp_all_text;
int counter = 0;
temp_all_text = all_text.ToCharArray();
for (int i = 0; i < temp_all_text.Length; i++)
{
for (int j = 0; j < required_text.Length; j++)
{
temp1 += temp_all_text[j];
}
if (temp1 == required_text)
{
counter++;
temp1 = "";
//изменяет найденный текст на БОЛЬШОЙ
//all_text.Substring(i, required_text.Length) = all_text.Substring(i, required_text.Length).ToUpper();
}
}
return counter;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
StreamReader sr = new StreamReader(openFileDialog1.FileName);
textBox1.Text = sr.ReadToEnd();
textBox3.Text = Text_func(textBox2.Text, sr.ReadToEnd()).ToString();
//textBox1.Text = myStream.ToString();
sr.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}