读取文本文件并将第一行乘以其他每一行

时间:2016-04-18 01:22:08

标签: c# arrays split streamreader

我正在做这个作业,我需要读取整数的文本文件,将数字存储到数组中。然后对每行中的数字进行平方(25之后),然后将方格除以25,然后检查结果是否超过150

我被困在哪里读取每行的数字并在我的方法中使用它们,就像我应该的那样,到目前为止,我的循环和数组打印将每个数字按顺序放入文件中。

我非常感谢阵列部分的任何帮助,谢谢。

这是文本文件:

25   150
60        
63
61
70
72
68
66
68
70

所以,请Math.Pow(60,2) / 25Math.Pow(63,2) / 25等等。然后如果它高于150,则打印“是”并且如果低于150则打印“否”

这是我所拥有的: 我有另一个班级

class Resistors
{
    //declare variables for the resistance and the volts.
    private  int resistance;
    private int volts;

    public Resistors(int p1, int p2)
    {
        resistance = p2;
        volts = p1;
    }
    //GetPower method.
    //purpose: to get calculate the power dissipation of the resistor.
    //parameters: it takes two intigers.
    //returns: the total power as a double.
    public double GetPower()
    {
        return (Math.Pow(volts, 2) / resistance);

    }
}

这是剩下的。

static void Main(string[] args)

    //declare some variables and an array.
    const int MAX = 50;
    string inputLine = "";
    Resistors[] resistor = new Resistors[MAX];
    //declare a counter and set to zero
    int count = 0;

    // This line of code gets the path to the My Documents Folder
    string environment = System.Environment.GetFolderPath
    (System.Environment.SpecialFolder.Personal) + "\\";
    WriteLine("Resistor Batch Test Analysis Program");
    WriteLine("Data file must be in your Documents folder");
    Write("Please enter the file name: ");
    string input = Console.ReadLine();

    // concatenate the path to the file name
    string path = environment + input;

    // now we can use the full path to get the document
    StreamReader myFile = new StreamReader(path);

    while (inputLine != null)
    {
        inputLine = myFile.ReadLine();
        if (inputLine != null && count < MAX)
        {
            string[] data = inputLine.Split();
            int dataR = int.Parse(data[0]);

            string[] pie = inputLine.Split();
            int pieV = int.Parse(pie[0]);


            resistor[count++] = new Resistors(dataR, pieV);
        }
    }
    WriteLine("Res#\tDissipitation\tPassed");

    for (int j = 0; j < count; j++)
    {

        WriteLine("{0:d}\t{1:N}", j + 1, resistor[j].GetPower());
    }

    ReadKey();
}

3 个答案:

答案 0 :(得分:0)

让我从你的代码中取几行:

if (inputLine != null && count < MAX)
    {
        string[] data = inputLine.Split();
        int dataR = int.Parse(data[0]);
        string[] pie = inputLine.Split();
        int pieV = int.Parse(pie[0]);
    }

你实际上是用split()做同样的事情;此处也不需要Split(),您可以使用int dataR = int.Parse(inputLine);int pieV = int.Parse(inputLine);来实现相同的目标

从您在问题中提到的示例

  

取Math.Pow(60,2)/ 25,Math.Pow(63,2)/ 25等等。

您必须将文件中的第一个值指定为resistance,如果是这样(我理解正确),您可以使用以下代码完成整个操作:

List<string> stringArray = File.ReadAllLines(@"filePath").ToList();
List<int> intList= stringArray.Select(x => x!=null || x!="" ?0:int.Parse(x)).ToList(); 
//Now `intList` will be the list of integers, you can process with them;
int resistance=intArray[0];
for (int i = 1; i < intArray.Count ; i++)
{
  resistor[i] = new Resistors(intArray[i], resistance);
}

您也可以尝试使用代码:

StreamReader myFile = new StreamReader(@"path_here");
const int MAX = 50;
string inputLine = "";
// Resistors[] resistor = new Resistors[MAX];             
int count = 0;
int resistance = 0;
while (inputLine != null)
{
    inputLine = myFile.ReadLine();
    if (inputLine != null && count < MAX)
    {
        int inputInteger = int.Parse(inputLine);
        if (count == 0) { resistance = inputInteger; }
        resistor[count++] = new Resistors(inputInteger, resistance);
    }
}

答案 1 :(得分:0)

我认为这应该做你需要的:

编辑:

根据您的评论,如果您想在第一行读取多个值,可以用逗号分隔它们,然后拆分。

25,150
60        
63
61
70
72
68
66
68
70

static void Main(string[] args)
{            
    // This line of code gets the path to the My Documents Folder
    string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";
    Console.WriteLine("Resistor Batch Test Analysis Program");
    Console.WriteLine("Data file must be in your Documents folder");
    Console.Write("Please enter the file name: ");

    string input = Console.ReadLine();

    // concatenate the path to the file name
    string path = environment + input;

    // Will read all lines
    var lines = File.ReadAllLines(path).ToList();
    // Will get the first line arguments and split them on the comma, you add more arguments if need, just separate them by a comma
    var firstLineArgs = lines[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                .Select(t => Convert.ToInt32(t))
                                .ToArray();
    // Will skip the first line arguments and parse all the following numbers
    var numbers = lines.Skip(1)
                       .Select(t => Convert.ToInt32(t))
                       .ToList();

    // Will create each Resistors object with the first line arguments (25) and the actual number
    // You can do whatever you want with the second arguments (150)
    var resistors = numbers.Select(t => new Resistors(firstLineArgs[0], t))
                           .ToList();  

    Console.WriteLine("Res#\tDissipitation\tPassed");

    foreach (var item in resistors)
    {
        // Check if item.GetPower() is greather firstLineArgs[1] (150)
        // I don't know what you want to do if it's greater
        Console.WriteLine("{0:d}\t{1:N}", resistors.IndexOf(item) + 1, item.GetPower());
    }

    Console.ReadKey();     
}

答案 2 :(得分:0)

您会发现您编写的代码使用输入文件的第1行和第2行调用Resistor构造函数,然后使用第3行和第4行,然后使用第5行和第6行...

您的描述表明您希望保留第一行,然后在构造每个Resistor对象时使用它。也许我误解了你的问题。您可能需要考虑针对预期输出的几个点显示几个输出点。

您的&#39; inputLine.Split()&#39;没必要。你可以解析inputLine字符串。