NEWS

[C#] Hướng dẫn đảo nội dung bài viết trong lập trình winform

[C#] Hướng dẫn đảo nội dung bài viết trong lập trình winform
Đăng bởi: Thảo Meo - Lượt xem: 2732 13:22:52, 09/12/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn các đảo nội dung bài viết trong lập trình C#, winform.

[C#] How to Rotate Text in csharp Winform

VD: mình có đoạn văn như sau

  • Tin 1 : “mua ban sim so dep,cho thue sim so dep,lien he 0989 999 999“
  • Tin 2: “cho thue sim so dep, mua ban sim so dep,lien he 0989 999 999“
  • Tin 3: “lien he 0989 999 999,mua ban sim so dep, cho thue sim so dep“
  • Tin 4 : “cho thue sim so dep,lien he 0989 999 999, mua ban sim so dep“

Ở đoạn trên, các bạn thấy các dữ liệu trong đoạn được đảo nhau bằng cách dấu phẩy (,)

Thuật toán ở đây chúng ta muốn đảo đơn giản là:

VD: abc => kết quả đảo

acb, bca, cab, abc, cba, bac.

Từ abc có 3 ký tự: số kết quả đảo được chính là 3! = 6 (3 giai thừa)

Giao diện demo ứng dụng đảo đoạn văn bản C#, winform:

rotate_text_csharp

Các bạn có thể ứng dụng này để làm content spin.

Class Algorithms.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpinEditorApp.Helpers
{
    class Algorithms
    {
        private int elementLevel = -1;
        private int numberOfElements;
        private int[] permutationValue = new int[0];

        private char[] inputSet;
        public char[] InputSet
        {
            get { return inputSet; }
            set { inputSet = value; }
        }

        private int permutationCount = 0;
        public int PermutationCount
        {
            get { return permutationCount; }
            set { permutationCount = value; }
        }

        public char[] MakeCharArray(string InputString)
        {
            char[] charString = InputString.ToCharArray();
            Array.Resize(ref permutationValue, charString.Length);
            numberOfElements = charString.Length;
            return charString;
        }
      
        public void Recursion(int k)
        {
            elementLevel++;
            permutationValue.SetValue(elementLevel, k);

            if (elementLevel == numberOfElements)
            {
                OutputPermutation(permutationValue);
            }
            else
            {
                for (int i = 0; i < numberOfElements; i++)
                {
                    if (permutationValue[i] == 0)
                    {
                        Recursion(i);
                    }
                }
            }
            elementLevel--;
            permutationValue.SetValue(0, k);
        }

    
        public List<string> list = new List<string>();
        private void OutputPermutation(int[] value)
        {

            var list_temp = new List<string>();
            foreach (int i in value)
            {
                var _char = inputSet.GetValue(i - 1).ToString();

                list_temp.Add(_char);

                Console.Write(inputSet.GetValue(i - 1));
            }
            list.Add(string.Join(",", list_temp));
            Console.WriteLine();
            PermutationCount++;

        }
    }
}

Full source code Rotate Text c#:

using SpinEditorApp.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RotateSentence
{
    public partial class Form1 : Form
    {
        public readonly char SYMBOL = ',';
        public Form1()
        {
            InitializeComponent();
        }

        private async void btn_rotate_Click(object sender, EventArgs e)
        {
            var content = txt_input.Text.Trim();
            var dataRotate = await RotateTextAsync(content);
            lbl_result.Text = $"Kết quả đảo nội dung: {dataRotate.Count} kết quả đảo được.";
            dataGridView1.DataSource = dataRotate;
        }

        public Task<List<Article>> RotateTextAsync(string content)
        {
            var task = Task.Run(() =>
            {
                var articles = new List<Article>();
                
                    var line = content.Split(SYMBOL);
                    var char_temp = "";
                    for (int i = 0; i < line.Length; i++)
                    {
                        char_temp += i;
                    }

                    Algorithms permAlgorithm = new Algorithms();
                    permAlgorithm.InputSet = permAlgorithm.MakeCharArray(char_temp);
                    permAlgorithm.Recursion(0);
                    var data = permAlgorithm.list;
                   
                    foreach (var index in data)
                    {                        
                        List<string> list_data_temp = new List<string>();
                        var arr = index.Split(',');
                        foreach (var pos in arr)
                        {
                            var pos_int = Convert.ToInt32(pos);
                            list_data_temp.Add(line[pos_int]);
                        }

                        var rotateText = string.Join(",", list_data_temp);

                        var article = new Article();
                        article.id = articles.Count() + 1;
                        article.content = rotateText;
                        articles.Add(article);

                }
                return articles;
            });
            return task;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://laptrinhvb.net");
        }
    }

    public class Article
    {
        public int id { get; set; }
        public string content { get; set; }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn đảo nội dung bài viết trong lập trình winform
Đăng bởi: Thảo Meo - Lượt xem: 2732 13:22:52, 09/12/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.