Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include "Word.h"
  2. #include<iostream>
  3. #include<string>
  4. #include<fstream>
  5. #include<ctype.h>
  6. using namespace std;
  7.  
  8. bool Word::find(std::string & t)
  9. {
  10.     char last = t[t.length() - 1];
  11.     last = tolower(last);
  12.     int first = letterIndex[last - 97];
  13.     int lastNumber = 0;
  14.     if (last != 'z')
  15.         lastNumber = letterIndex[last - 96];
  16.     else
  17.         lastNumber = word.size();
  18.     bool flag = true;
  19.     int length = t.length();
  20.     for (int i = first; i < lastNumber; i++) {
  21.         if (length == word[i].length()) {
  22.             for (int j = 0, k = length - 1; j < length; j++, k--) {
  23.                 if (tolower(t[k]) != tolower(word[i][j])) {
  24.                     flag = false;
  25.                     break;
  26.                 }
  27.             }
  28.             if (flag) {
  29.                 word[i] = "";
  30.                 return true;
  31.             }
  32.             flag = true;
  33.         }
  34.     }
  35.     return false;
  36. }
  37. void Word::reaoWord()
  38. {
  39.     ifstream data(file, ios::in);
  40.     if (data.is_open())
  41.     {
  42.         string t;
  43.         int i = 97;
  44.         int j = 0;
  45.         while (!data.eof()) {
  46.             data >> t;
  47.             word.push_back(t);
  48.             if (tolower(t[0]) == i) {
  49.                 letterIndex[i - 97]= j;
  50.                 i++;
  51.             }
  52.             j++;
  53.         }
  54.     }
  55.     else
  56.         cout << "Error!!" << endl;
  57. }
  58.  
  59. void Word::findInverseWord()
  60. {
  61.     for (int i = 0; i < word.size(); i++) {
  62.         if (word[i].length() > output.length())
  63.             if (find(word[i])) {
  64.                 output = word[i];
  65.                 word[i] = "";
  66.                 cout << output;
  67.             }
  68.     }
  69. }
  70. void Word::out()
  71. {
  72.     cout << output << endl;
  73. }
  74. Word::Word(std::string _file)
  75. {
  76.     file = _file;
  77.     for (int i = 0; i < 26; i++)
  78.         letterIndex[i] = 0;
  79.     word.clear();
  80.     output = "";
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement