本文介绍了阅读多个整型数组从一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要从一个txt文件,输出的最大总和子数组在多个阵列读取问题。这里是文本文件:

  [1,4,-9,8,1,3,3,-1,-4,-6,2,8,19,-10,-11 ]
[2,9,8,6,5,-11,9,-11,7,5,-1,-8,-3,7,-2]
[10,-11,-1,-9,33,-45,23,24,-1,-7,-8,19]
[31,-41,59,26,-53,58,97,-93,-23,84]
并[3,2,1,1,-8,1,1,2,3]
[12,99,99,-99,-27,0,0,0,-3,10]
[-2,-1,-3,4,-1 2,1,-5,4]

我无法弄清楚如何读取整数,并放置在单独的数组,所以我可以在我的执行功能。这里是code(不工作),我想只是在整数阅读:

  FILE * MYFILE
MYFILE = FOPEN(MSS_TestProblems.txt,R);INT numArray [100];
INT I;对于(i = 0; I< 100;我++)
{
     的fscanf(MYFILE,%1D,&安培; numArray [I]);
}对于(i = 0; I< 100;我++)
{
     的printf(%d个,numArray [I]);
}

我如何阅读这些整数并将它们放置在不同的阵列,所以我可以执行的操作?谢谢!

我有code确定的最大子阵,我从文件中的值加载到自己单独的数组,然后将它们传递给这个函数的过程中苦苦挣扎:

更新:我应该找到每个阵列的最大子阵列。我不是比较独立数组的最大子阵列。这里是什么我应该写一个文件的示例:

  [1,4,-9,8,1,3,3,-1,-4,-6,2,8,19,-10,-11 ]
[8,1,3,3,1,-1,-4,-6,2,8,19]
34[2,9,8,6,5,-11,9,-11,7,5,-1,-8,-3,7 -2]
[2,9,8,6,5]
三十[10,-11,-1,-9,33,-45,23,24,-1,-7 -8,19]
[23,24,-1,-7,-8,19]
50[31,-41,59,26,-53,58,97,-93,-23,84]
[59,26,-53,58,97]
187并[3,2,1,1,-8,1,1,2,3]
并[3,2,1,1]
7[12,99,99,-99,-27,0,0,0,-3,10]
[12,99,99]
210[-2,-1,-3,4,-1 2,1,-5,4]
[4,-1,2,1]
6


解决方案

分而治之。任务分解成片。把每件到一个单独的功能帮助。

Only 2 int arrays needed: The current array of int read and the best one. Use read_ints() to read one line. Various tests were applied to insure data was as expected. A classic approach is to read a line into a buffer (might need 100*40 char for a worst case) and then process it. The below reads pieces of a line, look for the start-of-Frame '[' and end-of-frame ']' to insure data integrity.

#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define StartOfFrame '['
#define Separator ','
#define EndOfFrame ']'
#define INT_COUNT_MAX 100

// Return number of `int` read
// or 0 on syntax error
// or EOF
int read_ints(int *dest, int size, FILE *inf) {
  int ch;
  char delimiter = 0;
  ch = fgetc(inf);
  if (ch == EOF) return EOF;
  if (ch != StartOfFrame) return 0; // unexpected text
  int i;
  for (i = 0; i < size; i++) {
    if (fscanf(inf, "%d%c", &dest[i], &delimiter) != 2) return 0;
    if (delimiter == EndOfFrame) break;
    if (delimiter != Separator) return 0;
  }
  do {
    ch = fgetc(inf);
    if (ch == '\n' || ch == EOF) return i;
  } while (isspace(ch));
  return 0; // unexpected text
}

OP's offered double maxSubArray(double * Array1);, but that lacks a length of valid int. Of course that array could be pre-filled with 0.

In writing a complete solution, the various error checking helped in quickly identifying problems.

The below read_lines() wraps the repeated calls of read_ints() and processes the results.

OP Ignore the rest should you not want to see the complete solution.

int read_lines(FILE *inf) {
  int best[INT_COUNT_MAX];
  unsigned best_line = 0;
  int best_count = 0;
  long long best_sum = LLONG_MIN;
  int a[INT_COUNT_MAX];
  unsigned line_count = 0;

  int count;
  while ((count = read_ints(a, INT_COUNT_MAX, inf)) >= 0) {
    line_count++;
    if (count == 0) {
      fprintf(stderr, "Trouble reading line %u\n", line_count);
      return 1;
    }
    long long sum = 0;
    for (int i = 0; i < count; i++) {
      sum += a[i];
    }
    if (sum > best_sum) {
      best_sum = sum;
      best_count = count;
      best_line =line_count;
      memcpy(best, a, sizeof best[0] * count);
    }
  }
  printf("Greatest sum:%lld on line:%u\n", best_sum, best_line);
  fputc(StartOfFrame, stdout);
  for (int i = 0; i < best_count; i++) {
    if (i > 0) printf("%c", Separator);
    printf("%d", best[i]);
  }
  fputc(EndOfFrame, stdout);
  return 0;
}
int main(void) {
  const char *filename = "MSS_TestProblems.txt";
  FILE *myFile;
  myFile = fopen(filename, "r");
  if (!myFile) {
    fprintf(stderr, "Unable to open file \"%s\"\n", filename);
    return 1;
  }
  read_lines(myFile);
  fclose(myFile);
  return 0;
}

--

Greatest sum:81 on line:6
[12,99,99,-99,-27,0,0,0,-3]

这篇关于阅读多个整型数组从一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:27