读取文本文件并将第一行乘以其他每一行
reading a text file and multiplying the first line by each of the other lines
我正在做这个作业,我需要读取整数文本文件,将数字存储到数组中。然后将每行中的数字(25 之后)平方,然后将平方除以 25,然后检查结果是否比 150
我卡住的地方是读取每一行的数字并像我应该的那样在我的方法中使用它们,到目前为止我的循环和数组打印将每个数字按顺序放入文件中。
我非常感谢任何关于数组部分的帮助,谢谢。
这是文本文件:
25 150
60
63
61
70
72
68
66
68
70
所以,取Math.Pow(60,2) / 25
,和Math.Pow(63,2) / 25
等等。那么如果大于150就打印"yes",如果小于150就打印"no"
这是我所拥有的:
我还有一个 class
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();
}
让我从你的代码中提取几行:
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);
达到同样的效果
来自你在问题中提到的例子
take Math.Pow(60,2) / 25, and Math.Pow(63,2) / 25 and so on.
您必须将文件中的第一个值指定为 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);
}
}
我认为这应该可以满足您的需求:
编辑:
根据您的意见,如果您想在第一行读取多个值,可以用逗号分隔它们,然后再拆分。
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();
}
您会发现您编写的代码使用输入文件的第一行和第二行调用电阻器构造函数,然后是第三行和第四行,然后是第五行和第六行...
您的描述表明您希望保留第一行,然后将其用于构造每个电阻器对象。也许我误解了你的问题。您可能需要考虑针对几个预期输出点显示几个输出点。
你的'inputLine.Split()'也是不必要的。你可以只解析 inputLine 字符串。
我正在做这个作业,我需要读取整数文本文件,将数字存储到数组中。然后将每行中的数字(25 之后)平方,然后将平方除以 25,然后检查结果是否比 150
我卡住的地方是读取每一行的数字并像我应该的那样在我的方法中使用它们,到目前为止我的循环和数组打印将每个数字按顺序放入文件中。
我非常感谢任何关于数组部分的帮助,谢谢。
这是文本文件:
25 150
60
63
61
70
72
68
66
68
70
所以,取Math.Pow(60,2) / 25
,和Math.Pow(63,2) / 25
等等。那么如果大于150就打印"yes",如果小于150就打印"no"
这是我所拥有的: 我还有一个 class
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();
}
让我从你的代码中提取几行:
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);
来自你在问题中提到的例子
take Math.Pow(60,2) / 25, and Math.Pow(63,2) / 25 and so on.
您必须将文件中的第一个值指定为 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);
}
}
我认为这应该可以满足您的需求:
编辑:
根据您的意见,如果您想在第一行读取多个值,可以用逗号分隔它们,然后再拆分。
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();
}
您会发现您编写的代码使用输入文件的第一行和第二行调用电阻器构造函数,然后是第三行和第四行,然后是第五行和第六行...
您的描述表明您希望保留第一行,然后将其用于构造每个电阻器对象。也许我误解了你的问题。您可能需要考虑针对几个预期输出点显示几个输出点。
你的'inputLine.Split()'也是不必要的。你可以只解析 inputLine 字符串。