【C】标准库中的sscanf函数 和 用变量控制格式化输出位数

1.sscanf function

方法转载自:https://www.cnblogs.com/wangshaowei/p/10837263.html

  1 /*****************************************************
  2 ** Name         : sscanf.c
  3 ** Author       : gzshun
  4 ** Version      : 1.0
  5 ** Date         : 2011-12
  6 ** Description  : sscanf function
  7 ******************************************************/
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <string.h>
 11 
 12 static void sscanf_test(void);
 13 
 14 static void sscanf_test(void)
 15 {
 16     int ret;
 17     char *string;
 18     int  digit;
 19     char buf1[255];
 20     char buf2[255];
 21     char buf3[255];
 22     char buf4[255];
 23 
 24     /*1.最简单的用法*/
 25     string = "china beijing 123";
 26     ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);
 27     printf("1.string=%s
", string);
 28     printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d

", ret, buf1, buf2, digit);
 29     /*
 30     **执行结果:
 31     **1.ret=3, buf1=china, buf2=beijing, digit=123
 32     **可以看出,sscanf的返回值是读取的参数个数
 33     */
 34 
 35     /*2.取指定长度的字符串*/
 36     string = "123456789";
 37     sscanf(string, "%5s", buf1);
 38     printf("2.string=%s
", string);
 39     printf("2.buf1=%s

", buf1);
 40     /*
 41     **执行结果:
 42     **2.buf1=12345
 43     */
 44 
 45     /*3.取到指定字符为止的字符串*/
 46     string = "123/456";
 47     sscanf(string, "%[^/]", buf1);
 48     printf("3.string=%s
", string);
 49     printf("3.buf1=%s

", buf1);
 50     /*
 51     **执行结果:
 52     **3.buf1=123
 53     */
 54 
 55     /*4.取到指定字符集为止的字符串*/
 56     string = "123abcABC";
 57     sscanf(string, "%[^A-Z]", buf1);
 58     printf("4.string=%s
", string);
 59     printf("4.buf1=%s

", buf1);
 60     /*
 61     **执行结果:
 62     **4.buf1=123abc
 63     */
 64 
 65     /*5.取仅包含指定字符集的字符串*/
 66     string = "0123abcABC";
 67     sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
 68     printf("5.string=%s
", string);
 69     printf("5.buf1=%s, buf2=%s, buf3=%s

", buf1, buf2, buf3);
 70     /*
 71     **执行结果:
 72     **5.buf1=123, buf2=abc, buf3=ABC
 73     */
 74 
 75     /*6.获取指定字符中间的字符串*/
 76     string = "ios<android>wp7";
 77     sscanf(string, "%*[^<]<%[^>]", buf1);
 78     printf("6.string=%s
", string);
 79     printf("6.buf1=%s

", buf1);
 80     /*
 81     **执行结果:
 82     **6.buf1=android
 83     */
 84 
 85     /*7.指定要跳过的字符串*/
 86     string = "iosVSandroid";
 87     sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
 88     printf("7.string=%s
", string);
 89     printf("7.buf1=%s, buf2=%s

", buf1, buf2);
 90     /*
 91     **执行结果:
 92     **7.buf1=ios, buf2=android
 93     */
 94 
 95     /*8.分割以某字符隔开的字符串*/
 96     string = "android-iphone-wp7";
 97     /*
 98     **字符串取道'-'为止,后面还需要跟着分隔符'-',
 99     **起到过滤作用,有点类似于第7点
100     */
101     sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
102     printf("8.string=%s
", string);
103     printf("8.buf1=%s, buf2=%s, buf3=%s

", buf1, buf2, buf3);
104     /*
105     **执行结果:
106     **8.buf1=android, buf2=iphone, buf3=wp7
107     */
108 
109     /*9.提取邮箱地址*/
110     string = "Email:beijing@sina.com.cn";
111     sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4);
112     printf("9.string=%s
", string);
113     printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s

", buf1, buf2, buf3, buf4);
114     /*
115     **执行结果:
116     **9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
117     */
118 
119     /*10.过滤掉不想截取或不需要的字符串--补充,
120     **在%号后面加一*号,代表过滤这个字符串,不读取
121     */
122     string = "android iphone wp7";
123     sscanf(string, "%s %*s %s", buf1, buf2);
124     printf("10.string=%s
", string);
125     printf("10.buf1=%s, buf2=%s

", buf1, buf2);
126     /*
127     **执行结果:
128     **10.android wp7
129     */
130 }
131 
132 int main(int argc, char **argv)
133 {
134     sscanf_test();
135 
136     return 0;
137 }
138 
139 /*
140 **测试程序
141 **环境:
142 **Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux
143 **gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
144 **
145 gzshun@ubuntu:~/c/sscanf$ gcc sscanf.c -o sscanf
146 gzshun@ubuntu:~/c/sscanf$ ./sscanf
147 1.string=china beijing 123
148 1.ret=3, buf1=china, buf2=beijing, digit=123
149 2.string=123456789
150 2.buf1=12345
151 3.string=123/456
152 3.buf1=123
153 4.string=123abcABC
154 4.buf1=123abc
155 5.string=0123abcABC
156 5.buf1=0123, buf2=abc, buf3=ABC
157 6.string=ios<android>wp7
158 6.buf1=android
159 7.string=iosVSandroid
160 7.buf1=ios, buf2=android
161 8.string=android-iphone-wp7
162 8.buf1=android, buf2=iphone, buf3=wp7
163 9.string=Email:beijing@sina.com.cn
164 9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
165 10.string=android iphone wp7
166 10.buf1=android, buf2=wp7
167 */

用法:

很切实的,NUIST的LevOJ里【P1135 NBA总冠军】可以用此写法。

当遇到带空格的字符时,需要把空格输入进去却又要把第二个空格后的数字排除在外(eg:New York 2020)--> (aim:"New York" "2020" )

题目描述:

又要考试了,LJW决定放松一下,就打开电视,看见篮球赛,他立即想到了每年的NBA总冠军队伍。由于复习紧张,他只记起了一部分,记忆的内容是正确的,可能不是按时间顺序排列的,记忆的内容可能有重复。
现在请求学过编程的你帮助LJW,按时间顺序依次输出总冠军球队(不能重复)。(NBA从1947A.D到2009A.D)

  输入:第一行是一个整数n(0接下来的n行,每行先是城市名(由大小字母、空格组成),后是时间(由数字组成)。二者之间用空格隔开。

  输出:排序后的非重复数据,时间在前队伍名在后。

主函数:

 1 int main(){
 2     int count,n,i,t;
 3     char temp[max];
 4     team a[max];
 5     while(scanf("%d",&n)!=EOF){
 6         getchar();    //清除回车
 7         count = n;
 8         for(i=0;i<n;i++){
 9             gets(temp);        //临时的输入环境
10             for(t=0;temp[t]<'0' || temp[t]>'9';t++);    //测量字母字符的长度
11 /////////////////////////////////////////////////////////////////////////
12             sscanf(temp+t,"%d",&a[i].year);        //将数字放置进整型变量year中
13 /////////////////////////////////////////////////////////////////////////
14             temp[t-1] = '';        //把原来临时输入环境的数字全部砍去,只留下字母放置进字符数组name中
15             strcpy(a[i].name,temp);
16          }
17          n = pick(a,count);        //挑出重复多余的 
18         sort(a,n);        //排序 
19         for(i=0;i<n;i++)
20             printf("%d %s
",a[i].year,a[i].name);        //反位输出 
21     }
22     return 0;
23 }

2.用变量控制位数

1.“*”符:用以表示该输入项,读入后不赋予相应的变量,即跳过该输入值。

  忽略要读的项。比如 %*d 就是读一个 %d 该读的东西,但不赋值给任何变量。

  输入:

scanf("%*c %*c %c", "I","L","U");

  输出:

 U

2.在printf,控制输出格式

  输入:

printf("%*s
",5,"123");
printf("%*s",-5,"123");

  输出:

_ _123

123_ _

  也可以用%5s和%-5s达到一样的效果。

3.2情况而下的“浮点”

  输入:

char *s = "this is test example";
printf(
"%.*s ", 10, s);
printf(
"%*.*s ", 20, 10, s);

  输出:

this is te

__________this is te
//~~~~~~~^10个空格

4.用变量的方法去控制*的值

  输入:

a = 15,

b = 10;

printf("%*.*s
", a, b, s);

  输出:

this is te

__________this is te
//~~~~~~~^10个空格

  

原文地址:https://www.cnblogs.com/yinjx/p/14249280.html