fork(2) download
  1. // FR_02_08 - Walidacja adresu e-mail
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <set>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. size_t split(const string& strInput, const string& strDelim, vector<string>& vOutput)
  11. {
  12. size_t pos_start = 0, pos_end, delim_len = strDelim.length();
  13. while ((pos_end = strInput.find(strDelim, pos_start)) != string::npos)
  14. {
  15. vOutput.push_back(strInput.substr(pos_start, pos_end - pos_start));
  16. pos_start = pos_end + delim_len;
  17. }
  18. vOutput.push_back(strInput.substr(pos_start));
  19. return vOutput.size();
  20. }
  21.  
  22. set<char> sAllowed;
  23. set<char> sAllowedLast;
  24.  
  25. void PrepareAllowed()
  26. {
  27. string strAllowed = "1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM._";
  28.  
  29. for (auto c : strAllowed)
  30. sAllowed.insert(c);
  31.  
  32. string strAllowedLast = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
  33.  
  34. for (auto c : strAllowedLast)
  35. sAllowedLast.insert(c);
  36. }
  37.  
  38. bool ValidatePreMalpa(const string& strInput)
  39. {
  40. //pierwszy ciąg znaków musi się składać z n znaków, gdzie n zawiera się w przedziale [1..20]
  41. if (strInput.length() < 1 || strInput.length() > 20)
  42. {
  43. return false;
  44. }
  45.  
  46. for (char c : strInput)
  47. {
  48. if (sAllowed.find(c) == sAllowed.end())
  49. {
  50. //niedozwolony znak
  51. return false;
  52. }
  53. }
  54.  
  55. vector<string> vParts;
  56. split(strInput, ".", vParts);
  57.  
  58. for (const string& s : vParts)
  59. {
  60. if (s.length() == 0)
  61. {
  62. //dwie kropki obok siebie lub kropka na początku lub na koncu
  63. return false;
  64. }
  65. }
  66.  
  67. // cos jeszcze ?
  68.  
  69. return true;
  70. }
  71.  
  72. bool ValidatePostMalpa(const string& strInput)
  73. {
  74. for (char c : strInput)
  75. {
  76. if (sAllowed.find(c) == sAllowed.end())
  77. {
  78. //niedozwolony znak
  79. return false;
  80. }
  81. }
  82.  
  83. vector<string> vParts;
  84. split(strInput, ".", vParts);
  85.  
  86. if (vParts.size() == 1) //brak kropki po @, czyli brak "trzeciego ciągu znaków" (???)
  87. {
  88. const string& strLast = vParts[0];
  89.  
  90. if (strLast.length() < 1 || strLast.length() > 20)
  91. {
  92. // drugi ciąg znaków musi się składać z n znaków, gdzie n zawiera się w przedziale [1..20]
  93. return false;
  94. }
  95. }
  96. else
  97. {
  98. for (const string& s : vParts)
  99. {
  100. if (s.length() == 0)
  101. {
  102. //dwie kropki obok siebie lub kropka na początku lub na koncu
  103. return false;
  104. }
  105. }
  106.  
  107. const string& strLast = vParts[vParts.size() - 1];
  108. if (strLast.length() < 2 || strLast.length() > 3)
  109. {
  110. //[trzeci ciąg znaków składający się z 2 lub 3 liter]
  111. return false;
  112. }
  113.  
  114. for (char c : strLast)
  115. {
  116. if (sAllowedLast.find(c) == sAllowedLast.end())
  117. {
  118. //trzeci ciąg znaków składający się z 2 lub 3 liter -> dozwolone tylko litery
  119. return false;
  120. }
  121. }
  122.  
  123. // vParts.size() > 1
  124. int nPostMalpaLen = strInput.length();
  125. int nLastPartLen = strLast.length();
  126.  
  127. int nPreLastLen = nPostMalpaLen - nLastPartLen - 1;
  128.  
  129. if (nPreLastLen < 1 || nPreLastLen > 20)
  130. {
  131. // drugi ciąg znaków musi się składać z n znaków, gdzie n zawiera się w przedziale [1..20]
  132. return false;
  133. }
  134. }
  135.  
  136. // cos jeszcze ???
  137.  
  138. return true;
  139. }
  140.  
  141. bool ValidateAddress(const string& strInput)
  142. {
  143. vector<string> vParts;
  144. split(strInput, "@", vParts);
  145.  
  146. if (vParts.size() != 2)
  147. {
  148. // brak małpy lub za duzo małp
  149. return false;
  150. }
  151.  
  152. string strPreMalpa = vParts[0];
  153. string strPostMalpa = vParts[1];
  154.  
  155. if (strPreMalpa.length() == 0)
  156. {
  157. //małpa na początku
  158. return false;
  159. }
  160.  
  161. if (strPostMalpa.length() == 0)
  162. {
  163. //małpa na końcu
  164. return false;
  165. }
  166.  
  167. if (ValidatePreMalpa(strPreMalpa) == false)
  168. return false;
  169.  
  170. if (ValidatePostMalpa(strPostMalpa) == false)
  171. return false;
  172.  
  173. return true;
  174. }
  175.  
  176. int main(int argc, char * argv[])
  177. {
  178. PrepareAllowed();
  179.  
  180. int nCount;
  181. cin >> nCount;
  182. string strLine;
  183. getline(cin, strLine);
  184.  
  185. while (nCount--)
  186. {
  187. getline(cin, strLine);
  188.  
  189. if (ValidateAddress(strLine) == true)
  190. cout << "Tak\n";
  191. else
  192. cout << "Nie\n";
  193. }
  194.  
  195. return 0;
  196. }
  197.  
Success #stdin #stdout 0s 4396KB
stdin
5
mat h@edu.pl
algorytm@edu.pl
algoliga@algoliga.edu.pl
1234@123.PL
1234@123..pl
stdout
Nie
Tak
Tak
Tak
Nie