标签:
scanf()是C语言中用于读入格式化数据(formatted data)的函数。
我们可能对scanf()的一般用法已经了然,而至于scanf()读入数据的机制恐怕并不十分清楚。
下面我将比较详细地介绍scanf()的工作机制,并指出其丰富且强大的格式化方式。
内容来自这个链接
int scanf ( const char * format, ... );
Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.
The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within theformat string.
specifier | Description | Characters extracted |
---|---|---|
i | Integer | Any number of digits, optionally preceded by a sign (+ or -). Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0xhexadecimal digits (0-f). Signed argument. |
d or u | Decimal integer | Any number of decimal digits (0-9), optionally preceded by a sign (+ or -). d is for a signed argument, and u for an unsigned. |
o | Octal integer | Any number of octal digits (0-7), optionally preceded by a sign (+ or -). Unsigned argument. |
x | Hexadecimal integer | Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -). Unsigned argument. |
f, e, g | Floating point number | A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod). Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X . |
a | ||
c | Character | The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. |
s | String of characters | Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence. |
p | Pointer address | A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf. |
[characters] | Scanset | Any number of the characters specified between the brackets. A dash (-) that is not the first character may produce non-portable behavior in some library implementations. |
[^characters] | Negated scanset | Any number of characters none of them specified as characters between the brackets. |
n | Count | No input is consumed. The number of characters read so far from stdin is stored in the pointed location. |
% | % | A % followed by another % matches a single %. |
sub-specifier | description |
---|---|
* | An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument). |
width | Specifies the maximum number of characters to be read in the current reading operation (optional). |
length | One of hh, h, l, ll, j, z, t, L (optional). This alters the expected type of the storage pointed by the corresponding argument (see below). |
specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x | f e g a | c s [] [^] | p | n | |
(none) | int* | unsigned int* | float* | char* | void** | int* | |
hh | signed char* | unsigned char* | signed char* | ||||
h | short int* | unsigned short int* | short int* | ||||
l | long int* | unsigned long int* | double* | wchar_t* | long int* | ||
ll | long long int* | unsigned long long int* | long long int* | ||||
j | intmax_t* | uintmax_t* | intmax_t* | ||||
z | size_t* | size_t* | size_t* | ||||
t | ptrdiff_t* | ptrdiff_t* | ptrdiff_t* | ||||
L | long double* |
从stdin读入3行由小写英文字母 (lowercase Latin letters) 与空格 (space) 组成的字符串,统计每行中元音字母 (a, e, i, o, u)出现的次数,看是否依次为5, 7, 5。
问题归结为如何读入包含空格的一整行,根据上述题目中的要求我们将问题具体描述为:
如何读入含有空格的一整行字符串,起始空格(leading sapces) 可忽略(optional)。
#include<bits/stdc++.h>
using namespace std;
const char *vow="aeiou";
int count_vow(char *s){
int res=0;
for(int i=0; s[i]; i++)
for(int j=0; vow[j]; j++){
if(s[i]==vow[j]){
res++;
break;
}
}
return res;
}
int main(){
//freopen("in", "r", stdin);
char s[105];
int ok[]={5, 7, 5};
for(int i=0; i<3; i++){
scanf(" %[^\n]", s);
if(count_vow(s)!=ok[i]){
puts("NO");
return 0;
}
}
puts("YES");
return 0;
}
标签:
原文地址:http://www.cnblogs.com/Patt/p/4700877.html