在C中查找数组中的最大值

发布于 2024-10-20 19:51:09 字数 825 浏览 2 评论 0原文

我有几个问题。 我有一个包含这些信息的文本文件

x0,x1,y0,y1
142,310,0,959
299,467,0,959
456,639,0,959
628,796,0,959
  1. 首先,我想使用fscanf读取文本文件并仅将数字放入4个数组中,c1c2c3c4 跳过第一行。所以最终的结果就是

    <前><代码>c[1] = {142, 310, 0, 959} c[2] = {299, 467, 0, 959} c[3] = {456, 639, 0, 959} c[4] = {628, 796, 0, 959}
  2. 然后,对于每个 c[1]c[4],我想找到最大整数并将其存储在 [x , y] 数据类型。例如,在 c[1] 中,最大值将为 max[1] = [310, 959]。

有人可以帮忙吗?除了使用数组来解决这个问题之外,其他 C 解决方案也是受欢迎的。

在 matlab 中,代码为

fid = fopen('foo.txt','r');
c = textscan(fid,'%d%d%d%d','delimiter',',','headerlines',1);
fclose(fid);

这将简单地忽略第一行,然后将其余数字复制到 matlab 中的数组中。 我想把这段代码翻译成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
  1. First, I want to read the text file using fscanf and get only the numbers into 4 arrays, c1, c2, c3, and c4 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}
    
  2. 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].

Can anyone help? Other C solution other than using arrays to solve this problem is welcome as well.

In matlab, the code is

fid = fopen('foo.txt','r');
c = textscan(fid,'%d%d%d%d','delimiter',',','headerlines',1);
fclose(fid);

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-10-27 19:51:09

虽然不能直接满足您的问题,但我提供了一个使用 structstd::istreamstd::vector 读取点的示例。这些比 fscanf 和数组更受青睐。

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 声明其他类:

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

您可以使用 添加从文件读取的方法要点:

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、类或结构到数组。

此外,由于使用了 std::istream,这些结构和类可以轻松地与 std::cin 和文件(std::ifstream >):

  // Input from console
  Rectangle r;
  std::cin >> r;

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

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

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

Arrays are a problem and can lead to nasty runtime errors such as buffer overruns. Perfer std::vector, classes or structures to arrays.

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;
ゞ花落谁相伴 2024-10-27 19:51:09

以防万一你的

也欢迎使用其他格式来解决此问题。

也意味着其他语言,解决这个问题的Python代码将是这样的

fo = open('a.txt','r')
line = fo.readline() #ignore first line
max = [sorted(map(int,line.split(',')))[-2:] for line in fo]

,结果将是

[[310, 959]、[467, 959]、[639, 959]、[796, 959]]

Just in case your

Other format to solve this problem is welcome as well.

also means other language, Python code to solve this problem will be like this

fo = open('a.txt','r')
line = fo.readline() #ignore first line
max = [sorted(map(int,line.split(',')))[-2:] for line in fo]

and the result will be

[[310, 959], [467, 959], [639, 959], [796, 959]]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文