找出正确手机号码

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

 

posted @ 2019-04-24 20:40  治愈系的江予夺  阅读(622)  评论(0编辑  收藏  举报