经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
找出正确手机号码
来源:cnblogs  作者:治愈系的江予夺  时间:2019/4/25 8:57:54  对本文有异议

题目:已知手机号码是由0-9等组成的11位的字符串,现根据所输入的字符判断其是否为正确的手机号码

要求:1、若输入的字符串开头为151、153、173、180、193任意一组且刚好为11位的数字组成,则输出:%s is ok

2、若输入的字符都是由数字组成且字符个数不足11位,则输出:%s is short   

输入的字符都是由数字组成且字符个数大于11位,则输出:%s is long

 

 

3、若输入的字符包含除0-9以外的字符,则输出:illegal

思路:根据所输入的字符进行一个一个判断,此处可以参考形式语言与自动机理论中的自动机思想,画个自动机状态图,根据一个个输入的字符进行下一步判断,满足条件的进行相应情况输出。

  1. 1 #include<stdio.h>
  2. 2 #include<string.h>
  3. 3 #define x0 0
  4. 4 #define x1 1
  5. 5 #define x2 2
  6. 6 #define x3 3
  7. 7 #define x4 4
  8. 8 #define x5 5
  9. 9 #define short 6
  10. 10 #define long 7
  11. 11 #define ok 8
  12. 12 #define illegal 9
  13. 13 int FA(int state,char input);
  14. 14 int main()
  15. 15 {
  16. 16 char a[100];
  17. 17 int state,i=0;
  18. 18 gets(a);
  19. 19 state=x0;
  20. 20 while(a[i]!='\0')
  21. 21 {
  22. 22 state = FA(state,a[i]);
  23. 23 i++;
  24. 24 }
  25. 25 if(i==11 && state==short )
  26. 26 state = ok;
  27. 27 else if(i>11 && state==short)
  28. 28 state = long;
  29. 29 if(state >= 1 && state <= 6)
  30. 30 printf("%s is short",a);
  31. 31 else if(state == 7)
  32. 32 printf("%s is long",a);
  33. 33 else if(state == 8)
  34. 34 printf("%s is ok",a);
  35. 35 else if(state == 9 || state == 0)
  36. 36 printf("%s is illegal",a);
  37. 37 return 0;
  38. 38 }
  39. 39 int FA(int state, char input)
  40. 40 {
  41. 41 switch(state)
  42. 42 {
  43. 43 case x0:
  44. 44 if(input == '1') state=x1;
  45. 45 else state=illegal;
  46. 46 break;
  47. 47 case x1:
  48. 48 if(input == '5') state=x2;
  49. 49 else if(input == '7') state=x3;
  50. 50 else if(input == '8') state=x4;
  51. 51 else if(input == '9') state=x5;
  52. 52 else state=illegal;
  53. 53 break;
  54. 54 case x2:
  55. 55 if(input == '1' || input == '3') state=short;
  56. 56 else state=illegal;
  57. 57 break;
  58. 58 case x3:
  59. 59 if(input == '3') state=short;
  60. 60 else state=illegal;
  61. 61 break;
  62. 62 case x4:
  63. 63 if(input == '0') state=short;
  64. 64 else state=illegal;
  65. 65 break;
  66. 66 case x5:
  67. 67 if(input == '8') state=short;
  68. 68 else state=illegal;
  69. 69 break;
  70. 70 case short:
  71. 71 if(input <= '9' && input >= '0') state=short;
  72. 72 else state=illegal;
  73. 73 break;
  74. 74 }
  75. 75 return state;
  76. 76 }

 

原文链接:http://www.cnblogs.com/zyxdjyd/p/10742705.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号