Int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... );
{%[*] [width][{h | l | l64 | L}]type | ' ' | t' | '\n' | 非%符号}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int result;
char str[100];
char buf1[255], buf2[255], buf3[255], buf4[255];
//基本用法
memset(str, 0, sizeof(str));
strcpy(str, "i love china!");
result = sscanf(str, "%s %s %s", buf1, buf2, buf3);
printf("%d\n%s\n%s\n%s\n", result, buf1, buf2, buf3);
/**
* 执行结果:
* 3
* i
* love
* china!
* 可以看出,sscanf的返回值为读取的参数个数
*/
//读取指定长度的字符串
memset(str, 0, sizeof(str));
strcpy(str, "abcdefghijklmnopq");
sscanf(str, "%5s", buf4);
printf("%s\n", buf4);
/**
* 执行结果:
* abcde
*/
//正则匹配字符串
memset(str, 0, sizeof(str));
memset(buf1, 0, sizeof(buf1));
memset(buf2, 0, sizeof(buf2));
memset(buf3, 0, sizeof(buf3));
strcpy(str, "123456abcdedfANDFS");
sscanf(str, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
printf("%s\n%s\n%s\n", buf1, buf2, buf3);
/**
* 执行结果:
* 123456
* abcdedf
* ANDFS
* 很难相信c语言竟然支持正则,不过c支持的正则挺弱的
*/
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct mission
{
char str[200];
char name[20];
int year, month, day, hour, minute, second, micro;
double runtime;
};
int compare(const void *p, const void *q);
int main()
{
struct mission mis[10001];
int i, n = 0;
memset(mis, 0, sizeof(mis));
while(gets(mis[n].str))
{
if(strcmp(mis[n].str, "") == 0)
{
break;
}
sscanf(mis[n].str, "%s%d-%d-%d %d:%d:%d,%d %lf", mis[n].name, &mis[n].year, &mis[n].month, &mis[n].day, &mis[n].hour, &mis[n].minute, &mis[n].second, &mis[n].micro, &mis[n].runtime);
n ++;
}
qsort(mis, n, sizeof(mis[0]), compare);
for(i = 0; i < n; i ++)
{
printf("%s\n", mis[i].str);
}
return 0;
}
int compare(const void *p, const void *q)
{
const struct mission *a = p;
const struct mission *b = q;
if(a->runtime > b->runtime)
{
return 1;
}else if(a->runtime == b->runtime && a->year > b->year)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month > b->month)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day > b->day)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour > b->hour)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute > b->minute)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second > b->second)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second == b->second && a->micro > b->micro)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second > b->second && a->micro == b->micro)
{
return 0;
}
else
{
return -1;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有