juinda

NameSpaceTraining

Jun 14th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. //--------------
  2. //----user.h----
  3. //--------------
  4.  
  5. #ifndef USERNAME_H
  6. #define USERNAME_H
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. namespace authenticate
  14. {
  15. void inputUserName();
  16. string getUserName();
  17. }
  18. #endif
  19.  
  20. //--------------
  21. //---user.cpp---
  22. //--------------
  23.  
  24. #include <iostream>
  25. #include <string>
  26. using namespace std;
  27.  
  28. namespace
  29. {
  30. string username;
  31.  
  32. //Intent:判斷帳號是否符合規則
  33. //Pre:無
  34. //Post:如果符合則回傳true, 否則回傳false
  35. bool isValid()
  36. {
  37.  
  38. //判斷帳號長度是否恰好為8個字且皆為letter
  39. if (username.length() == 8)
  40. {
  41. for (int i = 0; i < username.length(); i++)
  42. {
  43. if (!isalpha(username[i])) return false;
  44. }
  45. return true;
  46. }
  47. return false;
  48.  
  49. }
  50. }
  51.  
  52. namespace authenticate
  53. {
  54.  
  55. //Intent:供使用者輸入帳號並且將使用者的帳號儲存起來
  56. //Pre:無
  57. //Post:無
  58. void inputUserName()
  59. {
  60.  
  61. //供使用者輸入帳號
  62. do
  63. {
  64. cout << "Enter your username " <<
  65. "(8 letters only)" <<
  66. endl;
  67. cin >> username;
  68. } while (!isValid());
  69.  
  70. }
  71.  
  72. //Intent:得到使用者的帳號
  73. //Pre:無
  74. //Post:回傳使用者的帳號
  75. string getUserName()
  76. {
  77. return username;
  78. }
  79. }
  80.  
  81. //------------------
  82. //----password.h----
  83. //------------------
  84.  
  85. #ifndef PASSWORD_H
  86. #define PASSWORD_H
  87.  
  88. #include <iostream>
  89. #include <string>
  90.  
  91. using namespace std;
  92.  
  93. namespace authenticate
  94. {
  95. void inputPassword();
  96. string getPassword();
  97. }
  98. #endif
  99.  
  100. //------------------
  101. //---password.cpp---
  102. //------------------
  103.  
  104. #include <iostream>
  105. #include <string>
  106. #include <cctype>
  107. using namespace std;
  108.  
  109. namespace
  110. {
  111. string password;
  112.  
  113. //Intent:判斷密碼是否符合規則
  114. //Pre:無
  115. //Post:如果符合則回傳true, 否則回傳false
  116. bool isValid()
  117. {
  118.  
  119. //判斷密碼長度是否8個字以上且至少包含一個non-letter的字
  120. if (password.length() >= 8)
  121. {
  122. for (int i = 0; i < password.length(); i++)
  123. {
  124. if (!isalpha(password[i])) return true;
  125. }
  126. }
  127. return false;
  128.  
  129. }
  130. }
  131.  
  132. namespace authenticate
  133. {
  134.  
  135. //Intent:供使用者輸入密碼並且將使用者的密碼儲存起來
  136. //Pre:無
  137. //Post:無
  138. void inputPassword()
  139. {
  140.  
  141. //供使用者輸入密碼
  142. do
  143. {
  144. cout << "Enter your password (at least 8" <<
  145. " characters and at least one non-letter)" <<
  146. endl;
  147. cin >> password;
  148. } while (!isValid());
  149.  
  150. }
  151.  
  152. //Intent:得到使用者的密碼
  153. //Pre:無
  154. //Post:回傳使用者的密碼
  155. string getPassword()
  156. {
  157. return password;
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment