逗号判断--求助
21,0x10,28,0,1,1,1,,,,,,,,,17,,,,,,,6,0xE70C,,,,,,,,,,,,,,,,,8,8,8,8,8,8,8,,,,,,,,,,交差点名称,交差点名称,
//上面的是题目,要求以逗号为标示,将字符存到结构体中,逗号间有字符的直接存,没有字符的补零后再存。
这只是其中的一行,下面还有很多行,
[解决办法]
。。。楼主,你题目太笼统了,比如结构体是什么样子的。。。等等
[解决办法]
仅供参考
#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 = control1; const unsigned char *ctrl2 = 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=string; } else str=nextoken; string=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=str; flag=0; } else if (map2[*str >> 3] & (1 << (*str & 7))) { str++; string=str; } else { string=str; str++; flag=1; } } } nextoken=str; if (string==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>#include <stdlib.h>#include <string.h>void convert(const char *input){ int has = 0; while (*input != '\0') { if (*input != ',') { has = 1; } else { if (!has) { printf("0"); } has = 0; } printf("%c", *input); ++ input; }}int main(){ char input[100]; while (fgets(input, sizeof(input), stdin) != NULL) { convert(input); } return 0;}21,0x10,28,0,1,1,1,,,,,,,,,17,,,,,,,6,0xE70C,,,,,,,,,,,,,,,,,8,8,8,8,8,8,8,,,,,,,,,21,0x10,28,0,1,1,1,0,0,0,0,0,0,0,0,17,0,0,0,0,0,0,6,0xE70C,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,0,0,0,0,0,0,0,0,
[解决办法]
#include <iostream>#include <vector>using namespace std;// 获取以指定分隔符分隔的字符串所有子字符串// ab,d,,d :会在v中存储搜索到的字符串: "ab", "d", "0", "d"void getCommaSeperateStr(std::string str, char seperateCh, std::vector<string> &v){ int begin = 0; for(int i = 0; i < str.length(); ++i) { if(str[i] == seperateCh) { std::string temp = str.substr(begin, i - begin); if(temp == "") v.push_back("0"); else v.push_back(temp); begin = i + 1; } }}int main(){ vector<string> v; getCommaSeperateStr("21,0x10,28,0,1,1,1,,,,,,,,,17,,,,,,,6,0xE70C,,,,,,,,,,,,,,,,,8,8,8,8,8,8,8,,,,,,,,,,", ',', v); for(int i = 0; i < v.size(); ++i) cout << v[i] << endl;}