zoukankan      html  css  js  c++  java
  • C Primer Plus_第8章_字符输入输出和输入确认_编程练习

    1.题略

    #include <stdio.h>
    
    int main(void)
    {
        int ch,i=0;
        printf("Please enter text here(end with Ctrl + Z):
    ");
        while (ch=getchar() != EOF)
            i++;
        printf("There are %d characters in text.
    ", i);
        return 0;
    }

    运行结果

    输入第一个Ctrl+Z时,并没有结束,下一行再输入Ctrl+Z才检测到EOF。说明我的控制台环境下,文件结尾形式是一行的开始位置Ctrl+Z,而不是任意位置的Ctrl+Z。

    2.题略

    /*  */
    #include <stdio.h>
    int main(void)
    {
        int i=0, ch;
        
        while ((ch = getchar()) != EOF)
        {
            if (ch == '
    ')
                printf("\n%3d  
    ",ch);    //换行符就是一个字符,同时它提示读入行缓冲区中的数据
            else if (ch == '	')
                printf("\t%3d  ",ch);
            else if ((ch < ' ') && (ch != '
    ') && (ch != '	'))
                printf("^%c%3d  ",ch+64,ch);
            else
                printf("%-2c%3d  ",ch,ch);
            i++;
            if(i%10 == 0)
                putchar('
    ');
        }
    }

    3.题略

    /*统计大小字母个数*/
    #include <stdio.h>
    #include <ctype.h>  //关联函数isupper(),islower()
    
    int main(void)
    {
        int count_up = 0, count_low = 0;
        int count_other = 0;
        int ch;
    
        printf("Please enter some text: 
    ");
        while ((ch = getchar()) != EOF)
        {
            if (isupper(ch))    //isupper(ch)函数:ch是大写字母的话,函数返回真值1
                count_up++;
            else if (islower(ch)) //islower(ch)函数:ch是小写字母的话,函数返回真值1
                count_low++;
            else
                count_other++;
        }
        printf("There are %d upper letters
    ", count_up);
        printf("and %d lower letters
    ", count_low);
        printf("and %d other letters
    ", count_other);
        return 0;
    }

    运行结果

    4.题略

    /*报告单词中的字母数*/
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        int CountLet = 0, CountWrd =0;
        int ch, ch_pre=' ';
    
        printf("Please enter some text (ctrl+z to quit): 
    ");
        while ((ch = getchar()) != EOF)
        {
            if (isalpha(ch))
                CountLet++;
            if ((isspace(ch) || ispunct(ch)) && isalnum(ch_pre))
                CountWrd++;
            ch_pre = ch;
        }
        printf("There are %d letters and %d words.
    ", CountLet, CountWrd);
        printf("There are %d letters in a word on average.
    ", CountLet / CountWrd);
    
        return 0;
    }

    运行结果

    5.题略

    /*问大小后再猜数*/
    #include <stdio.h>
    int main(void)
    {
        int min,max,mid,ch;
    
        printf("请输入被猜整数的范围:min(较小的数)和max(较大的数)
    ");
        scanf("%d%d",&min,&max);
        printf("min = %d, max = %d
    ",min, max);
        mid = min + (max-min)/2;
        printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
        while((ch = getchar ()) != 'y')
        {
            if (ch == 'b')
            {
                max = mid;
                mid = min + (max-min)/2;
                printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
            }
            else if (ch == 's')
            {
                min = mid;
                mid = min + (max-min)/2;
                printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
            }
            else
            {
                if (ch == '
    ')
                    continue;
                else
                    printf("Please just enter y, b, s.
    ");
            }
        }
        printf("the number is %d!
    ",mid);
    
        return 0; 
    }

    自己写的有点复杂了可能,不过运行起来还是可行的

    6.题略

    #include <stdio.h>
    #include <ctype.h>
    
    char get_first();
    
    int main(void)
    {
        printf("get_first() is %c", get_first());
    }
    
    char get_first()
    {
        int ch;
        printf("Please enter some words:
    ");
        while ((ch = getchar()) && (isspace(ch) == 0))
            continue;
        ch = getchar();
        return ch;
    }

    8.题略

    /**/
    #include<stdio.h>
    #include<ctype.h>
    float get_float(void);
    char get_first(void);
    
    
    int main(void)
    {
        char select;
        float num1,num2;
        while(1)
        {
            printf("Enter the operation of your choice:
    ");
            printf("a.add            s.subtract:
    ");
            printf("m.multiply       d.divide
    ");
            printf("q.quit
    ");
            select = get_first();
            if( select != 'a' && select != 's' && select != 'm' && select != 'd')
            {
                printf("Bye.
    ");
                break;
            }
            printf("Enter first number:");
            num1 = get_float();
            printf("Enter second number:");
            num2 = get_float();
            while( select == 'd' && num2 == 0) 
            {
                printf("Enter a number other than 0:");
                num2 = get_float();
            }
            switch(select)
            {
                case 'a': printf("%.2f + %.2f = %.2f
    ",num1, num2, num1 + num2); break;
                case 's': printf("%.2f - %.2f = %.2f
    ",num1, num2, num1 - num2); break;
                case 'm': printf("%.2f * %.2f = %.2f
    ",num1, num2, num1 * num2); break;
                case 'd': printf("%.2f / %.2f = %.2f
    ",num1, num2, num1 / num2); break;
                default : break;
            }
        }
        return(0);
    }
    
    float get_float(void) //得到一个合适的浮点数,滤除非法数
    {
        float num;
        char str[40];
        while(scanf("%f",&num)!=1)
        {
            gets(str);
            printf("%s is not a number.
    ",str);
            printf("Please enter a numbe, such as 2.5, -1.78E8, or 3:");
        }
        while ( getchar() != '
    ');
        return num;
    }
    
    char get_first(void) //得到字符串中的第一个字符,滤除其他字符
    {
        int ch;
        while( isspace( ch = getchar() ) );
        while ( getchar() != '
    ');
        return ch;
    }
  • 相关阅读:
    [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...
    <yii 框架学习> yii 框架改为中文提示
    Yii 语言设置 中文提示信息
    yii新手在实例化models(controller调用models实化化)php warning错误
    yii CFormModel中的rules验证机制
    神舟优雅系列和神舟精盾系列哪个好?
    response.sendRedirect跳转 jsp:forward跳转
    jsp post/get中接处理
    jsp动作之 forward
    JDK eclipse selenium的安装以及环境变量的配置
  • 原文地址:https://www.cnblogs.com/TomLily/p/5819384.html
Copyright © 2011-2022 走看看