Title: it is known that the mobile phone number is an 11 digit string composed of 0-9 and so on. Now, it is judged whether it is the correct mobile phone number according to the entered characters
Requirements: 1. If the input string starts with any group of 151, 153, 173, 180, 193 and consists of just 11 digits, then the output is:% s is ok
2. If the input characters are all composed of numbers and the number of characters is less than 11, the output is:% s is short
If the input characters are all composed of numbers and the number of characters is greater than 11 bits, then the output is:% s is long
3. If the input character contains characters other than 0-9, the output: illegal
Idea: make a judgment according to the characters you input. Here, you can draw a state diagram of the automaton with reference to the automaton thought in formal language and automaton theory. Make the next judgment according to the characters you input, and output the corresponding situation if you meet the conditions.
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 }