Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <boost/regex.hpp>
  4. #include <locale>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int alphaNumericCheck(string a);
  10. int numberOfDigit(string str, int max);
  11. int mbSplitChar2Vector(const char* str, vector<string>& ret);
  12.  
  13. //
  14. //g++ -o Hello HelloWorld.cpp -lboost_regex && ./Hello
  15. //
  16. int main(){
  17.  
  18. setlocale( LC_CTYPE, "ja_JP.UTF-8");
  19. //setlocale( LC_CTYPE, "ja_JP.EUC-JP");
  20.  
  21. cout << "-------------半角英数字のみチェック---------------" << endl;
  22. //半角英数字のみチェック
  23. alphaNumericCheck("aaaa");
  24. alphaNumericCheck("aaaa1");
  25. alphaNumericCheck("aaaaAA");
  26. alphaNumericCheck("aaaa!");
  27. alphaNumericCheck("aaaaア");
  28.  
  29. cout << "-------------桁数チェック(文字列、最大桁数)---------------" << endl;
  30. //桁数チェック(文字列、最大桁数)
  31. numberOfDigit("12345", 5);
  32. numberOfDigit("1 2 3 4 5 ", 11);
  33. numberOfDigit("abc",2);
  34. numberOfDigit("あいう", 9);
  35.  
  36. cout << "-------------字列を1文字単位でVectorに格納する---------------" << endl;
  37. vector<string> ret;
  38. //半角カナ存在チェック
  39. const string aa1="def";
  40. const char* aaa1 = aa1.c_str();
  41. mbSplitChar2Vector(aaa1, ret);
  42. cout << "-------------------" << endl;
  43. const string aa2="ア";
  44. const char* aaa2 = aa2.c_str();
  45. mbSplitChar2Vector(aaa2, ret);
  46. cout << "-------------------" << endl;
  47. const string aa3="アブ";
  48. const char* aaa3 = aa3.c_str();
  49. mbSplitChar2Vector(aaa3, ret);
  50. cout << "-------------------" << endl;
  51. const string aa4="ア";
  52. const char* aaa4 = aa4.c_str();
  53. mbSplitChar2Vector(aaa4, ret);
  54. cout << "-------------------" << endl;
  55.  
  56. for (int i=0;i<ret.size();i++){
  57. cout << "#" << ret[i] << "#" << endl;
  58. }
  59.  
  60. cout << "-------------半角カナ存在チェック---------------" << endl;
  61. //半角カナ存在チェック
  62.  
  63.  
  64. return 0;
  65. }
  66.  
  67. //----------------------------------------------------
  68. //文字列を1文字単位でVectorに格納する。
  69. //(半角カナでも全角文字でも半角英数でもmblenで判定して1文字ちぎる)
  70. //----------------------------------------------------
  71. int mbSplitChar2Vector(const char* str, vector<string>& ret){
  72.  
  73. char *rep;
  74. int i=0;
  75.  
  76. while(str[i] != '\0'){
  77. char *rep;
  78. int len = mblen(&str[i],MB_CUR_MAX);
  79. cout << "MBLEN=" << (int)len << endl;
  80.  
  81. rep = (char *)malloc(sizeof(char) * strlen(str) );
  82.  
  83. /*ワークを初期化*/
  84. for(int j=0 ; j<len + 1 ; j++){
  85. rep[j]='\0';
  86. }
  87.  
  88. /*文字数を指定してコピー*/
  89. strncpy(rep,str + i,len);
  90. cout << "MOJI=" << rep << endl;
  91. ret.push_back(rep);
  92.  
  93. free(rep);
  94. i+=len;
  95. }
  96. }
  97. //----------------------------------------------------
  98. //半角英数字のみチェック
  99. //----------------------------------------------------
  100. int alphaNumericCheck(string str){
  101. //半角英数字のみ-----------------------------Start
  102. boost::regex reg("^[0-9A-Za-z]+$");
  103. boost::smatch mat;
  104.  
  105. if( boost::regex_search(str,mat,reg)){
  106. cout << "全て半角英数字" << endl;
  107. }
  108. else{
  109. cout << "半角英数字出ないものあり!" << endl;
  110. }
  111. //半角英数字のみ-----------------------------End
  112. }
  113.  
  114. //----------------------------------------------------
  115. //桁数チェック
  116. //----------------------------------------------------
  117. int numberOfDigit(string str,int max){
  118.  
  119. cout << "文字 : " << str << " | サイズ : " << str.size() << endl;
  120. cout << "文字 : " << str << " | サイズ : " << str.length() << endl;
  121. if(max < str.size()){
  122. cout << "桁数オーバー" << endl;
  123. }
  124. else{
  125. cout << "桁数OK!" << endl;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement