69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
FILE *f;
char ln[80];
char s1[40];
char s2[40];
char s3[40];
char s4[40];
int i;
int main()
{
f=fopen("in.txt","r");
if (NULL==f)
{
printf("Can not open file in.txt!\n");
return 1;
}
i=0;
while (fgets(ln,80,f))
{
if (3<=sscanf(ln,"f(%39[^,],%39[^)])=%39[^/\n]//%[^\n]",s1,s2,s3,s4))
{
printf("[%s],[%s],[%s],[%s]\n",s1,s2,s3,s4);
}
else
{
printf("in.txt line %d format error:%s",i+1,ln);
}
i++;
}
fclose(f);
return 0;
}
//有字符串
//1_22_333,,4444__55555,_666666
//需要解析为
//1
//22
//333
//_
//4444
//55555
//666666
#include <stdio.h>
char s[]="1_22_333,,4444__55555,_666666";
char c,*p,*p1;
int st;
void main() {
st=0;
p=s;
while (1) {
c=*p;
if (0==c) {
switch (st) {
case 1:printf("_\n"); break;
case 2:printf("%s\n",p1);break;
}
break;//
}
switch (st) {
case 0:
if ('_'==c) { st=0;}
else if (','==c) { st=1;}
else {p1=p; st=2;}
break;
case 1:
if ('_'==c) { st=1;}
else if (','==c) {printf("_\n"); st=1;}
else {p1=p; st=2;}
break;
case 2:
if ('_'==c) {*p=0;printf("%s\n",p1);*p=c;st=0;}
else if (','==c) {*p=0;printf("%s\n",p1);*p=c;st=1;}
else { st=2;}
break;
}
p++;
}
}
//1
//22
//333
//_
//4444
//55555
//666666
//in.txt
//f(a bc,甲)=丙
//f(a,乙)=戊/222 //123
//f(48,丙)=戊
//f(2aaaaaaa ,丙)=戊//aaa
//f(48,张三)=戊
//f(阿3,丁)=乙 //子丑寅卯
//f(48,)=李四 //子鼠丑牛
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *f;
char ln[80];
char s1[40];
char s2[40];
char s34[80];
char *p;
char s3[40];
char s4[40];
int i,k;
int main() {
f=fopen("in.txt","r");
if (NULL==f) {
printf("Can not open file in.txt!\n");
return 1;
}
i=0;
while (fgets(ln,80,f)) {
if (3==sscanf(ln,"f(%39[^,],%39[^)])=%79[^\n]",s1,s2,s34)) {
p=strstr(s34,"//");
if (p) {
k=__min(39,p-s34);
strncpy(s3,s34,k);s3[k]=0;
k=__min(39,strlen(p+2));
strncpy(s4,p+2,k);s4[k]=0;
} else {
strncpy(s3,s34,39);s3[39]=0;
s4[0]=0;
}
printf("[%s],[%s],[%s],[%s]\n",s1,s2,s3,s4);
} else {
printf("in.txt line %d format error:%s",i+1,ln);
}
i++;
}
fclose(f);
return 0;
}
//[a bc],[甲],[丙],[]
//[a],[乙],[戊/222 ],[123]
//[48],[丙],[戊],[]
//[2aaaaaaa ],[丙],[戊],[aaa]
//[48],[张三],[戊],[]
//[阿3],[丁],[乙 ],[子丑寅卯]
//in.txt line 7 format error:f(48,)=李四 //子鼠丑牛
//