BeautifulMan

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            16 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
          復習題
          1、putchar(getchar())是一個有效的表達式,它實現什么功能?getchar(putchar())也有效嗎?
          答:
          語句putchar(getchar())使程序讀取下一個輸入字符并打印它,getchar()的返回值作為putchar()的參數。getchar(putchar())則不是合法的,因為getchar()不需要參數而putchar()需要一個參數。
          2、下面的每個語句實現什么功能?
              a.putchar('H');
              b.putchar('\007');
              c.putchar('\n');
              d.putchar('\b');
          答:
          a. 顯示字符H
          b.如果系統使用ASCII字符編碼,則發出一聲警報
          c.把光標移動到下一行的開始
          d.退后一格
          3、假設您有一個程序count,該程序對輸入的字符進行統計。用count程序設計一個命令行命令,對文件essay中的字符進行計數并將結果保存在名為essayct的文件中。
          答:
          count < essay > essayct
          4、給定問題3中的程序和文件,下面哪個命令是正確的?
          答:
          a.essayct <essay
          b.count essay
          c.essay >count
          答:
          c是正確的。
          5、EOF是什么?
          答:
          它是由getchar()和scanf()返回的信號(一個特定的值),用來表明已經到達了文件的結尾。
          6、對給出的輸入,下面每個程序段的輸出是什么(假定ch是int類型的,并且輸入是緩沖的)?
          a. 輸入如下所示:
              If you quit, I will.[enter]
              程序段如下所示:
              while ((ch = getchar()) != 'i')
                      putchar(ch);
          b. 輸入如下所示:
              Harhar[enter]
              程序段如下所示:
              while ((ch = getchar()) != '\n')
              {
                         putchar(ch++);
                         putchar(++ch);
              }
          答:
          a.If you qu
          b.HJacrthjacrt
          7、C如何處理具有不同文件和換行約定的不同計算機系統?
          答:
          C的標準I/O庫把不同的文件形式映射為統一的流,這樣就可以按相同的方式對它們進行處理。
          8、在緩沖系統中把數值輸入與字符輸入相混合時,您所面臨的潛在問題是什么?
          答:
          數字輸入跳過空格和換行符,但是字符輸入并不是這樣。假設您編寫了這樣的代碼:
              int score;
              char grade;
              printf("Enter the score.\n");
              scanf("%d", &score);
              printf("Enter the letter grade.\n");
              grade = getchar();
          假設您輸入分數98,然后按下回車鍵來把分數發送給程序,您同時也發送了一個換行符,它會成為下一個輸入字符被讀取到grade中作為等級的值。如果在字符輸入之前進行了數字輸入,就應該添加代碼以在獲取字符輸入之前剔除換行字符。
          編程練習
          1、
          #include <stdio.h>
          int main(void)
          {
              int ch;
              int count = 0;
              while((ch = getchar()) != EOF) // 包括換行符
                  count++;
              printf("The number of characters is %d\n", count);
              return 0;
          }
          2、(覺得這題超難的!!!看了一些他人寫的例子,簡直胡說八道!!!不過還是完美解決了)
          #include <stdio.h>
          int main(void)
          {
              int ch;
              int i = 0;

              while((ch = getchar()) != EOF)
              {
                  if(ch >= 32)  // 可打印字符
                  {
                      putchar(ch);
                      printf("/%d  ", ch);
                      i++;
                  }
                  else if(ch == '\n')  // 打印換行符
                  {
                      printf("\\n");
                      printf("/%d  ", ch);
                      putchar(ch); // 清除輸入緩沖區里面的換行符
                      = 0// i置為0重新開始計數,因為題目要求每次遇到一個換行符時就要開始打印一個新行
                  }
                  else if(ch == '\t')  // 打印制表符
                  {
                      printf("\\t");
                      printf("/%d  ", ch);
                      i++;
                  }
                  else // 打印控制字符
                  {
                      putchar('^');
                      putchar(ch + 64);
                      printf("/%d  ", ch);
                  }
                  if(i == 10)
                  {
                      putchar('\n');
                      i = 0;
                  }
              }
              return 0;
          }
          運行結果如下:
          I love you!
          I/73   /32  l/108  o/111  v/118  e/101   /32  y/121  o/111  u/117(每行打印10個值)
          !/33  \n/10(每次遇到一個換行符時就開始一個新行)
          My hello world^A
          M/77  y/121   /32  h/104  e/101  l/108  l/108  o/111   /32  w/119(每行打印10個值)
          o/111  r/114  l/108  d/100  ^A/1  \n/10(每次遇到一個換行符時就開始一個新行)
          ^Z
          3、
          #include <stdio.h>
          #include <ctype.h>
          int main(void)
          {
              int ch;
              int low_count = 0, up_count = 0;

              while((ch = getchar()) != EOF)
              {
                  if(islower(ch))
                      low_count++;
                  if(isupper(ch))
                      up_count++;
              }
              printf("A number of capital letters: %d\n", up_count);
              printf("A number of lower case letters: %d\n", low_count);
              return 0;
          }
          4、
          #include <stdio.h>
          #include <ctype.h>
          #include <stdbool.h>
          int main(void)
          {
              char ch;
              long chars = 0L; // 統計單詞的字符數
              int words= 0; // 單詞數
              bool inword = false// 如果ch在一個單詞中,則inword為true

              printf("Enter text to be analyzed: \n");
              while((ch = getchar()) != EOF)
              {
                  if(!isspace(ch) && !ispunct(ch))
                      chars++;
                  if(!isspace(ch) && !inword)
                  {
                      inword = true;
                      words++;
                  }
                  if(isspace(ch) && inword)
                      inword = false;
              }
              printf("The average number of words per word: %ld\n", chars / words);
              return 0;
          }
          5、(二分搜索算法第一次碰見,搞了大半天了,借鑒的是CSDN-----vs9841作者的做法,不過稍微加了下工)
          #include <stdio.h>
          char get_choice(void);
          char get_first(void);
          int main(void)
          {
              int low = 1, high = 100, guess = 50;
              char ch;

              printf("Pick an integer from 1 to 100. I will try to guess it\n");
              printf("Unis your number %d?\n", guess);
              while((ch = get_choice()) != 'q')
              {
                  if(ch == 'a')
                  {
                      printf("I knew I could do it!\n");
                      break;
                  }
                  else if(ch == 'b')
                  {
                      printf("It is too small!\n");
                      low = guess + 1;
                  }
                  else if(ch == 'c')
                  {
                      printf("It is too big!\n");
                      high = guess - 1;
                  }
                  guess = (low + high) / 2;
                  printf("Unis your number %d?\n", guess);
              }
              printf("Done!\n");
              return 0;
          }
          char get_choice(void)
          {
              int ch;

              printf("Enter the letter of your choice: \n");
              printf("a. right       b. too small\n");
              printf("c. too big     q. quit\n");
              ch = get_first();
              while((ch < 'a' || ch > 'c') && ch != 'q')
              {
                  printf("Please respond with a, b, c, or q.\n");
                  ch = get_first();
              }
              return ch;
          }
          char get_first(void)
          {
              int ch;

              ch = getchar();
              while(getchar() != '\n')
                  continue;
              return ch;
          }
          6、
          char get_first(void)
          {
              int ch;

              while((ch = getchar()) == '\n')
                  continue;
              while(getchar() != '\n')
                  continue;
              return ch;
          }
          7、
          #include <stdio.h>
          #define WORK_OVERTIME 40
          #define MULTIPLE 1.5
          #define RATE1 0.15
          #define RATE2 0.20
          #define RATE3 0.25
          #define BREAK1 300
          #define BREAK2 450
          #define BASE1 (BREAK1 * RATE1)
          #define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
          char get_choice(void);
          char get_first(void);
          int main(void)
          {
              int hour, choise;
              double total, tax, net_pay;
              double base_pay; // 基本工資等級不能用#define來定義了,因為它要隨著程序而改變了,書上真是胡說八道

              while((choise = get_choice()) != 'q')
              {
                  switch(choise)
                  {
                  case 'a':
                      base_pay = 8.15;
                      break;  // break只是導致程序脫離switch語句,跳到switch之后的下一條語句!!!
                  case 'b':
                      base_pay = 9.33;
                      break;
                  case 'c':
                      base_pay = 10.00;
                      break;
                  case 'd':
                      base_pay = 11.20;
                      break;
                  default:
                      printf("Program error!\n");
                      break;
                  }
                  printf("Please enter the hour used: ");
                  scanf("%d", &hour); // 獲取每周工作小時數時沒有像書上那樣判斷,我偷懶了!!!
                  if(hour <= WORK_OVERTIME)
                  {
                      total = hour * base_pay;
                      if (total <= BREAK1)
                      {
                          tax = total * RATE1;
                          net_pay = total - tax;
                      }
                      else
                      {
                          tax = BASE1 + (total - BREAK1) * RATE2;
                          net_pay = total - tax;
                      }
                  }
                  else
                  {
                      total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
                      if(total <= BREAK2)
                      {
                          tax = BASE1 + (total - BREAK1) * RATE2;
                          net_pay = total - tax;
                      }
                      else
                      {
                          tax = BASE2 + (total - BREAK2) * RATE3;
                          net_pay = total - tax;
                      }
                  }
                  printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
              }
              printf("Bye!\n");
              return 0;
          }
          char get_choice(void)
          {
              int ch;

              printf("*****************************************************************\n");
              printf("Enter number corresponding to the desired pay rate or action:\n");
              printf("a) $8.75/hr\tb) $9.33/hr\n");
              printf("c) $10.00/hr\td) $11.20/hr\n");
              printf("q) quit\n");
              printf("*****************************************************************\n");
              printf("Please enter your choise: ");
              ch = get_first();
              while((ch < 'a' || ch > 'd') && ch != 'q')
              {
                  printf("Please respond with a, b, c, d, or q.\n");
                  ch = get_first();
              }
              return ch;
          }
          char get_first(void)
          {
              int ch;

              while((ch = getchar()) == '\n')
                  continue;
              while(getchar() != '\n')
                  continue;
              return ch;
          }
          8、
          #include <stdio.h>
          char get_choice(void);
          char get_first(void);
          float get_float(void);
          int main(void)
          {
              char choise;
              float first_number, second_number;

              while((choise = get_choice()) != 'q')
              {
                  printf("Enter first number: ");
                  first_number = get_float();
                  printf("Enter second number: ");
                  second_number = get_float();
                  switch(choise)
                  {
                  case 'a':
                      printf("%.1f + %.1f = %.1f\n", first_number, second_number, first_number + second_number);
                      break;
                  case 's':
                      printf("%.1f - %.1f = %.1f\n", first_number, second_number, first_number - second_number);
                      break;
                  case 'm':
                      printf("%.1f * %.1f = %.1f\n", first_number, second_number, first_number * second_number);
                      break;
                  case 'd':
                      if(second_number == 0)
                      {
                          printf("Enter a number other than 0: ");
                          second_number = get_float();
                          printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
                      }
                      else
                          printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
                      break;
                  default:
                      printf("Program error!\n");
                      break;
                  }
              }
              printf("Bye.\n");
              return 0;
          }
          char get_choice(void)
          {
              int ch;

              printf("Enter the operation of your choice: \n");
              printf("a. add\ts. subtract\n");
              printf("m. multiply\td. divide\n");
              printf("q. quit\n");
              ch = get_first();
              while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
              {
                  printf("Please respond with a, s, m, d, or q.\n");
                  ch = get_first();
              }
              return ch;
          }
          char get_first(void)
          {
              int ch;

              while((ch = getchar()) == '\n')
                  continue;
              while(getchar() != '\n')
                  continue;
              return ch;
          }
          float get_float(void)
          {
              float input;
              char ch;

              while((scanf("%f", &input)) != 1)
              {
                  while((ch = getchar()) != '\n')
                      putchar(ch);
                  printf(" is not a number.\nPlease enter a ");
                  printf("number, such as 2.5, -1.78E8, or 3: ");
              }
              return input;
          }

          posted on 2015-11-21 20:12 李阿昀 閱讀(432) 評論(0)  編輯  收藏 所屬分類: C Primer Plus 復習題與編程練習
          主站蜘蛛池模板: 尉氏县| 冕宁县| 五峰| 兴仁县| 滁州市| 偏关县| 布拖县| 依安县| 桃源县| 北京市| 宣恩县| 仁布县| 晋州市| 济南市| 双流县| 郴州市| 东台市| 巴东县| 潜山县| 桐乡市| 沙湾县| 扶风县| 邛崃市| 岱山县| 华池县| 勃利县| 宁晋县| 乌鲁木齐市| 繁峙县| 普定县| 麻城市| 曲阳县| 宾阳县| 莱西市| 宜宾县| 米泉市| 湖州市| 临武县| 资源县| 宣汉县| 库尔勒市|