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

本文关键字:一行 其他 读取 文件 取文本 | 更新日期: 2024-09-23 10:31:37

我正在做这项作业,我需要读取一个整数文本文件,将数字存储到数组中。然后将每行中的数字平方(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();
}

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

我认为这应该满足您的需要:

编辑:

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

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();     
}

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

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

此外,您的"inputLine.Splitt()"也是不必要的。您可以解析inputLine字符串。

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

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