2015-11-15 20 views
0

我正在編寫一個程序,可以計算在外部文件中找到某個單詞的次數。要搜索的單詞也在外部文件中,但我可以檢索到這些單詞。我意識到如果單詞完全匹配,它只會更新count的值。例如,如果我正在搜索「學校」一詞並在文本文件中包含「學校」一詞,我認爲count的值不會改變。我還認爲,如果要搜索的單詞是「SCHOOL」,並且文本文件中的單詞是「school」,則計數不會更改。那麼,如何編輯我的if語句,以便例如「學校」一詞將匹配「SCHOOL」和「School」?如何將我從外部文件中的函數獲取的字符串轉換爲全部大寫

這是我的主要功能:

#include <iostream> 
#include "ReadWords.h" 
#include "Writer.h" 
#include <cctype> 
#include <string> 

using namespace std; 


int main() { 

    int x = 9; 


    int count = 0; 
    int count0; 
    int count1; 
    int count2; 
    int count3; 
    int count4; 
    int count5; 
    int count6; 
    int count7; 
    int count8; 
    int count9; 


    int scount; 
    const int size = 10; 
    string word_search[size]; 
    string word; 

    cout << "Please enter a filename: " << flush; 

    char filename[30]; 
    cin >> filename; 
    ReadWords reader(filename); 



    while (reader.isNextWord()){ 
     count = count + 1; 
     reader.getNextWord(); 


    } 


    cout << "There are: " << count << " words in the play" << endl; 


    cout << "Please enter the name of the file with the search words: " << flush; 

    char filename1[30]; 
    cin >> filename1; 
    ReadWords reader1(filename1); 

    scount = 0; 
    while (reader1.isNextWord()) { 

     word_search[scount] = reader1.getNextWord(); 
     ++scount; 

    } 

    cout << "" << endl; 

    while (reader.isNextWord()) { 

這是我試圖將輸入轉換爲大寫,看看這個詞本身的大寫版本一致,但並沒有工作。在這裏,如果第一個字母是大寫,我還需要檢查單詞是否與自己匹配?

 if (reader.getNextWord() == word_search[0] || toupper(reader.getNextWord()) == word_search[0]) { 
      count0 = count0 + 1; 
     } 
     if (reader.getNextWord() == word_search[1]) { 
       count1 = count1 + 1; 
      } 

     if (reader.getNextWord() == word_search[2]) { 
       count2 = count2 + 1; 
      } 
     if (reader.getNextWord() == word_search[3]) { 
       count3 = count3 + 1; 
      } 
     if (reader.getNextWord() == word_search[4]) { 
       count4 = count4 + 1; 
      } 
     if (reader.getNextWord() == word_search[5]) { 
       count5 = count5 + 1; 
      } 
     if (reader.getNextWord() == word_search[6]) { 
       count6 = count6 + 1; 
      } 
     if (reader.getNextWord() == word_search[7]) { 
       count7 = count7 + 1; 
      } 
     if (reader.getNextWord() == word_search[8]) { 
       count8 = count8 + 1; 
      } 
     if (reader.getNextWord() == word_search[9]) { 
       count9 = count9 + 1; 
      } 


    } 


    cout << "Please enter the name of the file to write to: " << flush; 

    char filename2[30]; 
    cin >> filename2; 
    Writer reader2(filename2); 
    cout << "File has been written too.." << endl; 

    reader2.writeInt(count); 
    reader2.writeString("Hello my name is Joshua Ogunnote"); 

    return 0; 
} 

這是我的一些函數聲明爲一個單獨的文件:

#include "ReadWords.h" 
#include <cstring> 
#include <iostream> 
using namespace std; 


void ReadWords::close(){ 
    wordfile.close(); 

} 

ReadWords::ReadWords(const char *filename) { 


     wordfile.open(filename); 

     if (!wordfile) { 
      cout << "could not open " << filename << endl; 
      exit(1); 
     } 
} 

string ReadWords::getNextWord() { 

    string n; 


    if(isNextWord()){ 
     wordfile >> n; 


     int len = n.length(); 
     for(int i = 0; i < len ; i++) { 


      if (ispunct(n[i])) 

        { 
         n.erase(i--, 1); 
         len = n.length(); 
        } 
     } 
      cout << n << endl; 
     return n; 

    } 
} 


bool ReadWords::isNextWord() { 

     if (wordfile.eof()) { 
      return false; 
     } 
     return true; 
} 
+0

請使用[一個最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) –

+0

每當有許多變量具有相同的名稱但只有一個id號不同時,這是一個提示你應該使用一個數組和循環。 –

回答

0

如果你僅僅使用英語,簡單的tolower的()轉換就行了。現在

std::string tolower(std::string s) 
{ 
    for (char& c : s) c = std::tolower(c); 
    return s; 
} 

你可以對它們進行比較:

if (tolower("Hello") == tolower("HELLO")) 

如果您正在使用Unicode的工作,你應該執行一個名爲情況下的文本摺疊轉換,並將得到的字符串數據進行比較。

+0

是的,如果我真的有字符串,但我從我的外部文件中獲取我的字符串,所以我不知道如何正確應用您的邏輯 – jayoguntino

相關問題