C語言中的sscanf()函數(shù)使用詳解
sscanf() - 從一個字符串中讀進與指定格式相符的數(shù)據(jù).
函數(shù)原型:
Int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... );
說明:
sscanf與scanf類似,都是用于輸入的,只是后者以屏幕(stdin)為輸入源,前者以固定字符串為輸入源。
其中的format可以是一個或多個 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '\t' | '\n' | 非%符號}
注:
1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星號 (*) 表示跳過此數(shù)據(jù)不讀入. (也就是不把此數(shù)據(jù)讀入?yún)?shù)中)
2、{a|b|c}表示a,b,c中選一,[d],表示可以有d也可以沒有d。
3、width表示讀取寬度。
4、{h | l | I64 | L}:參數(shù)的size,通常h表示單字節(jié)size,I表示2字節(jié) size,L表示4字節(jié)size(double例外),l64表示8字節(jié)size。
5、type :這就很多了,就是%s,%d之類。
6、特別的:%*[width] [{h | l | I64 | L}]type 表示滿足該條件的被過濾掉,不會向目標參數(shù)中寫入值
支持集合操作:
%[a-z] 表示匹配a到z中任意字符,貪婪性(盡可能多的匹配)
%[aB'] 匹配a、B、'中一員,貪婪性
%[^a] 匹配非a的任意字符,貪婪性
format格式
{%[*] [width][{h | l | l64 | L}]type | ' ' | t' | '\n' | 非%符號}
注:
*可用于格式中,(即%*d和%*s)加了星號(*)表示跳過此數(shù)據(jù)不讀入。(也就是不把數(shù)據(jù)讀入到參數(shù)中)
width表示讀取寬度
{h | l | l64 | L}:參數(shù)size,通常h表示單字節(jié)size,l表示2字節(jié)size,L表示4字節(jié)size,l64表示8字節(jié)size
type參數(shù)類型,例如%s,%d
支持正則表達式,例如%[a-z]匹配a到z中任意字符(ps:正則表達式這個假期我會寫一篇博客記錄)
參考用例
#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); /** * 執(zhí)行結(jié)果: * 3 * i * love * china! * 可以看出,sscanf的返回值為讀取的參數(shù)個數(shù) */ //讀取指定長度的字符串 memset(str, 0, sizeof(str)); strcpy(str, "abcdefghijklmnopq"); sscanf(str, "%5s", buf4); printf("%s\n", buf4); /** * 執(zhí)行結(jié)果: * 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); /** * 執(zhí)行結(jié)果: * 123456 * abcdedf * ANDFS * 很難相信c語言竟然支持正則,不過c支持的正則挺弱的 */ return 0; }
九度ac題目
題目描述
題目描述:
有一個網(wǎng)絡(luò)日志,記錄了網(wǎng)絡(luò)中計算任務(wù)的執(zhí)行情況,每個計算任務(wù)對應(yīng)一條如下形式的日志記錄:
“hs_10000_p”是計算任務(wù)的名稱,
“2007-01-17 19:22:53,315”是計算任務(wù)開始執(zhí)行的時間“年-月-日 時:分:秒,毫秒”,
“253.035(s)”是計算任務(wù)消耗的時間(以秒計)
hs_10000_p 2007-01-17 19:22:53,315 253.035(s)
請你寫一個程序,對日志中記錄計算任務(wù)進行排序。
時間消耗少的計算任務(wù)排在前面,時間消耗多的計算任務(wù)排在后面。
如果兩個計算任務(wù)消耗的時間相同,則將開始執(zhí)行時間早的計算任務(wù)排在前面。
輸入:
日志中每個記錄是一個字符串,每個字符串占一行。最后一行為空行,表示日志結(jié)束。日志中最多可能有10000條記錄。
計算任務(wù)名稱的長度不超過10,開始執(zhí)行時間的格式是YYYY-MM-DD HH:MM:SS,MMM,消耗時間小數(shù)點后有三位數(shù)字。
計算任務(wù)名稱與任務(wù)開始時間、消耗時間之間以一個或多個空格隔開,行首和行尾可能有多余的空格。
輸出:
排序好的日志記錄。每個記錄的字符串各占一行。
輸入的格式與輸入保持一致,輸入包括幾個空格,你的輸出中也應(yīng)該包含同樣多的空格。
樣例輸入:
hs_10000_p 2007-01-17 19:22:53,315 253.035(s)
hs_10001_p 2007-01-17 19:22:53,315 253.846(s)
hs_10002_m 2007-01-17 19:22:53,315 129.574(s)
hs_10002_p 2007-01-17 19:22:53,315 262.531(s)
hs_10003_m 2007-01-17 19:22:53,318 126.622(s)
hs_10003_p 2007-01-17 19:22:53,318 136.962(s)
hs_10005_m 2007-01-17 19:22:53,318 130.487(s)
hs_10005_p 2007-01-17 19:22:53,318 253.035(s)
hs_10006_m 2007-01-17 19:22:53,318 248.548(s)
hs_10006_p 2007-01-17 19:25:23,367 3146.827(s)
樣例輸出:
hs_10003_m 2007-01-17 19:22:53,318 126.622(s)
hs_10002_m 2007-01-17 19:22:53,315 129.574(s)
hs_10005_m 2007-01-17 19:22:53,318 130.487(s)
hs_10003_p 2007-01-17 19:22:53,318 136.962(s)
hs_10006_m 2007-01-17 19:22:53,318 248.548(s)
hs_10000_p 2007-01-17 19:22:53,315 253.035(s)
hs_10005_p 2007-01-17 19:22:53,318 253.035(s)
hs_10001_p 2007-01-17 19:22:53,315 253.846(s)
hs_10002_p 2007-01-17 19:22:53,315 262.531(s)
hs_10006_p 2007-01-17 19:25:23,367 3146.827(s)
ac代碼
#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; } }
相關(guān)文章
Matlab實現(xiàn)繪制高階版本韋恩圖(upset圖)
韋恩圖隨著階數(shù)升高會越來越復(fù)雜,當階數(shù)達到7或者以上時幾乎沒辦法繪制,但是使用upset圖卻可以比較輕易的繪制。本文就來用Matlab實現(xiàn)繪制upset圖,需要的可以參考一下2023-01-01實例講解在C++的函數(shù)中變量參數(shù)及默認參數(shù)的使用
這篇文章主要介紹了在C++的函數(shù)中變量參數(shù)及默認參數(shù)的使用,是C++函數(shù)入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào)
這篇文章主要介紹了C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào),本文講解了函數(shù)的地址、聲明函數(shù)指針、歷史原因、typedef挽救復(fù)雜的函數(shù)指針等內(nèi)容,需要的朋友可以參考下2014-11-11C++構(gòu)造析構(gòu)賦值運算函數(shù)應(yīng)用詳解
構(gòu)造函數(shù)主要作用在于創(chuàng)建對象時為對象的成員屬性賦值,構(gòu)造函數(shù)由編譯器自動調(diào)用,無須手動調(diào)用;析構(gòu)函數(shù)主要作用在于對象銷毀前系統(tǒng)自動調(diào)用,執(zhí)行一 些清理工作2022-09-09