c#文本差异

本文关键字:文本 | 更新日期: 2023-09-27 17:59:43

我有两个词:
来源:John
ConvertTo:Jack

我想展示将所有字母同时从"Source"转换为"ConvertTo"单词的效果。我已经创建了一个程序来实现这一点,但一次处理一个字母,以显示我使用Threads的效果,问题是要同时处理所有字母,我想每个字母需要一个线程,每个线程都会调用处理字母的同一函数,我使用全局变量。

这是代码(仅适用于相同长度的文本):

private void button1_Click(object sender, EventArgs e)
    {            
        lblResult.Text = "";
        lblResult2.Text = "";            
        ThreadPool.QueueUserWorkItem(new WaitCallback(Process));            
    }
    int movement = 0;
    string CumulateText;
    private void Process(object stateinfo)
    {
        int value;
        int operation; //0->[+] 1->[-]
        CumulateText = "";
        for (int i = 0; i <= textBox1.Text.Length - 1; i++)
        {
            if (textBox1.Text[i] != ' ')                
            {
                value = (char)textBox1.Text[i] - (char)textBox2.Text[i];
                if (value >= 0)
                    operation = 1;
                else
                    operation = 0;
                for (int ii = 0; ii <= Math.Abs(value); ii++)
                {
                    if (operation == 1)
                        movement = (char)textBox1.Text[i] - ii;
                    else
                        movement = (char)textBox1.Text[i] + ii;
                    this.Invoke(new EventHandler(ShowMovement));
                    System.Threading.Thread.Sleep(10);
                }
            }
            CumulateText += textBox2.Text[i].ToString();                
        }
    }
    private void ShowMovement(object sender, EventArgs e)
    {            
        lblResult.Text = CumulateText + Convert.ToString((char)movement);            
    }

我希望我能让别人理解我自己。请给我一些建议。感谢

为了更清楚地说明我想要实现的目标,这里有一个例子:
来源:John
ConvertTo:Jack

J-相同J
o-减少到a(o,n,m,…,a)
h-减少到c(h,g,f,…,c)
n-减少到k(n,m,l,k)

c#文本差异

我曾经为了好玩而为一个小项目做过类似的事情。

我不明白为什么你需要为每个字母创建一个线程来创建两个单词之间的转换,除非我不理解你假装做得正确。

检查并研究以下代码,看看它是否有任何帮助:

static class Program
{
    static void Main()
    {
        TextTranstition transition = new TextTranstition();
        transition.TransitionFinished += TransitionTicked;
        transition.TransitionTicked += TransitionTicked;
        transition.StartTransition("AmazingWordTransition", "MyNewWordAppearing", 100);
        Thread.CurrentThread.Join();
        Console.ReadKey();
    }
    public static void TransitionTicked(object sender, TranstitionEventArgs e)
    {
        Console.Clear();
        Console.Write(e.TransitionText);
    }
}
public class TranstitionEventArgs : EventArgs
{
    private readonly string transitionText;
    public string TransitionText { get { return this.transitionText; } }
    public TranstitionEventArgs(string transitionText)
    {
        this.transitionText = transitionText;
    }
}

public class TextTranstition
{
    private struct StartInfo
    {
        public StartInfo(string initialText, string finalText, int timeStep)
        {
            this.initialText = initialText;
            this.finalText = finalText;
            this.timeStep = timeStep;
        }
        private readonly string initialText;
        public string InitialText { get { return this.initialText; } }
        private readonly string finalText;
        public string FinalText { get { return this.finalText; } }
        private readonly int timeStep;
        public int TimeStep { get { return this.timeStep; } }
    }
    public EventHandler<TranstitionEventArgs> TransitionFinished;
    public EventHandler<TranstitionEventArgs> TransitionTicked;
    public void StartTransition(string initialText, string finalText, int timeStep)
    {
        StartInfo startInfo = new StartInfo(initialText, finalText, timeStep);
        Thread t = new Thread(startTransition);
        t.Start(startInfo);
    }
    private void startTransition(object info)
    {
        StartInfo startInfo = (StartInfo)info;
        string initialText = startInfo.InitialText;
        string finalText = startInfo.FinalText;
        if (initialText.Length < finalText.Length)
        {
            initialText = initialText.PadRight(finalText.Length);
        }
        if (TransitionTicked != null) TransitionTicked(this, new TranstitionEventArgs(initialText));
        while ((initialText = transition(initialText, finalText)) != finalText)
        {
            Thread.Sleep(startInfo.TimeStep);
            if (TransitionTicked != null) TransitionTicked(this, new TranstitionEventArgs(initialText));
        }
        if (TransitionFinished != null) TransitionFinished(this, new TranstitionEventArgs(finalText));
    }
    private string transition(string initialText, string finalText)
    {
        StringBuilder b = new StringBuilder(finalText.Length);
        for (int i = 0; i < finalText.Length; i++)
        {
            char c = initialText[i];
            int charCode = (int)c;
            if (c != finalText[i])
            {
                if (charCode == 122 || charCode==32) charCode = 65;
                else if (charCode == 90) charCode = 97;
                else
                {
                    charCode += 1;
                }
            }
            b.Append((char)charCode);
        }
        return b.ToString();
    }
}

对这类东西使用BackGroundWorker。