请高手们帮帮忙,着急!!
字串扩展:
short expand_str(char *str,char **phead,short count)
说明: str 需要扩展的串,ex: abc[0-9], [a-z]abc[0-9], a[a-z]b[A-z][0-5]....
phead 返回扩展后的串的头指针(动态分配)
count 扩展后的数目
注: a. 范围在串中的格式有以下几种[0-9],[a-z],[A-Z],[0-z]
b. 扩展后的串最大长度为32
c. 也可改变函数的入参及返回值,只要实现功能就行
不慎感激!!!
[解决办法]
写了个比较简单的。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 32
int fcnt;
char astr[MAX + 1], st[MAX], ed[MAX];
int id[MAX];
void f(char **head, int lev, int maxlev){
if(lev == maxlev){
strcpy(head[fcnt], astr);
++fcnt;
return;
}
char i;
for(i = st[lev]; i <= ed[lev]; ++i){
astr[id[lev]] = i;
f(head, lev + 1, maxlev);
}
}
short expand_str(char *str,char ***phead,short *count){
int nid = 0, len = 0, cnt = 1, i;
char **head;
char *p = str;
for(; *p; ++len){
if(*p == '['){
st[nid] = *(p + 1);
ed[nid] = *(p + 3);
cnt *= (ed[nid] - st[nid] + 1);
id[nid++] = len;
astr[len] = 0;
p += 5;
}else{
astr[len] = *p++;
}
}
astr[len] = 0;
head = (char**)malloc(sizeof(char*) * cnt);
for(i = 0; i < cnt; ++i) head[i] = (char*)malloc(len + 1);
fcnt = 0;
f(head, 0, nid);
*phead = head;
*count = cnt;
return 0;
}
int main(){
char **head;
short count, i;
char str[100];
while(scanf("%s", str) == 1){
expand_str(str, &head, &count);
printf("count = %d\n", count);
for(i = 0; i < count; ++i) printf("%s\n", head[i]);
for(i = 0; i < count; ++i) free(head[i]);
free(head);
}
return 0;
}
#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
FILE *f;
int i,n,line,st;
int n1,n2,n3;
int b1,b2,b3;
int e1,e2,e3;
int w1,w2,w3;
int v1,v2,v3;
char ln[256];
char fmt[256];
char fmd[6];
char tmp[256];
char *s1,*s2,*s3,*s4;
char L,R,D;
void main(int argc,char *argv[]) {
if (argc<2) {
PMT:
printf("%s filename.ext [ParenthesesChar] [DelimiterChar]\nParenthesesChar is '(' default or '[' or '{'\nDelimiterChar is ',' default",argv[0]);
return;
}
if (argc==2) {
L='(';R=')';D=',';
} else {
switch (argv[2][0]) {
case '(':L='(';R=')';break;
case '[':L='[';R=']';break;
case '{':L='{';R='}';break;
default:goto PMT;
}
D=',';
if (argc>=4) D=argv[3][0];
}
sprintf(fmd,"%%d%c%%d",D);
if ((f=fopen(argv[1],"rt"))==NULL) {
printf("Can not open %s",argv[1]);
return;
}
line=0;
while (1) {
fgets(ln,255,f);
if (feof(f)) break;//
line++;
n=0;st=0;
for (i=0;i<(int)strlen(ln);i++) {
switch (st) {
case 0:if (ln[i]==L) st=1;break;
case 1:if (ln[i]==D) st=2;break;
case 2:if (ln[i]==R) {st=0;n++;} break;
}
}
switch (n) {
case 0:
printf("%s",ln);
break;
case 1:
s1=strchr(ln,L);
s2=strchr(s1,R);
if (s2==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s2++;
v1=sscanf(s1,fmd,&b1,&e1);
if (v1==2) {
w1=1+(int)log10((double)e1);
sprintf(fmt,"%%s%%0%1dd%%s",w1);
for (n1=b1;n1<=e1;n1++) {
printf(fmt,ln,n1,s2);
}
} else if (s1[1]==D) {
b1=s1[0];
e1=s1[2];
for (n1=b1;n1<=e1;n1++) {
printf("%s%c%s",ln,n1,s2);
}
} else {
printf("%s",ln);
}
break;
case 2:
s1=strchr(ln,L);
s2=strchr(s1,R);
if (s2==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s2++;
v1=sscanf(s1,fmd,&b1,&e1);
if (v1==2) {
w1=1+(int)log10((double)e1);
} else {
b1=s1[0];
e1=s1[2];
}
s1=strchr(s2,L);
s3=strchr(s1,R);
if (s3==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s3++;
v2=sscanf(s1,fmd,&b2,&e2);
if (v2==2) {
w2=1+(int)log10((double)e2);
} else {
b2=s1[0];
e2=s1[2];
}
strcpy(fmt,"%s");
if (v1==2) sprintf(tmp,"%%0%1dd",w1);
else strcpy(tmp,"%c");
strcat(fmt,tmp);
strcat(fmt,"%s");
if (v2==2) sprintf(tmp,"%%0%1dd",w2);
else strcpy(tmp,"%c");
strcat(fmt,tmp);
strcat(fmt,"%s");
for (n1=b1;n1<=e1;n1++) {
for (n2=b2;n2<=e2;n2++) {
printf(fmt,ln,n1,s2,n2,s3);
}
}
break;
case 3:
s1=strchr(ln,L);
s2=strchr(s1,R);
if (s2==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s2++;
v1=sscanf(s1,fmd,&b1,&e1);
if (v1==2) {
w1=1+(int)log10((double)e1);
} else {
b1=s1[0];
e1=s1[2];
}
s1=strchr(s2,L);
s3=strchr(s1,R);
if (s3==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s3++;
v2=sscanf(s1,fmd,&b2,&e2);
if (v2==2) {
w2=1+(int)log10((double)e2);
} else {
b2=s1[0];
e2=s1[2];
}
s1=strchr(s3,L);
s4=strchr(s1,R);
if (s4==NULL) {
cprintf("\a\a\a missing %c in line %d",R,line);
fclose(f);
return;
}
s1[0]=0;
s1++;
s4++;
v3=sscanf(s1,fmd,&b3,&e3);
if (v3==2) {
w3=1+(int)log10((double)e3);
} else {
b3=s1[0];
e3=s1[2];
}
sprintf(fmt,"%%s%%0%1dd%%s%%0%1dd%%s%%0%1dd%%s",w1,w2,w3);
strcpy(fmt,"%s");
if (v1==2) sprintf(tmp,"%%0%1dd",w1);
else strcpy(tmp,"%c");
strcat(fmt,tmp);
strcat(fmt,"%s");
if (v2==2) sprintf(tmp,"%%0%1dd",w2);
else strcpy(tmp,"%c");
strcat(fmt,tmp);
strcat(fmt,"%s");
if (v3==2) sprintf(tmp,"%%0%1dd",w3);
else strcpy(tmp,"%c");
strcat(fmt,tmp);
strcat(fmt,"%s");
for (n1=b1;n1<=e1;n1++) {
for (n2=b2;n2<=e2;n2++) {
for (n3=b3;n3<=e3;n3++) {
printf(fmt,ln,n1,s2,n2,s3,n3,s4);
}
}
}
break;
default:
cprintf("\a\a\a > 3 repeat in line %d",line);
fclose(f);
return;
}
}
fclose(f);
}