比较规则:
(1)从左到右分离单纯的子字符串(全字符)和子数字串(全数字)进行比较。
(2)如果被比较的都是子字符串,则可以调用strcmp比较子字符串大小。
(3)如果被比较的都是子数字串,则根据值比较大小,如果值相等,则子数字串短的一方大(前面的0少)。
(4)如果被比较的一边是子数字串,一边是子字符串,则子字符串大。
写一个int Compare(char *a, char *b)函数,不能用递归,符合上面条件,并用一个测试程序,测试这个函数。
测试例子,B1,B01,B2,B11大小顺序将是B01,B1,B2,B11。
13 个解决方案
#1
有人会吗?
急啊~~~
急啊~~~
#2
不是高手,路过~~
#3
这个很难么?我感觉没什么头绪阿?
555
555
#4
用数组进行存储 然后进行一一比较
配合if语句进行分类
然后进行分类比较
应该就ok了
配合if语句进行分类
然后进行分类比较
应该就ok了
#5
怎样分离字符串与数字?
#6
怎样分离字符串与数字?
------------------------
分离数字是不是要一个一个的比较其ASCII码值
------------------------
分离数字是不是要一个一个的比较其ASCII码值
#7
在VC6.0下写了一个:
/******************************************
*
* Write by: qqtcc
* Date: 2006-11-30
*
*******************************************/
#include <iostream.h>
#include <malloc.h>
#include <string.h>
#define NULL 0;
#define LEN sizeof(struct sstr)
struct sstr
{ int num;
char ss[80];
struct sstr *next;
};
bool isNumber(char *p)
{ if(*p>='0'&& *p<='9'){return true;}
else return false;
}
void initial(struct sstr *head)
{ for(int i=0;i<80;i++)
{ head->ss[i]='\0';
head->next=NULL;
}
}
struct sstr *sparate(char *head)
{ int count=0,i=0;
char tmp;
char *p1;
struct sstr *shead,*sp1;
p1=head;
sp1=shead=(struct sstr *)malloc(LEN);
initial(shead);
while(*p1!='\0')
{
if(isNumber(p1))
{ while (isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
else
{ while (!isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
++p1;
}
return shead;
}
void deleZero(struct sstr *p1,struct sstr *p2)
{ int nsum=0;
for(int i=0;p1->ss[i]=='0';i++) nsum++;
strcpy(p1->ss,(p1->ss)+nsum);
}
int len(struct sstr *p1)
{ int nsum=0;
for(int i=0;p1->ss[i]!='\0';i++) nsum++;
return nsum;
}
int campare(struct sstr *p1,struct sstr *p2)
{ while(!strcmp(p1->ss,p2->ss)&&p1->next&&p1->next)
{ p1=p1->next;
p2=p2->next;
}
if(!p1&&!p2) return 0;
if(!isNumber(p1->ss)||!isNumber(p2->ss)) {return strcmp(p1->ss,p2->ss);}
if(isNumber(p1->ss)&&isNumber(p2->ss))
{ int s_longer=len(p2)-len(p1);
deleZero(p1,p2);
if(len(p1)>len(p2)) {return 1;}
else if(strcmp(p1->ss,p2->ss)==0){return s_longer;}
else return strcmp(p1->ss,p2->ss);
}
return 0;
}
void result(int x)
{ if(x>0) cout<<"str1 is more bigger"<<endl;
if(x<0) cout<<"str2 is more bigger"<<endl;
if(x==0)cout<<"str1 equl str2"<<endl;
}
void main()
{ char str1[80];
char str2[80];
cout<<"input str1:";
cin>>str1;
cout<<"input str2:";
cin>>str2;
cout<<"campare string1:"<<str1<<endl;
cout<<"campare string2:"<<str2<<endl;
char *head1=str1,*head2=str2;
struct sstr *p1,*p2;
p1=sparate(head1);
p2=sparate(head2);
cout<<"result of campare:";
result(campare(p1,p2));
}
/******************************************
*
* Write by: qqtcc
* Date: 2006-11-30
*
*******************************************/
#include <iostream.h>
#include <malloc.h>
#include <string.h>
#define NULL 0;
#define LEN sizeof(struct sstr)
struct sstr
{ int num;
char ss[80];
struct sstr *next;
};
bool isNumber(char *p)
{ if(*p>='0'&& *p<='9'){return true;}
else return false;
}
void initial(struct sstr *head)
{ for(int i=0;i<80;i++)
{ head->ss[i]='\0';
head->next=NULL;
}
}
struct sstr *sparate(char *head)
{ int count=0,i=0;
char tmp;
char *p1;
struct sstr *shead,*sp1;
p1=head;
sp1=shead=(struct sstr *)malloc(LEN);
initial(shead);
while(*p1!='\0')
{
if(isNumber(p1))
{ while (isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
else
{ while (!isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
++p1;
}
return shead;
}
void deleZero(struct sstr *p1,struct sstr *p2)
{ int nsum=0;
for(int i=0;p1->ss[i]=='0';i++) nsum++;
strcpy(p1->ss,(p1->ss)+nsum);
}
int len(struct sstr *p1)
{ int nsum=0;
for(int i=0;p1->ss[i]!='\0';i++) nsum++;
return nsum;
}
int campare(struct sstr *p1,struct sstr *p2)
{ while(!strcmp(p1->ss,p2->ss)&&p1->next&&p1->next)
{ p1=p1->next;
p2=p2->next;
}
if(!p1&&!p2) return 0;
if(!isNumber(p1->ss)||!isNumber(p2->ss)) {return strcmp(p1->ss,p2->ss);}
if(isNumber(p1->ss)&&isNumber(p2->ss))
{ int s_longer=len(p2)-len(p1);
deleZero(p1,p2);
if(len(p1)>len(p2)) {return 1;}
else if(strcmp(p1->ss,p2->ss)==0){return s_longer;}
else return strcmp(p1->ss,p2->ss);
}
return 0;
}
void result(int x)
{ if(x>0) cout<<"str1 is more bigger"<<endl;
if(x<0) cout<<"str2 is more bigger"<<endl;
if(x==0)cout<<"str1 equl str2"<<endl;
}
void main()
{ char str1[80];
char str2[80];
cout<<"input str1:";
cin>>str1;
cout<<"input str2:";
cin>>str2;
cout<<"campare string1:"<<str1<<endl;
cout<<"campare string2:"<<str2<<endl;
char *head1=str1,*head2=str2;
struct sstr *p1,*p2;
p1=sparate(head1);
p2=sparate(head2);
cout<<"result of campare:";
result(campare(p1,p2));
}
#8
对不起最后忘记释放链表,在结尾释放一下即可.
#9
实在看不下去了,qqtcc(),你的编码风格我太不习惯了!
#10
你的字符串是不是有规律呀,比如字符一定是在数字前面--要不是分离字符和数字的规则是什么?
用C++的string会实现简单些。
用C++的string会实现简单些。
#11
把我能用的差不多都用了,供你以后慢慢提高的时候看吧。
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using namespace boost;
int main(void)
{
typedef tuple<string, int, int> KEY;
typedef vector<KEY> KEYS;
typedef multimap<KEYS, string> RESULT;
string ss[] = {"b01", "b1", "b11", "b2", "a1", "1a"};
RESULT r;
for (string * s = ss; s != ss + sizeof(ss) / sizeof(ss[0]); ++s)
{
KEYS temp;
for (sregex_iterator iter(s->begin(), s->end(), regex("(\\d+)|([[:alpha:]]+)")); iter != sregex_iterator(); ++iter)
{
temp.push_back(make_tuple(iter->str(2), iter->length(1) ? lexical_cast<int>(iter->str(1)) : 0, -iter->length(1)));
}
r.insert(make_pair(temp, *s));
}
for (RESULT::iterator iter = r.begin(); iter != r.end(); ++iter)
{
cout << iter->second << " ";
}
system("pause");
return 0;
}
输出1a a1 b01 b1 b2 b11 请按任意键继续. . .
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using namespace boost;
int main(void)
{
typedef tuple<string, int, int> KEY;
typedef vector<KEY> KEYS;
typedef multimap<KEYS, string> RESULT;
string ss[] = {"b01", "b1", "b11", "b2", "a1", "1a"};
RESULT r;
for (string * s = ss; s != ss + sizeof(ss) / sizeof(ss[0]); ++s)
{
KEYS temp;
for (sregex_iterator iter(s->begin(), s->end(), regex("(\\d+)|([[:alpha:]]+)")); iter != sregex_iterator(); ++iter)
{
temp.push_back(make_tuple(iter->str(2), iter->length(1) ? lexical_cast<int>(iter->str(1)) : 0, -iter->length(1)));
}
r.insert(make_pair(temp, *s));
}
for (RESULT::iterator iter = r.begin(); iter != r.end(); ++iter)
{
cout << iter->second << " ";
}
system("pause");
return 0;
}
输出1a a1 b01 b1 b2 b11 请按任意键继续. . .
#12
#include <iostream>
#include <stdio.h>
using namespace std;
int Compare(char *a, char *b)
{
string sa,sb;
int ia=0,ib=0;
int counta=0,countb=0;
for (char * p=a;*p!='\0';p++)
{
if(isalpha(*p))
sa+=*p;
else
if (isdigit(*p))
{
ia=ia*10+*p-'0';
counta++;
}
}
for (char * p=b;*p!='\0';p++)
{
if(isalpha(*p))
sb+=*p;
else
if (isdigit(*p))
{
ib=ib*10+*p-'0';
countb++;
}
}
if (sa<sb)
return -1;
else if (sa>sb)
return 1;
else if (ia<ib)
return -1;
else if (ia>ib)
return 1;
else if (counta<countb)
return 1;
else if (counta>countb)
return -1;
else
return 0;
}
int main()
{
char s1[]="B1";
char s2[]="B01";
char s3[]="B2";
char s4[]="B11";
cout<<Compare(s1,s2)<<endl;
cout<<Compare(s1,s3)<<endl;
cout<<Compare(s3,s4)<<endl;
cout<<Compare(s4,s4)<<endl;
return 0;
}
#include <stdio.h>
using namespace std;
int Compare(char *a, char *b)
{
string sa,sb;
int ia=0,ib=0;
int counta=0,countb=0;
for (char * p=a;*p!='\0';p++)
{
if(isalpha(*p))
sa+=*p;
else
if (isdigit(*p))
{
ia=ia*10+*p-'0';
counta++;
}
}
for (char * p=b;*p!='\0';p++)
{
if(isalpha(*p))
sb+=*p;
else
if (isdigit(*p))
{
ib=ib*10+*p-'0';
countb++;
}
}
if (sa<sb)
return -1;
else if (sa>sb)
return 1;
else if (ia<ib)
return -1;
else if (ia>ib)
return 1;
else if (counta<countb)
return 1;
else if (counta>countb)
return -1;
else
return 0;
}
int main()
{
char s1[]="B1";
char s2[]="B01";
char s3[]="B2";
char s4[]="B11";
cout<<Compare(s1,s2)<<endl;
cout<<Compare(s1,s3)<<endl;
cout<<Compare(s3,s4)<<endl;
cout<<Compare(s4,s4)<<endl;
return 0;
}
#13
sorry 本来都是有缩进的,发到上面的时候缩进没有了,呵呵,对不起!有兴趣可以重发一下.
#1
有人会吗?
急啊~~~
急啊~~~
#2
不是高手,路过~~
#3
这个很难么?我感觉没什么头绪阿?
555
555
#4
用数组进行存储 然后进行一一比较
配合if语句进行分类
然后进行分类比较
应该就ok了
配合if语句进行分类
然后进行分类比较
应该就ok了
#5
怎样分离字符串与数字?
#6
怎样分离字符串与数字?
------------------------
分离数字是不是要一个一个的比较其ASCII码值
------------------------
分离数字是不是要一个一个的比较其ASCII码值
#7
在VC6.0下写了一个:
/******************************************
*
* Write by: qqtcc
* Date: 2006-11-30
*
*******************************************/
#include <iostream.h>
#include <malloc.h>
#include <string.h>
#define NULL 0;
#define LEN sizeof(struct sstr)
struct sstr
{ int num;
char ss[80];
struct sstr *next;
};
bool isNumber(char *p)
{ if(*p>='0'&& *p<='9'){return true;}
else return false;
}
void initial(struct sstr *head)
{ for(int i=0;i<80;i++)
{ head->ss[i]='\0';
head->next=NULL;
}
}
struct sstr *sparate(char *head)
{ int count=0,i=0;
char tmp;
char *p1;
struct sstr *shead,*sp1;
p1=head;
sp1=shead=(struct sstr *)malloc(LEN);
initial(shead);
while(*p1!='\0')
{
if(isNumber(p1))
{ while (isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
else
{ while (!isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
++p1;
}
return shead;
}
void deleZero(struct sstr *p1,struct sstr *p2)
{ int nsum=0;
for(int i=0;p1->ss[i]=='0';i++) nsum++;
strcpy(p1->ss,(p1->ss)+nsum);
}
int len(struct sstr *p1)
{ int nsum=0;
for(int i=0;p1->ss[i]!='\0';i++) nsum++;
return nsum;
}
int campare(struct sstr *p1,struct sstr *p2)
{ while(!strcmp(p1->ss,p2->ss)&&p1->next&&p1->next)
{ p1=p1->next;
p2=p2->next;
}
if(!p1&&!p2) return 0;
if(!isNumber(p1->ss)||!isNumber(p2->ss)) {return strcmp(p1->ss,p2->ss);}
if(isNumber(p1->ss)&&isNumber(p2->ss))
{ int s_longer=len(p2)-len(p1);
deleZero(p1,p2);
if(len(p1)>len(p2)) {return 1;}
else if(strcmp(p1->ss,p2->ss)==0){return s_longer;}
else return strcmp(p1->ss,p2->ss);
}
return 0;
}
void result(int x)
{ if(x>0) cout<<"str1 is more bigger"<<endl;
if(x<0) cout<<"str2 is more bigger"<<endl;
if(x==0)cout<<"str1 equl str2"<<endl;
}
void main()
{ char str1[80];
char str2[80];
cout<<"input str1:";
cin>>str1;
cout<<"input str2:";
cin>>str2;
cout<<"campare string1:"<<str1<<endl;
cout<<"campare string2:"<<str2<<endl;
char *head1=str1,*head2=str2;
struct sstr *p1,*p2;
p1=sparate(head1);
p2=sparate(head2);
cout<<"result of campare:";
result(campare(p1,p2));
}
/******************************************
*
* Write by: qqtcc
* Date: 2006-11-30
*
*******************************************/
#include <iostream.h>
#include <malloc.h>
#include <string.h>
#define NULL 0;
#define LEN sizeof(struct sstr)
struct sstr
{ int num;
char ss[80];
struct sstr *next;
};
bool isNumber(char *p)
{ if(*p>='0'&& *p<='9'){return true;}
else return false;
}
void initial(struct sstr *head)
{ for(int i=0;i<80;i++)
{ head->ss[i]='\0';
head->next=NULL;
}
}
struct sstr *sparate(char *head)
{ int count=0,i=0;
char tmp;
char *p1;
struct sstr *shead,*sp1;
p1=head;
sp1=shead=(struct sstr *)malloc(LEN);
initial(shead);
while(*p1!='\0')
{
if(isNumber(p1))
{ while (isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
else
{ while (!isNumber(p1))
{ tmp=*p1;
sp1->ss[i++]=tmp;
++p1;
}
--p1;
sp1->next=(struct sstr *)malloc(LEN);
initial(sp1->next);
sp1=sp1->next;i=0;
}
++p1;
}
return shead;
}
void deleZero(struct sstr *p1,struct sstr *p2)
{ int nsum=0;
for(int i=0;p1->ss[i]=='0';i++) nsum++;
strcpy(p1->ss,(p1->ss)+nsum);
}
int len(struct sstr *p1)
{ int nsum=0;
for(int i=0;p1->ss[i]!='\0';i++) nsum++;
return nsum;
}
int campare(struct sstr *p1,struct sstr *p2)
{ while(!strcmp(p1->ss,p2->ss)&&p1->next&&p1->next)
{ p1=p1->next;
p2=p2->next;
}
if(!p1&&!p2) return 0;
if(!isNumber(p1->ss)||!isNumber(p2->ss)) {return strcmp(p1->ss,p2->ss);}
if(isNumber(p1->ss)&&isNumber(p2->ss))
{ int s_longer=len(p2)-len(p1);
deleZero(p1,p2);
if(len(p1)>len(p2)) {return 1;}
else if(strcmp(p1->ss,p2->ss)==0){return s_longer;}
else return strcmp(p1->ss,p2->ss);
}
return 0;
}
void result(int x)
{ if(x>0) cout<<"str1 is more bigger"<<endl;
if(x<0) cout<<"str2 is more bigger"<<endl;
if(x==0)cout<<"str1 equl str2"<<endl;
}
void main()
{ char str1[80];
char str2[80];
cout<<"input str1:";
cin>>str1;
cout<<"input str2:";
cin>>str2;
cout<<"campare string1:"<<str1<<endl;
cout<<"campare string2:"<<str2<<endl;
char *head1=str1,*head2=str2;
struct sstr *p1,*p2;
p1=sparate(head1);
p2=sparate(head2);
cout<<"result of campare:";
result(campare(p1,p2));
}
#8
对不起最后忘记释放链表,在结尾释放一下即可.
#9
实在看不下去了,qqtcc(),你的编码风格我太不习惯了!
#10
你的字符串是不是有规律呀,比如字符一定是在数字前面--要不是分离字符和数字的规则是什么?
用C++的string会实现简单些。
用C++的string会实现简单些。
#11
把我能用的差不多都用了,供你以后慢慢提高的时候看吧。
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using namespace boost;
int main(void)
{
typedef tuple<string, int, int> KEY;
typedef vector<KEY> KEYS;
typedef multimap<KEYS, string> RESULT;
string ss[] = {"b01", "b1", "b11", "b2", "a1", "1a"};
RESULT r;
for (string * s = ss; s != ss + sizeof(ss) / sizeof(ss[0]); ++s)
{
KEYS temp;
for (sregex_iterator iter(s->begin(), s->end(), regex("(\\d+)|([[:alpha:]]+)")); iter != sregex_iterator(); ++iter)
{
temp.push_back(make_tuple(iter->str(2), iter->length(1) ? lexical_cast<int>(iter->str(1)) : 0, -iter->length(1)));
}
r.insert(make_pair(temp, *s));
}
for (RESULT::iterator iter = r.begin(); iter != r.end(); ++iter)
{
cout << iter->second << " ";
}
system("pause");
return 0;
}
输出1a a1 b01 b1 b2 b11 请按任意键继续. . .
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using namespace boost;
int main(void)
{
typedef tuple<string, int, int> KEY;
typedef vector<KEY> KEYS;
typedef multimap<KEYS, string> RESULT;
string ss[] = {"b01", "b1", "b11", "b2", "a1", "1a"};
RESULT r;
for (string * s = ss; s != ss + sizeof(ss) / sizeof(ss[0]); ++s)
{
KEYS temp;
for (sregex_iterator iter(s->begin(), s->end(), regex("(\\d+)|([[:alpha:]]+)")); iter != sregex_iterator(); ++iter)
{
temp.push_back(make_tuple(iter->str(2), iter->length(1) ? lexical_cast<int>(iter->str(1)) : 0, -iter->length(1)));
}
r.insert(make_pair(temp, *s));
}
for (RESULT::iterator iter = r.begin(); iter != r.end(); ++iter)
{
cout << iter->second << " ";
}
system("pause");
return 0;
}
输出1a a1 b01 b1 b2 b11 请按任意键继续. . .
#12
#include <iostream>
#include <stdio.h>
using namespace std;
int Compare(char *a, char *b)
{
string sa,sb;
int ia=0,ib=0;
int counta=0,countb=0;
for (char * p=a;*p!='\0';p++)
{
if(isalpha(*p))
sa+=*p;
else
if (isdigit(*p))
{
ia=ia*10+*p-'0';
counta++;
}
}
for (char * p=b;*p!='\0';p++)
{
if(isalpha(*p))
sb+=*p;
else
if (isdigit(*p))
{
ib=ib*10+*p-'0';
countb++;
}
}
if (sa<sb)
return -1;
else if (sa>sb)
return 1;
else if (ia<ib)
return -1;
else if (ia>ib)
return 1;
else if (counta<countb)
return 1;
else if (counta>countb)
return -1;
else
return 0;
}
int main()
{
char s1[]="B1";
char s2[]="B01";
char s3[]="B2";
char s4[]="B11";
cout<<Compare(s1,s2)<<endl;
cout<<Compare(s1,s3)<<endl;
cout<<Compare(s3,s4)<<endl;
cout<<Compare(s4,s4)<<endl;
return 0;
}
#include <stdio.h>
using namespace std;
int Compare(char *a, char *b)
{
string sa,sb;
int ia=0,ib=0;
int counta=0,countb=0;
for (char * p=a;*p!='\0';p++)
{
if(isalpha(*p))
sa+=*p;
else
if (isdigit(*p))
{
ia=ia*10+*p-'0';
counta++;
}
}
for (char * p=b;*p!='\0';p++)
{
if(isalpha(*p))
sb+=*p;
else
if (isdigit(*p))
{
ib=ib*10+*p-'0';
countb++;
}
}
if (sa<sb)
return -1;
else if (sa>sb)
return 1;
else if (ia<ib)
return -1;
else if (ia>ib)
return 1;
else if (counta<countb)
return 1;
else if (counta>countb)
return -1;
else
return 0;
}
int main()
{
char s1[]="B1";
char s2[]="B01";
char s3[]="B2";
char s4[]="B11";
cout<<Compare(s1,s2)<<endl;
cout<<Compare(s1,s3)<<endl;
cout<<Compare(s3,s4)<<endl;
cout<<Compare(s4,s4)<<endl;
return 0;
}
#13
sorry 本来都是有缩进的,发到上面的时候缩进没有了,呵呵,对不起!有兴趣可以重发一下.