zoukankan      html  css  js  c++  java
  • 【C语言学习】《C Primer Plus》第8章 字符输入/输出和输入确认

    学习总结

    1、缓冲区分为完全缓冲区(fully buffered)I/O和行缓冲区(line-buffered)I/O。对完全缓冲输入来说,当缓冲区满的时候会被清空(缓冲区内容发送至其目的地)。这类型的缓冲区通常出现在文件输入中。对于行缓冲I/O来说,遇到一个换行字符时将被清空缓冲区,键盘输入是标准的行缓冲区。

    2、EOF是C对文件结尾的一个标识,在stdio.h头文件中定义,#define EOF (-1)。在使用键盘输入时,可以通过Ctrl+D模拟EOF信号:

    复制代码
    #include <stdio.h>
    int main(){
            int ch;
            while((ch=getchar())!=EOF){
                    putchar(ch);
            }
            return 0;
    }
    复制代码

    Abc[Enter]

    abc

    123[Ctrl+D]123

    3、>和<都是重定向运算符,必须是可执行程序加文件,拿以上程序为例子:

    执行:./test > abc

    输入:

    abcdefg[Enter]1234567[Enter]

    执行:cat abc

    输出:

    abcdefg

    1234567

    4、除了以上重定向运算符还有>>运算符,该运算符可使您的一个现有文件的末尾追加数据。还有管道运算符(|),其实这些运算符都是Unix和Linux上的运算符。

    执行:./test >> abc

    输入:

    hijklmn[Enter]

    执行:cat abc

    输出:

    abcdefg

    1234567

    hijklmn

    执行:./test | grep aaa

    输入:aaabbbccc[Enter]dddeeefff[Enter]ggghhhaaa[Enter]

    输出:

    aaabbbccc

    ggghhhaaa

    5、在创建与用户对话的程序时,需要考虑到所有的边界问题,例如程序只需要用户输入a、b、c、d的时候,万一用户输入量其他的且一大串的字符会如何处理等等情况。还有程序同时需要getchar进行字符输入和使用scanf进行数字输入,这两个函数中的每一个都能很好的完成其工作,但它们不能很好地混合在一起,这是因为getchar读取每个字符,包括空格、制表符和换行符,而scanf在读取数字时则会跳过空格、制表符和换行符。

    6、编程题(题8)

    复制代码
     1 #include <stdio.h>
     2 
     3 int getChoice(void);
     4 int getFirst(void);
     5 
     6 int main(){
     7         int ch,t,y;
     8         float a,b;
     9         int c;
    10         char sa[10],sb[10];
    11 
    12         ch=getChoice();
    13         if(ch==113){
    14                 return 0;
    15         }else{
    16                 printf("Enter first number:");
    17                 scanf("%s",sa);
    18                 while(sscanf(sa,"%f",&a)!=1){
    19                         printf("%s is not an number.
    Please enter a number.such as 2.5, -1.78E8, or 3:",sa);
    20                         scanf("%s",sa);
    21                 }
    22                 printf("Enter second number:");
    23                 scanf("%s",sb);
    24                 while(sscanf(sb,"%f",&b)!=1||(ch==100&&b==0)){
    25                         if(ch==100&&b==0){
    26                                 printf("Enter a number other than 0:");
    27                         }else{
    28                                 printf("%s is not an number.
    Please enter a number.such as 2.5, -1.78E8, or 3:",sb);
    29                         }
    30                         scanf("%s",sb);
    31                 }
    32         }
    33 
    34         if(ch==97){
    35                 printf("%s+%s=%.1f
    ",sa,sb,a+b);
    36         }else if(ch==98){
    37                 printf("%s-%s=%.1f
    ",sa,sb,a-b);
    38         }else if(ch==99){
    39                 printf("%s*%s=%s
    ",sa,sb,a*b);
    40         }else{
    41                 printf("%s/%s=%.1f
    ",sa,sb,a/b);
    42         }
    43 
    44         return 0;
    45 }
    46 
    47 int getChoice(void){
    48         int ch;
    49         printf("Enter the operation of your choice:
    ");
    50         printf("a. add          b. subtract
    ");
    51         printf("c. multiply     d.divide
    ");
    52         printf("q. quit
    ");
    53         ch=getFirst();
    54         while(ch!=97&&ch!=98&&ch!=99&&ch!=100&&ch!=113){
    55                 printf("Please enter a right choice:
    ");
    56                 ch=getFirst();
    57         }
    58 
    59 
    60 }
    61 
    62 int getFirst(void){
    63         int ch;
    64         ch=getchar();
    65         while(getchar()!=10)
    66                 return 0;
    67         return ch;
    68 }
    复制代码

    运行结果:

    Enter the operation of your choice:

    a. add          b. subtract

    c. multiply     d.divide

    q. quit

    f

    Please enter a right choice:

    d

    Enter first number:qqq

    qqq is not an number.

    Please enter a number.such as 2.5, -1.78E8, or 3:1

    Enter second number:0

    Enter a number other than 0:1

    1/1=1.0

  • 相关阅读:
    【洛谷P4557】【JSOI2018】—战争(Minkowski Sum)
    【洛谷P4557】【JSOI2018】—战争(Minkowski Sum)
    【BZOJ3879】—SvT(后缀自动机+虚树/后缀自动机+单调栈)
    【BZOJ3879】—SvT(后缀自动机+虚树/后缀自动机+单调栈)
    多测师讲解pyhon__hashlib_高级讲师肖sir
    多测师讲解python函数 _open_高级讲师肖sir
    多测师讲解python函数 _zip_高级讲师肖sir
    多测师讲解内置函数 _format_高级讲师肖sir
    多测师讲解python _函数return_高级讲师肖sir
    多测师讲解python _函数中变量_高级讲师肖sir
  • 原文地址:https://www.cnblogs.com/zhangyingai/p/7087386.html
Copyright © 2011-2022 走看看