C简单文件整数计算程序实现问题

时间:2013-11-14 00:17:35

标签: c arrays file count percentage

我做了这个程序,它在给定的文件中找到特定数字的出现。

这是我的完整计划:

#include <string.h>
#define SIZE 100

int main(void) {
   int count=0;
   char *pch=NULL;
   char line[SIZE];
   char target[SIZE]={"20"};
   FILE *fp=fopen("countNumber.txt","r");
   if(!fp) {
      printf("Error unable to open the file\n");
      return 0;
   }
   while(fgets(line, SIZE, fp)){          //gets each line of the file
      pch=&line[0];                       //sets the pointer address to the first char in line
      while((pch=strstr(pch,target)) != NULL) {  //searches for all occurrences of target in line
         //printf("%s\n",pch++); getchar();
         count++;
      }
   }

   fclose(fp);
   printf("target string %s was found %d times\n",target, count);
   return 0;
}

我的计划:

我正在考虑做一些棘手的事情。 我的方法是否正确?

3 个答案:

答案 0 :(得分:1)

通常的做法是:

  • 读入所有数字并将它们放入一个数组中(这有助于提前知道有多少数字,这样你就可以正确调整数组的大小;否则你必须首先计算它们,然后读取它们)< / p>

  • 按升序排序

  • 找到第90个百分点,找到sortedElement [floor(N * 0.9)]

  • 后面的元素

排序有点先进。有一些简单的(理解和实现)算法可以很好地处理小数据集。一种这样的算法是“冒泡排序”。你从一端开始,比较两个数字。较大的一个“泡沫”,再次比较,继续前进。一圈之后,你的最大数字是最高的。现在重复一遍,从底部开始,但要尽早停止。如果你只需要第90个百分位数(而不是一个完全排序的数组),你只需要做几次(N次的1/10) - 因为当你有10%的最大数字时,它们中最低的是你的答案。

根据问题的优秀措辞,我觉得你自己能够接受编写这些代码的挑战;如果你不是,请发表评论!

编辑这里是代码:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  FILE* fp;
  char* chBuf=NULL; // where line will be stored
  int* myArray;
  int ii, jj;
  int lineCount;
  int numCount;
  size_t byteCount; // used for reading in the line

  if((fp = fopen("numbers.txt", "r")) == NULL) {
    printf("Unable to open file\n");
    return -1;
  }

  // got here because file is openened.
  // Let's find out how many lines there are
  lineCount = 0;
  while(getline(&chBuf, &byteCount, fp)>0) lineCount++;
  printf("There are %d lines in the file\n", lineCount);

  // now "rewind" to the beginning, and read one line at a time:
  fseek(fp, 0, SEEK_SET);

  // create space for the numbers:
  myArray = malloc(lineCount * sizeof(int));
  numCount = 0;

  // read numbers in - this time, convert them to integers:
  while(getline(&chBuf, &byteCount, fp) > 0) {
    myArray[numCount] = atoi(chBuf);
    // take this line out - just there to show it is working:
    printf("converted number %d: it is %d\n", numCount, myArray[numCount]);
    numCount++;
  }
  fclose(fp);

  // now we have to sort. Since data was sorted low to high,
  // I will sort high to low just to show it works:

  for(ii = 0; ii < numCount - 1; ii++) {
    for(jj = ii + 1; jj < numCount; jj++) {
      if(myArray[ii] < myArray[jj]) {
        int temp = myArray[ii];
        myArray[ii] = myArray[jj];
        myArray[jj] = temp;
      }
    }
    printf("sorted element %d: %d\n", ii, myArray[ii]);
  }
  // we never "sort" the last number... it bubbled to the end:
  printf("sorted element %d: %d\n", ii, myArray[ii]);

  // now find 10% of the number of elements (rounded down)
  // and we will have the number that is bigger than 90% of the numbers in the file
  int index90 = 0.1 * numCount - 1; // automatically gets truncated;
                                    // offset by 1 since index starts at 0
  printf("The first number bigger than 90%% is element %d: it is %d\n", \
    index90, myArray[index90]);
}

这里有几个“技巧”值得向新手程序员指出:

  1. 检查文件是否已成功打开,如果不是
  2. 则执行操作
  3. 使用getline(实际上是gcc扩展名 - 我不知道你是否拥有它)来安全地读取一行:它将确保缓冲区中有足够的空间。您的方法对您的文件有效 - 我的“通常更安全”。
  4. 使用malloc为数字数组分配足够的空间
  5. 我排序“所有数字”,即使我真的只需要排序前10%来解决问题。您可以通过更改外部排序循环中ii的上限来提高性能(对于此实例)。
  6. 我使用的事实是,在我计算所需数字的索引时,为int分配浮点数会自动截断它。
  7. 享受!

答案 1 :(得分:0)

您需要有办法分隔文件中的数字。无论如何,在您的代码中,您可以将200作为另一个20。

关于您的计划,如果您可以将所有数字存入内存,则必须订购它们。一种方法是使用堆来表示订购数据的二叉树。订购数据后,您可以获得10%的最高价格。 O(log n)中的所有内容,但文件读取和堆中的插入量,将为O(n)。

答案 2 :(得分:0)

您需要考虑以下几点:   - 您需要做的第一件事是将您从文件中读取的数字转换为整数(请参阅 atoi 函数)。   - 第二,确保你分配足够的内存来容纳你的所有数字(100可能不够)   - 确保使用正确的数据类型(int应该没问题)

一旦你读完了记忆中的所有数字,你可以随心所欲地做任何事情:对它们进行排序,找到min,max..etc