scanf的高级输入

来源:互联网 发布:江苏瑞中数据 编辑:程序博客网 时间:2024/05/16 14:08

The scanf function pieces tips:

    sequence of characters can be input using a scan set. A scan set is a set of characters
    enclosed in square brackets, [] , and preceded by a percent sign in the format control
    string. A scan set scans the characters in the input stream, looking only for those characters
    that match characters contained in the scan set. Each time a character is matched, it’s
    stored in the scan set’s corresponding argument—a pointer to a character array. The scan
    set stops inputting characters when a character that is not contained in the scan set is
    encountered. If the first character in the input stream does not match a character in the
    scan set, only the null character is stored in the array. An example as followed uses the
    scan set [aeiou] to scan the input stream for vowels. Notice that the first seven letters
    of the input are read. The eighth letter ( h ) is not in the scan set and therefore the
    scanning is terminated.

    for example:

    #include <stdio.h>    int main()    {        char z[9];        printf("Enter string: ");        scanf("%[aeiou]", z);        printf("The input was \"%s\"\n", z);        return 0;    }



    while you run this program, it results as:
        +---------------------------------------------------------------+
        |   Enter string: ooeeooahah                                    |
        |   The input was "ooeeooa"                                     |
        +---------------------------------------------------------------+
    
    The scan set can also be used to scan for characters not contained in the scan set by
    using an inverted scan set. To create an inverted scan set, place a caret (^) in the square
    brackets before the scan characters. This causes characters not appearing in the scan set to
    be stored. When a character contained in the inverted scan set is encountered, input ter-
    minates. The followed example uses the inverted scan set [^aeiou] to search for consonants—more
    properly to search for “nonvowels.”
    
    for example:

      
       #include <stdio.h>       int main()       {           char z[9];           printf("Enter a string: ");           scanf("%[^aeiou]", z);           printf("The input was \"%s\"\n", z);           return 0;       }


    while you run this program, it results:
        +---------------------------------------------------------------+
        |   Enter a string: String                                              |
        |   The input was "Str"                                                 |
        +---------------------------------------------------------------+

    A field width can be used in a scanf conversion specifier to read a specific number of
    characters from the input stream. The next example inputs a series of consecutive digits as a two-
    digit integer and an integer consisting of the remaining digits in the input stream.

    for example:
       
        #include <stdio.h>        int main()        {            int x, y;            printf("Enter a six digit integer: ");            scanf("%2d%d", &x, &y);            printf("The integers input were %d and %d\n", x, y);            return 0;        }

    
    +-----------------------------------------------------------------------+
    |   Enter a six digit integer: 123456                                    |
    |   The integer input were 12 and 3456                             |
    +-----------------------------------------------------------------------+

    scanf provides the assignment suppression character * . The assignment suppres-
    sion character enables scanf to read any type of data from the input and discard it without
    assigning it to a variable.

    for example:
       
        #include <stdio.h>        int main()        {            int month1, day1, year1;            int month2, day2, year2;            printf("Enter a date in the form mm-dd-yyyy: ");            scanf("%d%*c%d%*c%d", &month1, &day1, &year1);            printf("month = %d  day = %d  year = %d\n\n", month1, day1, year1);            printf("Enter a date in the form mm/dd/yyyy: ");            scanf("%d%*c%d%*c%d", &month1, &day1, &year1);            printf("month = %d  day = %d  year = %d\n\n", month2, day2, year2);            return 0;        }



    +-------------------------------------------------------------------------------+
    |   Enter a date in the form mm-dd-yyyy: 11-18-2003                |
    |   month = 11  day = 18  year = 2003                                           |
    |                                                                                                            |
    |   Enter a date in the form mm/dd/yyyy: 11/18/2003                 |
    |   month = 11  day = 18  year = 2003                                         |
    +------------------------------------------------------------------------------+


0 0
原创粉丝点击