2012-09-07 27 views
-5

我正在用C++編寫一個程序來計算文本文檔中字母的數量。我需要知道我做錯了什麼,我得到了瘋狂的結果數字。上次我得到了一些很好的幫助,在計數後我把「頻率[?]」。計數本身有什麼問題,但是什麼?字母計數器給出錯誤結果

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

const int ALPHA_SIZE = 26; 

void open_file (ifstream &); 

void process (ifstream &, int [], string lineBuffer); 

void draw (int []); 

void printchar (int[]); 

string lineBuffer; 


int main() 
{ 
    string lineBuffer; 
    ifstream infile; 
    open_file (infile); 

    int frequency [ALPHA_SIZE]; 

    process (infile, frequency, lineBuffer); 
     draw (frequency); 


    system("Pause"); 
    return 0; 
} 

void open_file (ifstream & inf) 
{ 
    char infilename[50]; //actual file name 
    cout << "File name for input? "; 
    cin >> infilename; 
    inf.open(infilename); 
} 

void process (ifstream &infile, int frequency[], string lineBuffer) 
{ 

    int A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z; 
    A = 0; 
    B = 0; 
    C = 0; 
    D = 0; 
    E = 0; 
    F = 0; 
    G = 0; 
    H = 0; 
    I = 0; 
    J = 0; 
    K = 0; 
    L = 0; 
    M = 0; 
    N = 0; 
    O = 0; 
    P = 0; 
    Q = 0; 
    R = 0; 
    S = 0; 
    T = 0; 
    U = 0; 
    V = 0; 
    W = 0; 
    X = 0; 
    Y = 0; 
    Z = 0; 
    if (infile.is_open()) 
    { 

     while(getline(infile, lineBuffer)) 

     { 

     if ('a' || 'A') 
      { A++; 
      } 
     if ('b' || 'B') 
      { B++; 
      } 
     if ('c' || 'C') 
      { C++; 
      } 
     if ('d' || 'D') 
      { D++; 
      } 
     if ('e' || 'E') 
      { B++; 
      } 
     if ('f' || 'F') 
      { F++; 
      } 
     if ('g' || 'G') 
      { G++; 
      } 
     if ('h' || 'H') 
      { H++; 
      } 
     if ('i' || 'I') 
      { I++; 
      } 
     if ('j' || 'J') 
      { J++; 
      } 
     if ('k' || 'K') 
      { K++; 
      } 
     if ('l' || 'L') 
      { L++; 
      } 
     if ('m' || 'M') 
      { M++; 
      } 
     if ('n' || 'N') 
      { N++; 
      } 
     if ('o' || 'O') 
      { O++; 
      } 
     if ('p' || 'P') 
      { P++; 
      } 
     if ('q' || 'Q') 
      { Q++; 
      } 
     if ('r' || 'R') 
      { R++; 
      } 
     if ('s' || 'S') 
      { S++; 
      } 
     if ('t' || 'T') 
      { T++; 
      } 
     if ('u' || 'U') 
      { U++; 
      } 
     if ('v' || 'V') 
      { V++; 
      } 
     if ('w' || 'W') 
      { W++; 
      } 
     if ('x' || 'X') 
      { X++; 
      } 
     if ('y' || 'Y') 
      { Y++; 
      } 
     if ('z' || 'Z') 
      { Z++; 
      } 
      } 
     infile.close(); 
     frequency [0] = A; 
     frequency [1] = B; 
     frequency [2] = C; 
     frequency [3] = D; 
     frequency [4] = E; 
     frequency [5] = F; 
     frequency [6] = G; 
     frequency [7] = H; 
     frequency [8] = I; 
     frequency [9] = J; 
     frequency [10] = K; 
     frequency [11] = L; 
     frequency [12] = M; 
     frequency [13] = N; 
     frequency [14] = O; 
     frequency [15] = P; 
     frequency [16] = Q; 
     frequency [17] = R; 
     frequency [18] = S; 
     frequency [19] = T; 
     frequency [20] = U; 
     frequency [21] = V; 
     frequency [22] = W; 
     frequency [23] = X; 
     frequency [24] = Y; 
     frequency [25] = Z; 


    } 
} 

void draw(int frequency[]) 
{ 
    cout << "A:" << frequency[0] << endl; 
    cout << "B:" << frequency[1] << endl; 
    cout << "C:" << frequency[2] << endl; 
    cout << "D:" << frequency[3]<< endl; 
    cout << "E:" << frequency[4] << endl; 
    cout << "F:" << frequency[5] << endl; 
    cout << "G:" << frequency[6] << endl; 
    cout << "H:" << frequency[7] << endl; 
    cout << "I:" << frequency[8] << endl; 
    cout << "J:" << frequency[9] << endl; 
    cout << "K:" << frequency[10] << endl; 
    cout << "L:" << frequency[11] << endl; 
    cout << "M:" << frequency[12] << endl; 
    cout << "N:" << frequency[13] << endl; 
    cout << "O:" << frequency[14] << endl; 
    cout << "P:" << frequency[15] << endl; 
    cout << "Q:" << frequency[16] << endl; 
    cout << "R:" << frequency[17] << endl; 
    cout << "S:" << frequency[18] << endl; 
    cout << "T:" << frequency[19] << endl; 
    cout << "U:" << frequency[20] << endl; 
    cout << "V:" << frequency[21] << endl; 
    cout << "W:" << frequency[22] << endl; 
    cout << "X:" << frequency[23] << endl; 
    cout << "Y:" << frequency[24] << endl; 
    cout << "Z:" << frequency[25] << endl; 

} 
+0

數組和循環將使您無法進行所有輸入。 –

+0

'a'|| 'A' - >真,'b'|| 'B' - > true ...這將是分隔 – sehe

+0

這是什麼問題?哪裏?告訴我們你「遇到了另一個錯誤」並不是一個問題。 – jalf

回答

3

這些條件語句中的每一個

if ('n' || 'N') 

將始終爲true,因爲它們是非零常數char值。因此,你用來計數的變量總是增加的。要測試某個角色的發生情況,您需要類似這樣的東西

char char_to_test; 
if (char_to_test == 'n' || char_to_test == 'N') { 
    N++; 
} 

您目前總是增加您的計數。此外,您需要從文件中提取每個字符以進行測試。一旦你從getline得到string,你應該遍歷它的單個字符並按照上面的方法測試它們。

+0

不知道我是否理解,這只是我的第二個學期,我不得不重新學習很多,因爲他們不再教DarkGDK了。 –

+0

你需要從字符串中抽取每個字符,如果它等於你想要的字符,則從TEST中提取。你的測試正在做'if('n'!= 0 ||'N'!= 0)'此刻。很明顯,它們從不是'0',所以if塊中的每條語句都會被執行。無關 – mathematician1975

0

程序中沒有任何內容確保它只讀取每個字符一次。這與此問題類似: http://www.cplusplus.com/forum/beginner/24492/ 您必須告訴程序何時結束。你問程序要做的是「雖然A存在於文件A ++中」那麼它總是存在於文件中。你可以這樣: http://www.cplusplus.com/reference/iostream/istream/get/

這將幫助它通過文件,通過字符char。您仍然必須弄清楚如何在文件末尾停止。

+0

現在有意義,所以使用char的字符測試無論它將測試每一個字符。所以瘋狂的數字是由於我的節目同意一切。 –

0

我認爲你需要努力工作在懶惰。下面是在C版本:

unsigned counts[UCHAR_MAX]; 

int main() { 
    int ch; 

    while (EOF != (ch=getchar())) 
     ++counts[toupper((unsigned char)ch)]; 

    for (int i=0; i<UCHAR_MAX; i++) 
     if (isupper(i)) 
      printf("%c: %u\n", i, counts[i]); 

    return 0; 
} 

我已經簡化一點(例如,stdin閱讀而不是打開一個文件的),但是這畢竟是一個非常簡單的工作 - 不要試圖讓AA比實際情況複雜得多。

+0

對不起,我不明白C,我正在學習C++。 –

1

你的代碼有很多問題。

其中之一就是你正在調用一個函數來打開文件。

void open_file (ifstream & inf)

一次就範圍消失,此功能將關閉文件。

所有的變量都有很多重複的代碼,如果使用循環,它們可以很容易地變得更加緊湊。

#include <string> 
#include <iostream> 
#include <algorithm> 
#include <fstream> 
#include <cctype> 
#include <map> 

int main() { 
    std::map<char,int> alpha; 
    //Fills the map with A-Z with 0 as the value. 
    for(char k = 65; k <= 90; k++) { 
     alpha.insert(std::pair<char,int>(k,0)); 
    } 
    //opens example.txt contains "the quick brown fox jumped over the cow" 
    std::ifstream in("example.txt"); 
    std::string str; 
    if(in.is_open()) { 
     //loops through every line in the file. 
     while(std::getline(in,str)) { 
      //removes all whitespace 
      str.erase(std::remove_if(str.begin(),str.end(), ::isspace),str.end()); 
      //capitalizes the string 
      std::transform(str.begin(),str.end(),str.begin(), ::toupper); 
      //loops through the string 
      for(auto& i : str) { 
       if(isalpha(i)) 
        alpha[i]++; //Increments the frequency. 
      } 
     } 
    } 
    for(auto& i : alpha) 
     std::cout << i.first << " " << i.second << std::endl; 
}