[英]C Simple File integers count program implementation problems
我做了這個程序,它在給定的文件中找到特定數字的出現。
這是我的完整計划:
#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;
}
我的計划:
我當時正想着做一些棘手的事情。 我的方法是否正確?
通常的方法是:
讀入所有數字並將它們放在一個數組中(這有助於提前知道有多少數字,這樣你就可以正確地調整數組的大小;否則你必須首先計算它們,然后讀取它們)
按升序排序
找到第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]);
}
這里有幾個“技巧”值得向新手程序員指出:
getline
(實際上是一個gcc擴展 - 我不知道你是否擁有它)來安全地讀取一行:它將確保緩沖區中有足夠的空間。 您的方法對您的文件有效 - 我的“通常更安全”。 malloc
為數組數組分配足夠的空間 ii
的上限來提高性能(對於此實例)。 int
將在我想要的數字索引的計算中自動截斷它。 請享用!
您需要有辦法分隔文件中的數字。 無論如何,在您的代碼中,您可以將200作為另一個20。
關於你的計划,如果你可以將所有數字都記在內存中,你將不得不訂購它們。 一種方法是使用堆來表示訂購數據的二叉樹。 訂購數據后,您可以獲得10%的最高價格。 O(log n)中的所有內容,但文件讀取和堆中的插入量,將為O(n)。
您需要考慮以下幾點: - 您需要做的第一件事是將您從文件中讀取的數字轉換為整數(請參閱atoi函數)。 - 第二,確保你分配足夠的內存來保存你的所有數字(100可能不夠) - 確保使用正確的數據類型(int應該沒問題)
一旦你在內存中讀取了所有數字,你就可以隨心所欲地做任何事情:對它們進行排序,找到min,max..etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.