69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//连续出现时视为中间夹空token
const char *control2 //连续出现时视为中间无空token
)
{
unsigned char *str;
const unsigned char *ctrl1 = (const unsigned char *)control1;
const unsigned char *ctrl2 = (const unsigned char *)control2;
unsigned char map1[32],map2[32];
static char *nextoken;
static char flag=0;
unsigned char c;
int L;
memset(map1,0,32);
memset(map2,0,32);
do {
map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));
} while (*ctrl1++);
do {
map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));
} while (*ctrl2++);
if (string) {
if (control2[0]) {
L=strlen(string);
while (1) {
c=string[L-1];
if (map2[c >> 3] & (1 << (c & 7))) {
L--;
string[L]=0;
} else break;
}
}
if (control1[0]) {
L=strlen(string);
c=string[L-1];
if (map1[c >> 3] & (1 << (c & 7))) {
string[L]=control1[0];
string[L+1]=0;
}
}
str=(unsigned char *)string;
}
else str=(unsigned char *)nextoken;
string=(char *)str;
while (1) {
if (0==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
string++;
str++;
} else {
flag=1;
str++;
}
} else if (1==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=0;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=2;
break;
} else str++;
} else {//2==flag
if (!*str) return NULL;
if (map1[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
flag=0;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
} else {
string=(char *)str;
str++;
flag=1;
}
}
}
nextoken=(char *)str;
if (string==(char *)str) return NULL;
else return string;
}
void main()
{
strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");
strcpy(seps1,",\n");strcpy(seps2," \t");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");
strcpy(seps1,"|");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"");strcpy(seps2,"|");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"|");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,a,,b,,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,,");
strcpy(seps1,",");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
}
//
//[A string of ,,tokens
//
//and some more tokens, ]
//Tokens:
// <A> <string> <of> <> <tokens> <> <and> <some> <more> <tokens> <>
//[1234| LIYI|China | 010 |201110260000|OK]
//Tokens:
// <1234> <LIYI> <China> <010> <201110260000> <OK>
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234> <LIYI> <010> <201110260000> <OK>
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234> <LIYI> <> <010> <201110260000> <OK>
//[a]
//Tokens:
// <a>
//[a,b]
//Tokens:
// <a> <b>
//[a,,b]
//Tokens:
// <a> <> <b>
//[,a]
//Tokens:
// <> <a>
//[a,]
//Tokens:
// <a> <>
//[,a,,b]
//Tokens:
// <> <a> <> <b>
//[,,a,,b,,]
//Tokens:
// <> <> <a> <> <b> <> <>
//[,]
//Tokens:
// <> <>
//[,,]
//Tokens:
// <> <> <>
//[,,,]
//Tokens:
// <> <> <> <>
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
p=s;
while (1) {
k=sscanf(p,"%d%n",&v,&n);
printf("k,v,n=%d,%d,%d\n",k,v,n);
if (1==k) {
p+=n;
} else if (0==k) {
printf("skip char[%c]\n",p[0]);
p++;
} else {//EOF==k
break;
}
}
printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
//有效信息格式为:<013|B2|C3|D4|E5|F6C><A1B2C3D4>
//其中013|B2|C3|D4|E5| 使用 符号分割出来的前两位字母或数字及其组合构成密钥如下
//( 0x01,0xB2,0xC3,0xD4,0xE5,0xF6 ),<A1B2C3D4>截取待用!!!!用C语言该怎么实现,
//有没有大佬帮忙解决一下!!!小弟,跪谢!!!
#include <stdio.h>
unsigned char key[6];
int buf[6];
char tail[11];
char *s="<013|B2|C3|D4|E5|F6C><A1B2C3D4>";
int i;
int main() {
sscanf(s,"<%2X%*c|%2X|%2X|%2X|%2X|%2X%*c%*c%10s",
&buf[0],
&buf[1],
&buf[2],
&buf[3],
&buf[4],
&buf[5],
tail);
for (i=0;i<6;i++) key[i]=(unsigned char)buf[i];
for (i=0;i<6;i++) printf("0x%02X ",key[i]);
printf("\n%s\n",tail);
return 0;
}
//0x01 0xB2 0xC3 0xD4 0xE5 0xF6
//<A1B2C3D4>
//