隨筆 - 251  文章 - 504  trackbacks - 0
          <2010年11月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          本博客系個(gè)人收集材料及學(xué)習(xí)記錄之用,各類“大俠”勿擾!

          留言簿(14)

          隨筆分類

          收藏夾

          My Favorite Web Sites

          名Bloger

          非著名Bloger

          搜索

          •  

          積分與排名

          • 積分 - 204894
          • 排名 - 283

          最新評論

           假如你的程序同時(shí)需要使用getchar()進(jìn)行字符輸入和使用 scanf()進(jìn)行數(shù)字輸入.這兩個(gè)函數(shù)的每一個(gè)都能很好的完成工作,
          但是它們不能很好地混合在一起.這是因?yàn)間etchar()讀取每個(gè)字符,包括空格,制表符和換行符;而scanf()在讀取數(shù)字時(shí)候則
          會(huì)跳過空格,制表符和換行符.下面是個(gè)例子,
          char1.cpp
          /*C語言一個(gè)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');

          ? }
          }
          結(jié)果輸出:
          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后面的那個(gè)換行符,scanf()函數(shù)將該換行符留在了輸入隊(duì)列中.而getchar()并不跳過換行符,所以在循環(huán)的
          下一周期,在你有機(jī)會(huì)輸入其他內(nèi)容之前,這個(gè)換行符由getchar()讀出,然后將其賦值給ch,而ch為換行符正是終止循環(huán)的條件.

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

          /*C語言一個(gè)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");????????? /*后面有多少字符就輸出多少個(gè)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');

          ? }
          }

          輸出結(jié)果:
          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 閱讀(2609) 評論(1)  編輯  收藏 所屬分類: 數(shù)據(jù)結(jié)構(gòu)與算法設(shè)計(jì)

          FeedBack:
          # re: C語言的混合輸入數(shù)字和字符問題 2010-11-23 13:40 冰塵
          我覺得只要在調(diào)用函數(shù)后面加上 getchar(); 也可以解決。。  回復(fù)  更多評論
            
          主站蜘蛛池模板: 宁陕县| 万载县| 石河子市| 丽水市| 涿州市| 桃源县| 蚌埠市| 灵川县| 元朗区| 永新县| 泰宁县| 博兴县| 绥宁县| 盈江县| 东丽区| 汉沽区| 清镇市| 洛扎县| 淅川县| 桃江县| 宜阳县| 阳曲县| 兴海县| 黄梅县| 锡林郭勒盟| 宁国市| 定结县| 福鼎市| 昌宁县| 宁海县| 类乌齐县| 中阳县| 崇阳县| 渝中区| 仙游县| 建阳市| 磐安县| 郎溪县| 大安市| 本溪市| 介休市|