64,267
社区成员
发帖
与我相关
我的任务
分享
#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 <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <utility>
using namespace std;
int main(int argc,char **argv)
{
string str="236,7526,1.9|308,,3|1133,1133,1.7|4151,,3|2322,2322,1.8|4389,5779,3|1123,1123,2|5779,,3|5942,233,0.9|8116,,3";
//string separators="|";
string word;
int a,b;
float c;
int count_1=0;//a!=b的个数
int count_2=0;//a==b,c>1.5的个数
vector< pair<int,int> > ivec1; //存放a!=b的组
vector< pair<int,float> > ivec2; //存放a=b且c>1.5的组
string::iterator it1=str.begin();
string::size_type start_pos=0,end_pos=0;
while(it1!=str.end())
{
//找到一组数据
start_pos=str.find_first_not_of('|',end_pos);
end_pos=str.find_first_of('|',start_pos);
if (end_pos==string::npos)
{
end_pos=str.size();
}
word.assign(str.begin()+start_pos,str.begin()+end_pos);
//找出一组数据里面的a,b,c的值
string::iterator it2=word.begin();
string::size_type start_digit=0,end_digit=0,start;
string temp;
for (int i=0;i<3;i++)
{
start=end_digit;//记录上一次的结束位置
start_digit=word.find_first_not_of(',',end_digit);
end_digit=word.find_first_of(',',start_digit);
if (end_digit==string::npos)
{
end_digit=word.size();
}
switch(i)
{
case 0:
a=atoi(temp.assign(word.begin()+start_digit,word.begin()+end_digit).c_str());
break;
case 1:
if (start_digit-start>1)
{
b=0;
}
else
{
b=atoi(temp.assign(word.begin()+start_digit,word.begin()+end_digit).c_str());
}
break;
case 2:
c=atof(temp.assign(word.begin()+start_digit,word.begin()+end_digit).c_str());
break;
}
}
//比较a,b,c的值
if (a!=b)
{
++count_1;
pair<int,int> isper(a,b);
ivec1.push_back(isper);
}
else if (c>1.5)
{
++count_2;
pair<int,float> isper(a,c);
ivec2.push_back(isper);
}
//迭代器赋值
it1=str.begin()+end_pos;
}
//得到按c大小排序的数组
int *a_array=new int[count_2];
float *c_array=new float[count_2];
for (int i=0;i<count_2;i++)
{
int pos;
for (int j=0;j<ivec2.size();j++)
{
float max=0.0;
if (ivec2[j].second>max)
{
max=ivec2[j].second;
pos=j;
}
}
*(a_array+i)=ivec2[pos].first;
*(c_array+i)=ivec2[pos].second;
ivec2.erase(ivec2.begin()+pos);
}
//输出a!=b的组
vector< pair<int,int> >::iterator it3=ivec1.begin();
while(it3!=ivec1.end())
{
if (it3!=ivec1.begin())
{
cout<<"|";
}
cout<<it3->first<<","<<it3->second;
++it3;
}
cout<<endl;
//输出a==b,c>1.5的组
cout<<"a数组:";
for ( i=0;i<count_2;i++)
{
cout<<*(a_array+i)<<" ";
}
cout<<endl;
cout<<"c数组:";
for ( i=0;i<count_2;i++)
{
cout<<*(c_array+i)<<" ";
}
cout<<endl;
return 0;
}