2014-07-16 39 views
-3

我正在使用C#我有一個函數來檢查這個值。 (姓名,身份證號碼和學校號碼姓名,身份證號碼和學校號碼控制空缺

我的記錄在文本文件上。

如果兩個或兩個以上相同的名字和姓氏(不同的身份號和學號)的文本文件,我需要顯示他們。

如何用循環來做到這一點?

注= identityBox是我的TextBox名稱和身份號碼。這段代碼只運行一條記錄。這是identityFounder Code。姓名和創始人代碼與此相同。我將使用下一個和上一個按鈕查看所有記錄。這裏是C#代碼。請幫幫我。謝謝。

void identityFounder() 
    { 
     int inout = 0; 
     double identityNo = Convert.ToDouble(founderBox.Text); 

     String[] line = File.ReadAllLines("C:\\OgrenciBilgisi_v2.txt"); 

     for (int i = 0; i < line.Length; i++) 
     { 
      if (line[i].Contains(identityNo.ToString())) 
      { 
       temp = i; 
       inout = 1; 
       break; 
      } 

      else 
      { 
       inout = 0; 
       continue; 
      } 
     } 

     if (inout == 1) 
     { 
      name.Text = line[temp - 3]; 
      surname.Text = line[temp - 2]; 
      address.Text = line[temp - 1]; 
      identity.Text = line[temp]; 
      school.Text = line[temp + 1]; 
      number.Text = line[temp + 2]; 
      faculty.Text = line[temp + 3]; 
      deparrtment.Text = line[temp + 4]; 
      class.Text = line[temp + 5]; 
     } 

     else 
     { 
      MessageBox.Show("Record cannot found file.","Warning",MessageBoxButtons.OK 
       ,MessageBoxIcon.Information); 
     } 
    } 
+0

你能從文本文件發佈一個示例行嗎? –

+0

這與TextBox有什麼關係?如果我們可以使用LINQ,查找並顯示重複內容應該足夠簡單,是嗎?另外,如果這都是文本框,我們應該如何顯示多個項目? – BradleyDotNET

+0

@Justin R.你爲什麼要這樣?我可以爲它做一個截圖。在文本文件上查看我的記錄類型。這裏是SS - > http://s7.directupload.net/images/140716/3hooj3u2.jpg –

回答

0

首先,一對夫婦快速的旁白:

  1. INOUT應該是一個布爾值,而不是一個int。
  2. 您可以刪除else塊;它沒有做任何事情。

現在,更廣泛地說,您要做的是跟蹤在解析記錄時已經看到的名稱。如果你一次解析所有的身份,這實際上會更快更容易。由於您的行不是按順序排列的,所以唯一的方法是使用狀態機。

我只是做一個小的控制檯應用程序來解析文本文件,尋找重複的名字,如果它發現任何然後報告文件名稱(或打印整個文件到屏幕或任何;我不清楚你的意思是「展示所有人」)。可能是這樣的:

struct Record 
{ 
    public string Name, Surname; 
    public int Identity, School; 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     const string path = @"C:\OgrenciBilgisi_v2.txt"; 
     var position = 0; 
     var record = new Record(); 
     var records = new Dictionary<string, Record>(); // key is the concatenation of name and surname 

     using (var reader = new StreamReader(path)) 
     { 
      var line = reader.ReadLine(); 
      if (line == "---") // TODO use your record delimiter here 
      { 
       // start of new record. compare the existing one and commit it if all fields have been written to 
       if (position == 9) 
       { 
        // compare records 
        var key = record.Name + record.Surname; 
        if (records.ContainsKey(key)) 
        { 
         // this is a duplicate student name. confirm that the id and school # are different 
         if (records[key].Identity == record.Identity && 
          records[key].School == record.School) 
         { 
          // id and school are the same, so this is an entirely duplicate record 
         } 
         else 
         { 
          // id and school are different, but name and surname are the same 
          // TODO: show all records 
          return; 
         } 
        } 
        else 
        { 
         records.Add(key, record); 
        } 
       } 
       else 
       { 
        Console.Error.WriteLine("Not all fields in record. Malformed data."); 
       } 

       position = 0; 
       record = new Record(); 
      } 
      else 
      { 
       switch (position) 
       { 
        case 0: 
         record.Name = line; 
         break; 
        case 1: 
         record.Surname = line; 
         break; 
        case 3: 
         int id; 
         if (int.TryParse(line, out id)) 
         { 
          record.Identity = id; 
         } // else there's an error 
         break; 
        case 4: 
         int school; 
         if (int.TryParse(line, out school)) 
         { 
          record.School = school; 
         } // else there's an error 
         break; 
       } 
       position++; 
      } 
     } 
    } 
} 

這假設你的數據格式不錯;添加錯誤檢查留給讀者的練習:)

+0

謝謝你的回答先生,它工作:) @Justin R. 對不起,我的不敬。 –