87 lines
2.5 KiB
C
87 lines
2.5 KiB
C
|
/*
|
|||
|
Program: sscanf.c
|
|||
|
Function: <EFBFBD>i<EFBFBD><EFBFBD>sscanf()<EFBFBD>Pqsort()<EFBFBD><EFBFBD><EFBFBD>Ϊk
|
|||
|
Note: <EFBFBD>s<EFBFBD>צ۳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>۱бª<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> http://ccckmit.wikidot.com/cp:sscanf
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
|
|||
|
int ascend(const void *a, const void *b); //<2F>t<EFBFBD>Xqsort()<29>ҥΨ<D2A5><CEA8>ơA<C6A1>Ω<EFBFBD><CEA9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǤJ<C7A4>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>j<EFBFBD>p<EFBFBD><70><EFBFBD>Y<EFBFBD>A<EFBFBD>Ω<EFBFBD><CEA9><EFBFBD><EFBFBD>{<7B><><EFBFBD>W<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ĪG
|
|||
|
|
|||
|
int main() {
|
|||
|
char name[20], tel[50], field[20], areaCode[20], code[20];
|
|||
|
int age;
|
|||
|
|
|||
|
sscanf("name:john age:40 tel:082-313530", "%s", name);
|
|||
|
printf("%s\n", name);
|
|||
|
sscanf("name:john age:40 tel:082-313530", "%8s", name);
|
|||
|
printf("%s\n", name);
|
|||
|
sscanf("name:john age:40 tel:082-313530", "%[^:]", name);
|
|||
|
printf("%s\n", name);
|
|||
|
sscanf("name:john age:40 tel:082-313530", "%[^:]:%s", field, name);
|
|||
|
printf("%s %s\n", field, name);
|
|||
|
sscanf("name:john age:40 tel:082-313530", "name:%s age:%d tel:%s", name, &age, tel);
|
|||
|
printf("%s %d %s\n", name, age, tel);
|
|||
|
sscanf("name:john age:40 tel:082-313530", "%*[^:]:%s %*[^:]:%d %*[^:]:%s", name, &age, tel);
|
|||
|
printf("%s %d %s\n", name, age, tel);
|
|||
|
|
|||
|
char protocol[10], site[50], path[50];
|
|||
|
sscanf("http://ccckmit.wikidot.com/cp/list/hello.txt",
|
|||
|
"%[^:]:%*2[/]%[^/]/%[a-zA-Z0-9._/-]",
|
|||
|
protocol, site, path);
|
|||
|
printf("protocol=%s site=%s path=%s\n", protocol, site, path);
|
|||
|
|
|||
|
/*-----------------------------------------------------------------------*/
|
|||
|
|
|||
|
char str[30];
|
|||
|
// char line[] = "Steve Jobs 50 60";
|
|||
|
char line[] = "U. S. Grant 97 88";
|
|||
|
int s1, s2;
|
|||
|
|
|||
|
for (;;) {
|
|||
|
if (sscanf(line,"%[A-Za-z.] %[^\n]",str,line)==0) {
|
|||
|
sscanf(line,"%d %d",&s1,&s2);
|
|||
|
printf("\n<EFBFBD>̥<EFBFBD><EFBFBD>G<EFBFBD>Ʀr<EFBFBD>Gs1 = %d; s2 = %d\n",s1,s2);
|
|||
|
break;
|
|||
|
}
|
|||
|
printf("\nŪ<EFBFBD><EFBFBD><EFBFBD>X<EFBFBD>ŰO<EFBFBD>G%-10s<30>Ѿl<D1BE><6C><EFBFBD>e<EFBFBD>G%s",str,line);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
char fullName[] = "U. S. Grant", tmp[3][24];
|
|||
|
|
|||
|
s1 = sscanf(fullName,"%s %s %s",tmp[0],tmp[1],tmp[2]);
|
|||
|
printf("\n<EFBFBD>Ĥ@<40>ؤ<EFBFBD><D8A4>k<EFBFBD><6B><EFBFBD>X<EFBFBD>m<EFBFBD><6D><EFBFBD>G%s",tmp[s1-1]);
|
|||
|
|
|||
|
s1 = sscanf(fullName,"%s %s %s",str,str,str);
|
|||
|
printf("\n<EFBFBD>ĤG<EFBFBD>ؤ<EFBFBD><EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>X<EFBFBD>m<EFBFBD><EFBFBD><EFBFBD>G%s",str);
|
|||
|
|
|||
|
for (;;) {
|
|||
|
if (sscanf(fullName,"%s %[^\n]",str,fullName) < 2) {
|
|||
|
printf("\n<EFBFBD>ĤT<EFBFBD>ؤ<EFBFBD><EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>X<EFBFBD>m<EFBFBD><EFBFBD><EFBFBD>G%s",fullName);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*-----------------------------------------------------------------------*/
|
|||
|
|
|||
|
char bigName[][12] = {"Jobs", "Cruise", "Jane", "Obama", "Tyler", "Grant", "Redford", "Lee"};
|
|||
|
int i, count = sizeof(bigName)/sizeof(bigName[0]),
|
|||
|
size = sizeof(bigName[0]);
|
|||
|
|
|||
|
qsort(bigName,count,size,ascend);
|
|||
|
|
|||
|
printf("\n\n<EFBFBD>Ƨǫᵲ<EFBFBD>G<EFBFBD>p<EFBFBD>U:\n");
|
|||
|
for (i = 0; i < count; i++)
|
|||
|
printf("%s\n",bigName[i]);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
int ascend(const void *a, const void *b) /* <20><><EFBFBD>W<EFBFBD>Ҧ<EFBFBD> */
|
|||
|
{
|
|||
|
char *x = (char *)a;
|
|||
|
char *y = (char *)b;
|
|||
|
return strcmp(x,y);
|
|||
|
}
|