c#并行数组和顺序数组的混淆

本文关键字:数组 并行 顺序 | 更新日期: 2023-09-27 18:15:17

我正在上一门入门课程,在我们介绍了方法和数组之前,并没有遇到太多麻烦。我很难弄清楚如何运行一个数组的序列搜索,这个数组允许我接受用户输入的邮政编码,看看它是否在邮政编码文本文件中。如果是的话,我需要看看它在另一个文本文件"AGI文本文件"中是什么索引下标,并将AGI编号输出到我的agiOutputLabel。

如果有人能澄清我的误解,或者给我解释一下比我的课本更有意义的,我将不胜感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace AGI_Florida
{
    public partial class Form1 : Form
    {
        public Form1()
        {          
            InitializeComponent();
        }
        // Creating an array method
        private void SetZipArray(int[] zips)
        {
        }
        private void SetAGIArray(double[] agi)
        {
        }
        private void FindAGI(int[] zips, int agi)
        {
            bool found = false;
            int index = 0;
            int position = -1;
            while (!found && index < zips.Length)
            {
                if (zips[index] == agi)
                {
                    found = true;
                    position = index;
                }
                index++;
            }
            return;
        }

        private void getAgiButton_Click(object sender, EventArgs e)
        {
            try
            {
                // Initializing the array with 917 spaces
                int[] zips = new int[917];
                int[] agi = new int[917];
                // Pulling array from text file
                StreamReader inputFile = File.OpenText("AGI_FL_ZipCodes-               1.txt");
                // Setting index to 0 so the loop knows to stop at 917 spaces
                int index = 0;
                // Running loop
                while ((index < zips.Length) && (!inputFile.EndOfStream))
                {
                    // Creating the index from the zip and parsing it 
                    zips[index] = int.Parse(inputFile.ReadLine());
                    index++;
                }
                inputFile.Close();
                int zipInput;

                if (int.TryParse(zipInputTextBox.Text, out zipInput))
                {
                    if (zipInput > 32003 && zipInput < 34997)
                    {
                        if (zipInput != -1)
                    {
                            MessageBox.Show(agi.ToString());
                    }
                    }
                    else
                    {
                        MessageBox.Show("Please enter a valid zip code");
                    }
                }

                else
                {
                    MessageBox.Show("Please enter a valid zip code");
                    zipInputTextBox.Text = " ";
                    zipInputTextBox.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void getAverageAgiButton_Click(object sender, EventArgs e)
        {
            // initialize new array
            double[] agi = new double[917];
            StreamReader inputFile = File.OpenText("AGI_FL-1-1.txt");
            // Setting index to 0 so the loop knows to stop at 917 spaces
            int index = 0;
            while ((index < agi.Length) && (!inputFile.EndOfStream))
            {
                // Creating the index from the zip and parsing it 
                agi[index] = double.Parse(inputFile.ReadLine());
                index++;
            }
            inputFile.Close();
            // create a variable sum
            double sum = 0;
            // create a loop to read through the text file
            for (index = 0; index < agi.Length; index++)
            {
                // sum the index of values
                sum += agi[index];
            }
            // perform calculations
            double averageAgi = sum / (double) agi.Length;
            // print to label
            agiLabel.Text = averageAgi.ToString("c");
        }
    }
    }

c#并行数组和顺序数组的混淆

首先,欢迎大家来编码!

这里有很多改进的机会。让我们把一些代码写得更简洁一点

<标题>神奇数字。

让我们去掉那个神奇的数字917。与其创建一个已知长度的数组,然后去一个包含未知数据的文件,希望它们能对齐,为什么不直接使用文件

中数据的长度呢?
var filePath = "AGI_FL_ZipCodes-1.txt";
var allLines = File.ReadAllLines(filePath); //Nicer version of File.OpenText +ReadLine+EndOfStream+whileloop
int[] zips = new int[allLines.Length];  //instead of hard coded magic value of 917;

解析和循环

但是现在你仍然需要读取和解析所有的值。

for (int i = 0; i < allLines.Length; i++)
{
    var line = allLines[i];
    zips[i] = int.Parse(line);  //Chance of an error here is the file is invalid :-/
}
<标题> 条件检查

您有一个条件检查if (zipInput > 32003 && zipInput < 34997),这很好。然而,考虑到我们的代码被读取的次数比被编写的次数要多,最好以一种直观的方式对代码进行格式化。通过将边界值移动到语句的边缘,并使所有比较操作符指向相同的方向,我们可以看到,我们希望zipInput值 32003和34997之间(不包括)

if (32003 < zipInput && zipInput < 34997)
<标题> 死代码

接下来检查zipInput是否不等于-1。这是不必要的,因为在此之前会立即进行检查。32003 ~ 34997之间的值不等于-1

您还声明了agi变量,但从不使用它。你可以删除这一行

int[] agi = new int[917];

查找数组中的值

你的FindAGI方法看起来很好。但是,我会将其"升级"为返回您正在寻找的值的函数。

private int IndexOf(int[] source, int value)
{
    for (int i = 0; i < source.Length; i++)
    {
        if (source[i] == value)
        {
            return i;
        }
    }
    return -1;
}

注意,它将在找到元素后立即终止。如果没有匹配,那么它将返回-1(这是非常标准的行为)。

这可能会帮助你得到你的方式,但现在我不知道任何这段代码实际上是在哪里使用,即FindAGI(现在的IndexOf)似乎没有被引用。