<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 251  文章 - 504  trackbacks - 0
    <2006年11月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

    本博客系個人收集材料及學習記錄之用,各類“大俠”勿擾!

    留言簿(14)

    隨筆分類

    收藏夾

    My Favorite Web Sites

    名Bloger

    非著名Bloger

    搜索

    •  

    積分與排名

    • 積分 - 204340
    • 排名 - 283

    最新評論

     假如你的程序同時需要使用getchar()進行字符輸入和使用 scanf()進行數字輸入.這兩個函數的每一個都能很好的完成工作,
    但是它們不能很好地混合在一起.這是因為getchar()讀取每個字符,包括空格,制表符和換行符;而scanf()在讀取數字時候則
    會跳過空格,制表符和換行符.下面是個例子,
    char1.cpp
    /*C語言一個I/O問題程序*/
    #include<stdio.h>
    void display(char cr,int lines,int width);
    int main(void)
    {
    ? int ch;
    ? int rows,cols;
    ? printf("Enter a character and tow integers:\n");
    ? while((ch=getchar())!='\n')
    ? {
    ???? scanf("%d %d",&rows,&cols);
    ???? display(ch,rows,cols);

    ???? printf("Enter another character and tow integers;\n");
    ???? printf("Enter a newline to quit.\n");
    ? }
    ? printf("Bye.\n");
    ? return 0;
    }
    void display(char cr,int lines,int width)
    {
    ? int row ,col;
    ? for(row =1;row<=lines;row++)
    ? {
    ??? for(col=1;col<=width;col++)
    ??? {
    ????? putchar(cr);
    ?????? }
    ????? putchar('\n');

    ? }
    }
    結果輸出:
    Enter a character and tow integers:
    d 3 2
    dd
    dd
    dd
    Enter another character and tow integers;
    Enter a newline to quit.
    Bye.
    Press any key to continue...

     可以看見緊跟在2后面的那個換行符,scanf()函數將該換行符留在了輸入隊列中.而getchar()并不跳過換行符,所以在循環的
    下一周期,在你有機會輸入其他內容之前,這個換行符由getchar()讀出,然后將其賦值給ch,而ch為換行符正是終止循環的條件.

     要解決這個問題,必須跳過一個輸入周期中鍵入的最后一個數字與下一行開始處鍵入的字符之間的所有換行符或空格.
    改進如下:char2.cpp

    /*C語言一個I/O問題程序修改版本*/
    #include<stdio.h>
    void display(char cr,int lines,int width);
    int main(void)
    {
    ? int ch;
    ? int rows,cols;
    ? printf("Enter a character and tow integers:\n");
    ? while((ch=getchar())!='\n')
    ? {
    ??? if( scanf("%d %d",&rows,&cols)!=2)
    ??? break;
    ???? display(ch,rows,cols);
    ???? while(getchar()!='\n')????? /*剔除掉scanf()輸入后的所有字符.*/
    ???? {

    ??????? printf("1");????????? /*后面有多少字符就輸出多少個1*/
    ??????? continue;

    ???? }
    ???? printf("Enter another character and tow integers;\n");
    ???? printf("Enter a newline to quit.\n");
    ? }
    ? printf("Bye.\n");
    ? return 0;
    }
    void display(char cr,int lines,int width)
    {
    ? int row ,col;
    ? for(row =1;row<=lines;row++)
    ? {
    ??? for(col=1;col<=width;col++)
    ??? {
    ????? putchar(cr);
    ?????? }
    ????? putchar('\n');

    ? }
    }

    輸出結果:
    Enter a character and tow integers:
    d 3 4
    dddd
    dddd
    dddd
    Enter another character and tow integers;
    Enter a newline to quit.
    d 3 4
    dddd
    dddd
    dddd
    11Enter another character and tow integers;
    Enter a newline to quit.

    ?

    ?

    ?

    ?

    posted on 2006-11-03 14:12 matthew 閱讀(2600) 評論(1)  編輯  收藏 所屬分類: 數據結構與算法設計

    FeedBack:
    # re: C語言的混合輸入數字和字符問題 2010-11-23 13:40 冰塵
    我覺得只要在調用函數后面加上 getchar(); 也可以解決。。  回復  更多評論
      
    主站蜘蛛池模板: 一级中文字幕乱码免费| 自怕偷自怕亚洲精品| 免费中文字幕一级毛片| 精品无码国产污污污免费| 一二三四免费观看在线电影| 亚洲精品在线免费观看视频 | 亚洲日产韩国一二三四区| 亚洲AV无码乱码在线观看性色扶| 日本免费人成黄页在线观看视频| 成人免费无码大片A毛片抽搐色欲| 免费精品国产自产拍在| 91在线品视觉盛宴免费| 成人午夜性A级毛片免费| 四虎免费在线观看| 小小影视日本动漫观看免费| 免费国产高清视频| 亚洲国产成人久久一区WWW| 国产亚洲情侣一区二区无| 夜夜春亚洲嫩草影院| 亚洲AV无码乱码国产麻豆穿越| 亚洲AV电影院在线观看| 色拍自拍亚洲综合图区| 亚洲中文字幕在线无码一区二区| 91亚洲性爱在线视频| 亚洲精品久久无码av片俺去也| 国产成人亚洲精品无码AV大片| 三级片免费观看久久| 三级网站免费观看| 久久精品毛片免费观看| 最近中文字幕免费mv视频7| 国产一级一片免费播放i| 久久亚洲av无码精品浪潮| 久久91亚洲精品中文字幕| 亚洲伊人久久精品| 亚洲AV无码之国产精品| 老司机精品免费视频| 免费国产黄网站在线观看可以下载 | 亚洲男女内射在线播放| 亚洲av无码无在线观看红杏| 亚洲精品午夜视频| 综合偷自拍亚洲乱中文字幕|