如何从文本文件中读取数字,并从文本文件中输出每行的总和 [英] How can I read numbers from a textfile and output sum for each line from a textfile
问题描述
Read in a series of numbers from a text-file (the data is given below). For each row in the file, setup a thread to calculate the sum of the values of that vector. Each of the vector sums must then be written to a new file, sorted in ascending order.
Input files contents:
6,12,2,9,17
7,3,15,19,4
10,5,8,21,13
我尝试过:
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.IO;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:Test.txt";
string line;
if (File.Exists(filePath))
{
StreamReader file = null;
try
{
file = new StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
finally
{
if (file != null)
file.Close();
}
}
Console.ReadLine();
}
}
}
推荐答案
这是你的作业 - 所以我不会给你代码。
我不打扰StreamReader:
This is your homework - so I won't give you the code.
I wouldn't bother with a StreamReader:
string[] lines = File.ReadAllLines(pathToFile);
这样做会好的。然后,您可以使用 foreach
循环来处理每个字符串。
在循环内部,设置一个线程(BackgroundWorker可能是最简单的方法)处理每一行。
在线程中使用string.Split来打破每个逗号上的行:
Will do it fine. You can then use a foreach
loop to process each string.
Inside the loop, set up a thread (a BackgroundWorker is probably the easiest way) to process each line.
In the thread use string.Split to break the line on each comma:
string[] parts = line.Split(',');
然后您可以在每个字符串上使用int.TryParse将其转换为数字。
然后您可以计算出向量和并返回它是主线程。
主线程组合所有总和,将它们聚合在一个集合中,对集合进行排序,并将它们写入新文件。
一次做一点,这一切都很简单!
You can then use int.TryParse on each string to convert it to a number.
You can then work out the vector sum and return it to the main thread.
The main thread assembles all the sums, aggregates them in a collection, sorts the collection, and writes them to the new file.
Do it a bit at a time, and it's all pretty simple stuff!
谢谢我在这里做的功课是一个解决方案
Thank I did my homework here is a solution
using System;
using System.IO;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
// Read the data.txt textfile.
var data = System.IO.File.ReadAllText(@"..\..\Reading.txt");
// Create a new List of float[]
var arrays = new List<float[]> ();
// Split data file content into lines
var lines = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
// Loop all lines
foreach (var line in lines)
{
// Create a new List<float> representing all the commaseparated numbers in this line
var lineArray = new List <float> ();
// Slipt line by , and loop through all the numeric valus
foreach (var s in line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
// Add converted numeric value to our lineArray
lineArray.Add(Convert.ToInt64(s));
//lineArray.Add(Convert.ToSingle(s));
}
// Add lineArray to main array
arrays.Add(lineArray.ToArray());
// Loop repeats until there are noe more lines
int i = 0, result = 0;
while (i < lineArray.Count)
{
result += Convert.ToInt32(lineArray[i++]);
}
Console.WriteLine(result);
}
//var numberOfRows = lines.Count();
//var numberOfValues = arrays.Sum(s => s.Length);
//Console.WriteLine(numberOfRows);
//Console.WriteLine(numberOfValues);
Console.ReadLine();
}
}
}
这篇关于如何从文本文件中读取数字,并从文本文件中输出每行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!