.NET 3.x Поиск совпадений в строках циклом - C#

Узнай цену своей работы

Формулировка задачи:

Поиск совпадений в строках циклом
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Title("Test");
  12. string[] find = File.ReadAllLines("111.txt");
  13. string[] scan = File.ReadAllLines("111.txt");
  14. for (int i = 0; i < find.Length; i++)
  15. {
  16. for (int j = 0; j < scan.Length; j++)
  17. {
  18. if (i<j)
  19. {
  20. if(find[i].Trim()==scan[j])
  21. Console.WriteLine(j+1); // строки нумерованые совпадения
  22. }
  23. }
  24. }
  25. Console.ReadKey(true);
  26. }
  27. static void Title(string t)
  28. {
  29. Console.Title = t;
  30. Console.BackgroundColor=ConsoleColor.DarkCyan;
  31. Console.Clear();
  32. Console.ForegroundColor=ConsoleColor.White;
  33. }
  34. }
  35. }
Проверте товарищи правильно , я использую поиск совпадений строк циклом ?
Кажется нашол отсеить похожие
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Linq;
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Title("Test");
  14. int k = 0;
  15. string txt=String.Empty;
  16. string[] find = File.ReadAllLines("111.txt");
  17. List<string> list = new List<string>();
  18. for (int i = 0; i < find.Length; i++)
  19. {
  20. list.Add(find[i]);
  21. }
  22. List<string> distinct = list.Distinct().ToList();
  23. for (int i = 0; i < distinct.Count; i++)
  24. {
  25. Console.WriteLine((i+1)+" :"+distinct[i]);
  26. }
  27.  
  28. Console.ReadKey(true);
  29. }
  30. static void Title(string t)
  31. {
  32. Console.Title = t;
  33. Console.BackgroundColor=ConsoleColor.DarkCyan;
  34. Console.Clear();
  35. Console.ForegroundColor=ConsoleColor.White;
  36. }
  37. }
  38. }
Но все таки надо получить совпадения ))
До пер
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Linq;
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Title("Test");
  14. int k = 0;
  15. string txt=String.Empty;
  16. string[] find = File.ReadAllLines("111.txt");
  17. List<string> list = new List<string>();
  18. List<string> distinct = new List<string>();
  19. for (int i = 0; i < find.Length; i++)
  20. {
  21. list.Add(find[i]);
  22. }
  23. distinct = list.Distinct().ToList();
  24. for (int i = 0; i < distinct.Count; i++)
  25. {
  26. Console.WriteLine((i+1)+" :"+distinct[i]);
  27. }
  28. Console.WriteLine("\n\n");
  29. for (int i = 0; i < find.Length; i++)
  30. {
  31. for (int j = i+1; j < find.Length; j++)
  32. {
  33. if (find[i]==find[j])
  34. {
  35. Console.WriteLine("Дубликат: "+(j+1));
  36. break;
  37. }
  38. }
  39. }
  40. Console.ReadKey(true);
  41. }
  42. static void Title(string t)
  43. {
  44. Console.Title = t;
  45. Console.BackgroundColor=ConsoleColor.DarkCyan;
  46. Console.Clear();
  47. Console.ForegroundColor=ConsoleColor.White;
  48. }
  49. }
  50. }
Хих сам спросил , сам решил Пока
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Linq;
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Title("Test");
  14. int k = 0;
  15. string txt=String.Empty;
  16. string[] find = File.ReadAllLines("111.txt");
  17. List<string> list = new List<string>();
  18. List<string> distinct = new List<string>();
  19. for (int i = 0; i < find.Length; i++)
  20. {
  21. list.Add(find[i]);
  22. }
  23. Console.WriteLine("Список вместе с Дубликатами\n");
  24. for (int i = 0; i < list.Count; i++)
  25. {
  26. Console.WriteLine((i+1)+" :{0}",list[i]);
  27. }
  28. distinct = list.Distinct().ToList();
  29. Console.WriteLine("\n\n");
  30. Console.WriteLine("Список без Дубликатов\n");
  31. for (int i = 0; i < distinct.Count; i++)
  32. {
  33. Console.WriteLine((i+1)+" :"+distinct[i]);
  34. }
  35. Console.WriteLine("\n\n");
  36. Console.WriteLine("Список индексов Дубликатов\n");
  37. for (int i = 0; i < find.Length; i++)
  38. {
  39. for (int j = i+1; j < find.Length; j++)
  40. {
  41. if (find[i]==find[j])
  42. {
  43. Console.WriteLine("Дубликат: "+(j+1));
  44. break;
  45. }
  46. }
  47. }
  48. Console.ReadKey(true);
  49. }
  50. static void Title(string t)
  51. {
  52. Console.Title = t;
  53. Console.BackgroundColor=ConsoleColor.DarkCyan;
  54. Console.Clear();
  55. Console.ForegroundColor=ConsoleColor.White;
  56. }
  57. }
  58. }

Решение задачи: «.NET 3.x Поиск совпадений в строках циклом»

textual
Листинг программы
  1.  private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             int dat1 = 0, dat2 = 0,k=0;
  4.             int[] del = null;
  5.             dat1 = dataGridView1.Rows.Count - 1;
  6.             dat2 = dataGridView2.Rows.Count - 1;
  7.             List<string> list = new List<string>();
  8.             List<string> distinct = new List<string>();
  9.             if (dat1==dat2)
  10.             {
  11.                 using (StreamWriter sw=new StreamWriter("dell.tmp",false,Encoding.GetEncoding(1251)))
  12.                 {
  13.                     for (int i = 0; i < dat2; i++)
  14.                     {
  15.                         for (int j = i + 1; j < dat2; j++)
  16.                         {
  17.                             if (dataGridView2.Rows[i].Cells[0].Value.ToString() == dataGridView2.Rows[j].Cells[0].Value.ToString())
  18.                             {
  19.                                
  20.                                 sw.WriteLine(j);
  21.                                
  22.                             }
  23.                         }
  24.                     }
  25.                    
  26.                 }
  27.                 if (File.Exists("dell.tmp") == true)
  28.                 {
  29.                     string[] dellm = File.ReadAllLines("dell.tmp");
  30.                     for (int i = 0; i < dellm.Length; i++)
  31.                     {
  32.                        
  33.                         //MessageBox.Show(dataGridView2.Rows[Convert.ToInt32(dellm[i])].Cells[0].Value.ToString() + "\n" + dataGridView1.Rows[Convert.ToInt32(dellm[i])].Cells[0].Value.ToString());
  34.                         //MessageBox.Show(dellm[i]);
  35.                         dataGridView1.Rows.RemoveAt(Convert.ToInt32(dellm[(i)]));
  36.                         dataGridView2.Rows.RemoveAt(Convert.ToInt32(dellm[(i)]));
  37.                     }
  38.                     File.Delete("dell.tmp");
  39.                 }
  40.                 label1.Text = "Lines: " + (dataGridView1.Rows.Count - 1);
  41.                 label2.Text = "Lines: " + (dataGridView2.Rows.Count - 1);
  42.                 MessageBox.Show("Проверка окончена!","INFO",MessageBoxButtons.OK,MessageBoxIcon.Information);
  43.             }
  44.             else
  45.             {
  46.                 MessageBox.Show("Не соответствие строк , и контролеров\n\n"+"Строки: "+dat1+"\nКонтролеры: "+dat2, "ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
  47.             }
  48.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы