姓名,身份证号码和学号控制无效

时间:2014-07-16 18:23:40

标签: c# loops

我正在使用 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);
        }
    }

1 个答案:

答案 0 :(得分:0)

首先,几个快速的旁白:

  1. inout应该是bool,而不是int。
  2. 你可以删除else块;它没有做任何事情。
  3. 现在,更广泛地说,您要做的是跟踪您在解析记录时已经看到的名称。如果您一次解析所有身份,这实际上会更快更容易。由于您的行除了按顺序之外没有结构化,因此唯一的方法是使用状态机。

    我只想制作一个小的控制台应用程序来解析文本文件,查找重复的名称,如果找到任何,然后报告文件名(或将整个文件打印到屏幕或其他;我不清楚你的意思是“显示所有这些”)。也许是这样的:

    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++;
                }
            }
        }
    }
    

    这假设您的数据格式正确;添加错误检查留给读者练习:)