zoukankan      html  css  js  c++  java
  • C Primer Plus 第8章 字符输入/输出和验证输入 编程练习

    1.
    #include <stdio.h>
    int main(){
        char ch;
        int ct = 0;
        while ((ch=getchar()) != EOF)
            ct++;
        printf("%d characters read.", ct);
        return 0;
    }
    2.
    #include <stdio.h>
    int main(){
        char ch;
        int ct = 0;
        while ((ch = getchar()) != EOF)
        {
            if (ch >= 32){
                putchar(ch);
                printf("/%d  ",ch);
                ct++;
            }
            else if (ch == '
    '){
                printf("\n");
                putchar(ch);
                ct = 0;  //因为要换新的一行了所以ct清0
            }
            else if (ch == '	'){
    
                printf("\t");
                ct++;
            }
            else  //打印控制字符
            {
                putchar('^');
                putchar(ch + 64);
                printf("/%d  ", ch);
            }
            if (ct == 10){
                printf("
    ");
                ct = 0;
            }
        }
        return 0;
    }
    3.
    #include <stdio.h>
    #include <ctype.h>
    int main(){
        char ch;
        int a,b,c;
        a = b = c = 0;
        while ((ch = getchar()) != '#')
        {
            if (isupper(ch))
                a++;
            else if (islower(ch))
                b++;
            else c++;
        }
        printf("capital:%d lower case:%d other:%d", a, b, c);
        return 0;
    }
    4.
    #include <stdio.h>
    #include <ctype.h> //用来区分字母和字母的大小写
    #include <stdbool.h>
    int main(){
        char ch;
        bool inword = false;
        int n_chars = 0;
        int n_words = 0;
        int value;
        while ((ch = getchar()) != '#')
        {
            if (isalpha(ch))
                n_chars++;
            if (!isspace(ch) && !inword)
            {
                inword = true; //开始一个新的单词
                n_words++; //单词加1
            }
            if (isspace(ch) && inword)
                inword = false; //单词结束
        }
        value = n_chars / n_words ;
        printf("chars : %d
    ", n_chars);
        printf("words : %d
    ", n_words);
        printf("average value : %d", value);
        return 0;
    }
    5.
    #include <stdio.h>
    //如果50太大,则50变为最大值。反之则为最小值。
    int main(){
        char ch;
        int guess = 50, MAX = 100, MIN = 0;
        printf("Uh...is your number is %d
    ",guess);
        while ((ch = getchar()) != 'y')
        {
            if (ch == 'b') {
                MAX = guess;
                guess = (guess + MIN) / 2;
                printf("Well, then, is it %d
    ",guess);
            }
            else if (ch == 'l') {
                MIN = guess;
                guess = (guess + MAX) / 2;
                printf("Well, then, is it %d
    ",guess);
            }
            else printf("Sorry, I understand only big , less , yes.
    ");
            while ((ch = getchar()) != '
    ')
                continue;
        }
        printf("I konw i could do it");
        return 0;
    }
    6.
    //重要!!
    char get_first(void){
        char ch;
        while((ch = getchar()) == '
    ')
            continue;    
        while (getchar() != '
    ')
            continue;
        return ch;
    }
    7.
    #include <stdio.h>
    char get_choice(void);
    char get_first(void);
    #define WORK_OVERTIME 40
    #define RATE1 0.15
    #define RATE2 0.20
    #define RATE3 0.25
    #define BREAK1 300.0
    #define BREAK2 450.0
    #define BASE1 (BREAK1 * RATE1)
    #define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))
    int main(void) {
        int hr;
        char choice;
        double wage,income,tax,wage2;
        printf("------------------------------------------------------------------------
    ");
        printf("Enter the number number corresponding to the desired pay rate or action:
    ");
        printf("a) $8.15/hr                       b) $9.33/hr
    ");
        printf("c) $10.00/hr                      d) $11.20/hr
    ");
        printf("q) quit
    ");
        printf("------------------------------------------------------------------------
    ");
        printf("Please enter your choise:");
        while (choice = get_choice() ){
            if (choice == 'q')
                break;
            switch (choice){
                case 'a': wage2 = 8.75;
                    break;
                case 'b': wage2 = 9.33;
                    break;
                case 'c': wage2 = 10.00;
                    break;
                case 'd': wage2 = 11.20;
                    break;
            }
            printf("Please enter the hours:");
            scanf("%d",&hr);
            if (hr <= WORK_OVERTIME)
                wage = hr * wage2;
            else wage = WORK_OVERTIME * wage2 +((hr - WORK_OVERTIME) * wage2 * 1.5);
            if (wage <= BREAK1){
                tax = wage * RATE1;
            }
            else if (wage > 300 && wage <= 450 ){
                tax = BASE1 + (wage - BREAK1) * RATE2;
            }
            else tax = BASE2 + (wage - BREAK2) * RATE3;
            income = wage - tax;
            printf("The wage: %.2f income:%.2lf tax:%.2lf
    ", wage, income, tax);
            printf("------------------------------------------------------------------------
    ");
            printf("Enter the number number corresponding to the desired pay rate or action:
    ");
            printf("1) $8.15/hr                       2) $9.33/hr
    ");
            printf("3) $10.00/hr                      4) $11.20/hr
    ");
            printf("5) quit
    ");
            printf("------------------------------------------------------------------------
    ");
            printf("Please enter your choise:");
        }
        return 0;
    }
    //get_choice 输入正确的符号
    char get_choice(void){
        char ch;
        ch = get_first();
        while (ch != 'q' && (ch < 'a' || ch > 'd')){
            printf("Please enter the : a b c d q
    ");
            printf("Please enter your choise:");
            ch = get_first();
        }
        return  ch;
    }
    //get_first 读取第一个符号 扔掉其他符号
    char get_first(void){
        int ch;
        ch = getchar();
        while (getchar() != '
    ')
            continue;
        return ch;
    }
    8.
    #include <stdio.h>
    
    char get_choice(void);
    char get_first(void);
    double get_number(void);
    
    int main(void){
        char choice;
        double num1,num2,result;
    
        while ((choice = get_choice()) != 'q'){
            printf("Enter the first number:");
            num1 = get_number();
            printf("Enter the second number:");
            num2 = get_number();
            if (choice == 'a') {
                result = num1 + num2;
                printf("%.1lf + %.1lf = %.1lf
    ", num1, num2, result);
            }
            if (choice == 's') {
                result = num1 - num2;
                printf("%.1f - %.1f = %.1f
    ", num1, num2, result);
            }
            if (choice == 'm') {
                result = num1 * num2;
                printf("%.1f * %.1f = %.1f
    ", num1, num2, result);
            }
            if (choice == 'd') {
                if (num2 == 0) {
                    printf("Enter a number other than 0: ");
                    num2 = get_number();
                    result = num1 / num2;
                    printf("%.1f / %.1f = %.1f
    ",  num1, num2, result);
                } else
                    printf("%.1f / %.1f = %.1f
    ", num1, num2, result);
            }
        }
    
        return 0;
    }
    
    char get_choice(void){
        char ch;
        printf("Enter the operation of your choice:
    ");
        printf("a. add            s. subtract
    ");
        printf("m. multiply       d. divide
    ");
        printf("q. quit
    ");
        ch = get_first();
        while ((ch != 'a') && (ch != 's') && (ch != 'm') && (ch != 'd') && (ch != 'q')){
            printf("Please choice such as a s m d q:");
            ch = get_first();
        }
        return ch;
    }
    
    char get_first(void){
        char ch;
        while((ch = getchar()) == '
    ')//ch = getchar();之前用的的是这个,结果总是把回车给ch
            continue;    //还总是检查不出来问题,终于找出来问题在哪里了。!!
        while (getchar() != '
    ')
            continue;
        return ch;
    }
    
    double get_number(void){
        double num;
        char ch;
        while (scanf("%lf", &num) != 1){
            while ((ch = getchar()) != '
    ')
                putchar(ch); //处理错误的输入。
            printf("is not an number.
    ");
            printf("Please enter a number , such as 2.5, -1.78E8, or 3:");
        }
        return num;
    }
  • 相关阅读:
    Web 设计与开发终极资源大全(下)
    任务失败,原因是未找到“LC.exe”,或未安装正确的 Microsoft Windows SDK
    NET开发常用DLL资源下载
    sqlserver2005 sqlserver2000连接字符串的区别(NET)
    创业大讲座观后感
    工欲善其事必先利其器搭建Android平台
    Java学习很好的笔记
    Java调用jama实现矩阵运算
    MySQL的安装及使用教程
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/cccj/p/7667960.html
Copyright © 2011-2022 走看看