在C中找到一堆数组中的max [英] finding max in bunch of arrays in C
问题描述
我有几个问题. 我有一个包含这些信息的文本文件
I have couple of questions. I have a text file which contains these information
x0,x1,y0,y1
142,310,0,959
299,467,0,959
456,639,0,959
628,796,0,959
-
首先,我想使用
fscanf
读取文本文件,并通过跳过第一行将数字仅分成4个数组,分别为c1
,c2
,c3
和c4
.所以最终结果将是
First, I want to read the text file using
fscanf
and get only the numbers into 4 arrays,c1
,c2
,c3
, andc4
by skipping the first line. So the final result will be
c[1] = {142, 310, 0, 959}
c[2] = {299, 467, 0, 959}
c[3] = {456, 639, 0, 959}
c[4] = {628, 796, 0, 959}
然后,对于每个c[1]
至c[4]
,我想找到最大整数并将其存储在[x,y]数据类型中.因此,例如在c[1]
中,最大值将为max[1]
= [310,959].
Then, for each c[1]
to c[4]
, I want to find the maximum integer and store it in [x, y] datatype. So for example in c[1]
, the max will be max[1]
= [310, 959].
任何人都可以帮忙吗?除使用数组解决此问题外,其他C解决方案也将受到欢迎.
Can anyone help? Other C solution other than using arrays to solve this problem is welcome as well.
在matlab中,代码为
In matlab, the code is
fid = fopen('foo.txt','r');
c = textscan(fid,'%d%d%d%d','delimiter',',','headerlines',1);
fclose(fid);
这将简单地忽略第一行,然后将其余数字复制到matlab中的数组中. 我想将此代码转换为C. 非常感谢.
This will simply ignore the first line, then copy rest of numbers into arrays in matlab. I would like to translate this code into C. Thank you very much.
推荐答案
尽管没有直接满足您的问题,但我提供了使用struct
,std::istream
和std::vector
的阅读要点示例.这些优先于fscanf
和数组.
Although not satsifying your question directly, I have provided an example of reading points using a struct
, std::istream
and std::vector
. These are preferred over fscanf
and arrays.
struct Point
{
unsigned int x;
unsigned int y;
friend std::istream& operator>>(std::istream& inp, Point& p);
};
std::istream& operator>>(std::istream& inp, Point& p)
{
inp >> p.x;
//Insert code here to read the separator character(s)
inp >> p.y;
return inp;
}
void Read_Points(std::istream& input, std::vector<Point>& container)
{
// Ignore the first line.
inp.ignore(1024, '\n');
// Read in the points
Point p;
while (inp >> p)
{
container.push_back(p);
}
return;
}
由于您可以使用Point
声明其他类,所以Point
结构提供了更高的可读性,恕我直言,还具有更多的通用性:
The Point
structure provides more readability, and IMHO, more versatility because you can declare other classes using a Point
:
class Line
{
Point start;
Point end;
};
class Rectangle
{
Point upper_left_corner;
Point lower_right_corner;
friend std::istream& operator>>(std::istream& inp, Rectangle& r);
};
您可以使用operator>>
来添加用于读取文件的方法:
You can add methods for reading from a file using the operator>>
for point:
std::istream& operator>> (std::istream& input, Rectangle& r)
{
inp >> r.upper_left_corner;
//Insert code here to read the separator character(s)
inp >> r.lower_left_corner;
return inp;
}
数组是一个问题,可能导致讨厌的运行时错误,例如缓冲区溢出.对数组执行std::vector
,类或结构.
Arrays are a problem and can lead to nasty runtime errors such as buffer overruns. Perfer std::vector
, classes or structures to arrays.
此外,由于使用了std::istream
,因此这些结构和类可以轻松地用于std::cin
和文件(std::ifstream
):
Also, since std::istream
is used, these structures and classes can be easily used with std::cin
and files (std::ifstream
):
// Input from console
Rectangle r;
std::cin >> r;
这篇关于在C中找到一堆数组中的max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!