Процентное соотношение совпадений двух текстов - C#
Формулировка задачи:
Есть два текста без пробелов. Как их сравнить и написать процентное соотношение совпадений? Помогите, пожалуйста. В С++ написал бы, а в c# не разбираюсь((. Не могу написать код для пятой кнопки.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Create an OpenFileDialog object.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the filter to look for text files.
openFile1.Filter = "Text Files|*.txt";
// If the user selected a file, load its contents into the RichTextBox.
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);//открыть текстовый файл.
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
// Create an OpenFileDialog object.
OpenFileDialog openFile2 = new OpenFileDialog();
// Initialize the filter to look for text files.
openFile2.Filter = "Text Files|*.txt";
// If the user selected a file, load its contents into the RichTextBox.
if (openFile2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox2.LoadFile(openFile2.FileName, RichTextBoxStreamType.PlainText);
}
private void button3_Click(object sender, EventArgs e)
{
string Polinom = this.richTextBox1.Text;
string N = this.richTextBox1.Text;
string text = richTextBox1.Text;// Вписать текст
text = text.Replace(" ", string.Empty);
N = N.Replace(" ", string.Empty);
richTextBox1.Text = N;
}
private void button4_Click(object sender, EventArgs e)
{
string Polinom = this.richTextBox2.Text;
string M = this.richTextBox2.Text;
string text = richTextBox2.Text;// Вписать текст
text = text.Replace(" ", string.Empty);
M = M.Replace(" ", string.Empty);
richTextBox2.Text = M;
}
private void button5_Click(object sender, EventArgs e)
{
}Решение задачи: «Процентное соотношение совпадений двух текстов»
textual
Листинг программы
private void button5_Click(object sender, EventArgs e) {
var text1 = richTextBox1.Text;
var text2 = richTextBox2.Text;
var diffCount = 0;
var minLength = Math.Min(text1.Length, text2.Length);
for (var i = 0; i < minLength; i++) {
if (text1[i] == text2[i]) continue;
diffCount++;
}
var sovpadPercent = (minLength - diffCount / Math.Max(text1.Length, text2.Length)) * 100;
MessageBox.Show("Процент совпадений: " + sovpadPercent);
}