Guest User

Untitled

a guest
Nov 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace::std;
  5.  
  6. // prints all English words which are contiguous substrings of inputtedWord into a file
  7. void embeddedWords( vector< char > &dictionaryWord, vector< char > &inputtedWord );
  8.  
  9. // returns true if and only if dictionaryWord is a substring of inputtedWord
  10. bool isSubstring( vector< char > &dictionaryWord, vector< char > &inputtedWord );
  11.  
  12. int main()
  13. {
  14. vector< char > inputtedWord;
  15.  
  16. cout << "Enter a word: ";
  17. char ch = cin.get();
  18. while( ch != '\n' )
  19. {
  20. inputtedWord.push_back( ch );
  21. ch = cin.get();
  22. }
  23.  
  24. vector< char > dictionaryWord;
  25. embeddedWords( dictionaryWord, inputtedWord );
  26.  
  27. system( "pause" );
  28. }
  29.  
  30. void embeddedWords( vector< char > &dictionaryWord, vector< char > &inputtedWord ){
  31. ifstream infiletext("Dictionary.txt", ifstream::in);
  32. ofstream outfiletext("word.txt",ofstream::out);
  33. if(!infiletext){ //載入失敗
  34. cout<<"error"<<endl;
  35. }
  36. else{ //載入成功
  37. while(!infiletext.eof()){
  38. char ch=infiletext.get(); //放入第一個字元
  39. dictionaryWord.clear();
  40. if (infiletext.fail()) //解決最後有空白數據的bug
  41. break;
  42. while(ch!='\n'){
  43. dictionaryWord.push_back(ch);
  44. ch=infiletext.get();
  45. }
  46. if(dictionaryWord.size()<=inputtedWord.size() && isSubstring(dictionaryWord,inputtedWord)){
  47. for(int i=0;i<dictionaryWord.size();i++){ //印出
  48. cout<<dictionaryWord[i];
  49. outfiletext<<dictionaryWord[i];
  50. }
  51. cout<<endl;
  52. outfiletext<<endl;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. bool isSubstring( vector< char > &dictionaryWord, vector< char > &inputtedWord ){
  59. for(int i=0;i<=inputtedWord.size()-dictionaryWord.size();i++){ //起點做到兩者字串長度相減
  60. bool flag=true;
  61. for(int j=0;j<dictionaryWord.size();j++)
  62. if(dictionaryWord[j]!=inputtedWord[i+j]){ //非子字串時換起點且此段並非子字串
  63. flag=false;
  64. break;
  65. }
  66. if(flag) //上續完全正確,此段為子字串,結束跳出並return true
  67. return true;
  68. }
  69. return false; //上續沒有一段為子字串,結束跳出並return false;
  70. }
Add Comment
Please, Sign In to add comment