使用C#并行和顺序阵列进行混淆
我正在上一门介绍课程,在我们被介绍方法和数组之前,我没有遇到太多麻烦。我很难弄清楚如何对一个数组运行序列搜索,该数组允许我获取用户输入的邮政编码,看看它是否在邮政编码文本文件中。 如果是,我需要查看另一个文本文件“AGI文本文件”中的索引下标,并将AGI编号输出到我的AGI OutputLabel 如果有人能澄清我的误解,或者跟我解释一下,这比我的课本更有意义,我将不胜感激使用C#并行和顺序阵列进行混淆,c#,arrays,methods,C#,Arrays,Methods,我正在上一门介绍课程,在我们被介绍方法和数组之前,我没有遇到太多麻烦。我很难弄清楚如何对一个数组运行序列搜索,该数组允许我获取用户输入的邮政编码,看看它是否在邮政编码文本文件中。 如果是,我需要查看另一个文本文件“AGI文本文件”中的索引下标,并将AGI编号输出到我的AGI OutputLabel 如果有人能澄清我的误解,或者跟我解释一下,这比我的课本更有意义,我将不胜感激 using System; using System.Collections.Generic; using System.
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");
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
佛罗里达大学
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
//创建数组方法
私有void SetZipArray(int[]拉链)
{
}
私有void SetAGIArray(双[]agi)
{
}
私有void FindAGI(int[]拉链,int agi)
{
bool-found=false;
int指数=0;
int位置=-1;
而(!found&&index32003&&zipInput<34997)
{
如果(zipInput!=-1)
{
Show(agi.ToString());
}
}
其他的
{
MessageBox.Show(“请输入有效的邮政编码”);
}
}
其他的
{
MessageBox.Show(“请输入有效的邮政编码”);
zipInputTextBox.Text=“”;
zipInputTextBox.Focus();
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
私有void getAverageAgiButton_单击(对象发送者,事件参数e)
{
//初始化新数组
double[]agi=新的double[917];
StreamReader inputFile=File.OpenText(“AGI_FL-1-1.txt”);
//将索引设置为0,以便循环知道在917个空格处停止
int指数=0;
而((索引
首先,欢迎使用编码
这里有很多改进的机会。让我们把一些代码弄得更干净一些
神奇的数字。
让我们摆脱这个神奇的数字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 :-/
}
在数组中查找值
你的FindAGI
方法看起来不错。不过,我会将其“升级”为一个函数,返回您要查找的值
private int IndexOf(int[] source, int value)
{
for (int i = 0; i < source.Length; i++)
{
if (source[i] == value)
{
return i;
}
}
return -1;
}
private int IndexOf(int[]源,int值)
{
for(int i=0;i
请注意,它将在找到元素后立即终止。如果没有匹配,那么它将返回-1(这是相当标准的行为)
这可能会帮助您上路,但目前我看不出这些代码实际在哪里使用,即
FindAGI
(现在IndexOf
)似乎没有被引用。您的示例与并行无关。请提供一些您不了解的具体问题。另一方面,你的问题太宽泛和不清晰,平行性被陈述为在两个不同的文本行之间搜索。i、 例如:获取用户输入zipcode如果zipcode值在“zipcode-fl.txt”中,则检查用户输入zipcode值是否在“zipcode fl.txt”中查找subsc
int[] agi = new int[917];
private int IndexOf(int[] source, int value)
{
for (int i = 0; i < source.Length; i++)
{
if (source[i] == value)
{
return i;
}
}
return -1;
}