sscanf()函數詳解(c語言)

來源:互聯網
上載者:User
前言今天在家裡做acm的時候,用到了sscanf()函數,感覺特別犀利,這裡記錄一下用法。sscanf()函數原型
int sscanf(const char *str, const char *format, mixed var1, mixed var2);

說明:sscanf()會將參數str的字串根據參數format來轉換並格式化資料。format格式

{%[*] [width][{h | l | l64 | L}]type | ' ' | t' | '\n' | 非%符號}

註:

  1. *可用于格式中,(即%*d和%*s)加了星號(*)表示跳過此資料不讀入。(也就是不把資料讀入到參數中)
  2. width表示讀取寬度
  3. {h | l | l64 | L}:參數size,通常h表示單位元組size,l表示2位元組size,L表示4位元組size,l64表示8位元組size
  4. type參數類型,例如%s,%d
  5. 支援Regex,例如%[a-z]匹配a到z中任一字元(ps:Regex這個假期我會寫一篇部落格記錄)
參考用例
#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;}
九度ac題目題目描述
題目描述:有一個部落格,記錄了網路中計算任務的執行情況,每個計算任務對應一條如下形式的日誌記錄:“hs_10000_p”是計算任務的名稱,“2007-01-17 19:22:53,315”是計算任務開始執行的時間“年-月-日 時:分:秒,毫秒”, “253.035(s)”是計算任務消耗的時間(以秒計)hs_10000_p 2007-01-17 19:22:53,315 253.035(s)請你寫一個程式,對日誌中記錄計算任務進行排序。時間消耗少的計算任務排在前面,時間消耗多的計算任務排在後面。如果兩個計算任務消耗的時間相同,則將開始執行時間早的計算任務排在前面。輸入:日誌中每個記錄是一個字串,每個字串佔一行。最後一行為空白行,表示日誌結束。日誌中最多可能有10000條記錄。計算任務名稱的長度不超過10,開始執行時間的格式是YYYY-MM-DD HH:MM:SS,MMM,消耗時間小數點後有三位元字。計算任務名稱與任務開始時間、消耗時間之間以一個或多個空格隔開,行首和行尾可能有多餘的空格。輸出:排序好的日誌記錄。每個記錄的字串各佔一行。輸入的格式與輸入保持一致,輸入包括幾個空格,你的輸出中也應該包含同樣多的空格。範例輸入: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;}}
參考連結http://blog.csdn.net/gzshun/article/details/7081736

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.