新手菜鸟关于C语言查找的问题
3+2+0新手菜鸟关于C语言查找的问题新手菜鸟关于C语言查找的问题新手菜鸟关于C语言查找的问题新手菜鸟关于C语言查找的问题新手菜鸟关于C语言查找的问题新手菜鸟关于C语言查找的问题[此贴子已经被作者于2016-2-22 15:55编辑过]
#include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_EQ(s1, s2) (0 == strcmp(s1, s2)) #define START1 "0xfe" #define START2 "0xff" #define END1 "0xff" #define END2 "0xff" #define SEPARATOR "," void parse(char *source) { char *p = strtok(source, SEPARATOR); long int buffer[14]; int posToFill = 0; enum { WAITING_FOR_START1, WAITING_FOR_START2, /* 0xfe 0xff */ WAITING_FOR_END1, WAITING_FOR_END2 /* 0xff 0xff */ } state = WAITING_FOR_START1; while (p) { char *s = p; switch (state) { case WAITING_FOR_START1: if (STR_EQ(s, START1)) { state = WAITING_FOR_START2; } break; case WAITING_FOR_START2: if (STR_EQ(s, START2)) { state = WAITING_FOR_END1; } break; case WAITING_FOR_END1: if (STR_EQ(s, END1)) { state = WAITING_FOR_END2; } else { buffer[posToFill++] = strtol(s, NULL, 10); } break; case WAITING_FOR_END2: if (STR_EQ(s, END2)) { int i; for (i = 0; i < posToFill; ++i) { printf("%d ", buffer[i]); } printf("\n"); posToFill = 0; state = WAITING_FOR_START1; } break; } p = strtok(NULL, SEPARATOR); } } int main(int argc, char *argv[]) { char s[] = "0xfe,0xff,14,25,3,35,3,8,0,8,0,9,0xff,0xff," "0xfe,0xff,44,26,4,31,3,8,10,18,10,9,0xff,0xff," "0xfe,0xff,13,24,34,35,2,28,20,8,0,29,0xff,0xff," "0xfe,0xff,14,65,3,65,3,8,20,8,0,29,0xff,0xff"; parse(s); return 0; }
H:\>gcc -ansi -pedantic -Wall foo.c && a.exe 14 25 3 35 3 8 0 8 0 9 44 26 4 31 3 8 10 18 10 9 13 24 34 35 2 28 20 8 0 29 14 65 3 65 3 8 20 8 0 29
[此贴子已经被作者于2016-2-19 11:42编辑过]