Advertisement
ninepintcoggie

Halfabet code

Jun 3rd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm> // std::remove_if
  5. #include <fstream>
  6. using namespace std;
  7. bool checkExists(vector<string> v, string key)
  8. {
  9. bool returnString=false;
  10.  
  11. for (int i=0; i<v.size(); i++)
  12. {
  13. if (v[i]==key)
  14. returnString=true;
  15. }
  16. return returnString;
  17.  
  18. }
  19. void cleanup(string &s)
  20. {
  21. s.erase(remove_if(s.begin(), s.end(), ::ispunct), s.end());
  22. }
  23. string downSize(string &line)
  24. {
  25. char x;
  26. for(char& c : line)
  27. {
  28. x = c;
  29. if (c > 64 && c < 90)
  30. {
  31. c += 32;
  32. }
  33. replace(line.begin(), line.end(), x, c);
  34. }
  35. return line;
  36. }
  37.  
  38. bool isValid(string s)
  39. {
  40. for (int i = 0, j = 1; j < s.length(); i++, j++)
  41. {
  42. if (toupper(s[i]) >= 'A' && toupper(s[i]) < 'N')
  43. {
  44. if (toupper(s[j]) < 'N')
  45. return false;
  46. }
  47. else if (toupper(s[i]) >= 'N')
  48. {
  49. if (toupper(s[j]) >= 'N')
  50. return false;
  51. }
  52. }
  53.  
  54. return true;
  55. }
  56.  
  57. int whichList(string halfabet)
  58. {
  59. int returnInt = -1;
  60.  
  61. //if starts with a-m and ends with a-m
  62. if (halfabet[0]>= 'a' && halfabet[0] <'n')
  63. if (halfabet[halfabet.length()-1]>= 'a' && halfabet[halfabet.length()-1] < 'n')
  64. returnInt=0;
  65. //if starts with a-m and ends with m-z
  66. if (halfabet[0]>= 'a' && halfabet[0] <'n')
  67. if (halfabet[halfabet.length()-1] >= 'n')
  68. returnInt=1;
  69.  
  70. //if starts with a-m and ends with m-z
  71. if (halfabet[0]>= 'n')
  72. if (halfabet[halfabet.length()-1]>= 'a' && halfabet[halfabet.length()-1] < 'n')
  73. returnInt=2;
  74.  
  75. //if starts with m-z and ends with m-z
  76. if (halfabet[0] >= 'n')
  77. if (halfabet[halfabet.length()-1] >= 'n')
  78. returnInt=3;
  79.  
  80. //return -
  81. //0 = FF
  82. //1 = FL
  83. //2 = LF
  84. //3 = LL
  85. return returnInt;
  86. }
  87.  
  88.  
  89.  
  90. int main()
  91. {
  92. vector<string> FF;
  93. vector<string> FL;
  94. vector<string> LF;
  95. vector<string> LL;
  96.  
  97. ifstream fin;
  98. fin.open("Book.txt");
  99. string word;
  100. //if file is open
  101. if(fin.is_open())
  102. {
  103. //while file has text
  104. while (!fin.eof())
  105. {
  106. fin>>word;
  107. downSize(word);
  108. //clean it up for punctuation
  109. cleanup(word);
  110. bool halfabetCheck = isValid(word);
  111. if (halfabetCheck)
  112. {
  113. //find list
  114. //if 0..1..2...3 push back etc
  115. int list = whichList(word);
  116.  
  117. if (list == 0)
  118. {
  119. //check if exists already
  120. if (!checkExists(FF,word))
  121. {
  122. FF.push_back(word);
  123. }
  124. }
  125.  
  126.  
  127. if (list == 1)
  128. {
  129. //check if exists already
  130. if (!checkExists(FL,word))
  131. {
  132. FL.push_back(word);
  133. }
  134.  
  135. }
  136.  
  137.  
  138. if (list == 2)
  139. {
  140. //check if exists already
  141. if (!checkExists(LF,word))
  142. {
  143. LF.push_back(word);
  144. }
  145. }
  146.  
  147.  
  148. if (list == 3)
  149. {
  150. //check if exists already
  151. if (!checkExists(LL,word))
  152. {
  153. LL.push_back(word);
  154. }
  155. }
  156. }
  157. ofstream foutFF;
  158. foutFF.open("FF.txt");
  159. ofstream foutFL;
  160. foutFL.open("FL.txt");
  161. ofstream foutLF;
  162. foutLF.open("LF.txt");
  163. ofstream foutLL;
  164. foutLL.open("LL.txt");
  165.  
  166. for (int i=0; i<FF.size(); i++)
  167. {
  168.  
  169. foutFF<<FF[i]<<endl;
  170. }
  171.  
  172. for (int i=0; i<FL.size(); i++)
  173. {
  174.  
  175. foutFL<<FL[i]<<endl;
  176. }
  177. for (int i=0; i<LF.size(); i++)
  178. {
  179.  
  180. foutLF<<LF[i]<<endl;
  181. }
  182. for (int i=0; i<LL.size(); i++)
  183. {
  184.  
  185. foutLL<<LL[i]<<endl;
  186. }
  187.  
  188. }
  189.  
  190.  
  191. }
  192. fin.close();
  193.  
  194. return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement