• C Primer Plus 第9章 函数 编程练习


    复习题:

    8.
    int choice(int a,int b,int c){
        int max;
            max = a;
        if (b > max)
            max = b;
        if (c > max)
            max = c;
        return max;
    }
    9.
    #include <stdio.h>
    
    void menu(void);
    int choice(int low,int high);
    
    int main(void){
        menu();
        int ch = choice(1,4);
        switch (ch){
            case 1:printf("you choice is : copy files
    ");
                break;
            case 2:printf("you choice is : move files
    ");
                break;
            case 3:printf("you choice is : remove files
    ");
                break;
            case 4:break;
        }
        printf("Bye~");
    
        return 0;
    }
    
    void menu(void){
        printf("Please choose one of the following:
    ");
        printf("1) copy files        2) move files
    ");
        printf("3) remove files      4) quit
    ");
        printf("Enter the number of your choice:");
        return;
    }
    
    int choice(int low,int high){
        int ch;
        if (scanf("%d", &ch) != 1)
            return 4;
        while (ch > high || ch < low){
            menu();
            ch = choice(1,4);
        }
    
        return ch;
    }

    编程练习:

    1.
    #include <stdio.h>
    double min(double a, double b);
    
    int main(void){
        double x = 1.0;
        double y = 2.0;
        double c;
        c = min(1,2);
        printf("%.2f", c);
    }
    
    double min(double a, double b){
        return ( b < a ? b : a);
    }
    2.
    #include <stdio.h>
    void chline(char ch, int i, int j);
    
    int main(void){
        char ch = '*';
        int row = 5, col = 10;
        chline(ch,row,col);
        return 0;
    }
    
    void chline(char ch, int i, int j){
        for (int x = 0; x < i; ++x) {
            for (int y = 0; y < j; ++y) {
                putchar(ch);
            }
            putchar('
    ');
        }
    }
    3.
    #include <stdio.h>
    void chline(char ch, int i, int j);
    
    char character(void);
    int number(void);
    int main(void){
        char ch;
        int row, col;
        while ((ch = character()) != 'q'){
            printf("Please enter the number of lines to be printed:");
            row = number();
            printf("Please enter the number of columns to be printed:");
            col = number();
            chline(ch, row, col);
        }
        return 0;
    }
    
    void chline(char ch, int i, int j){
        for (int x = 0; x < i; ++x) {
            for (int y = 0; y < j; ++y) {
                putchar(ch);
            }
            putchar('
    ');
        }
    }
    
    char character(void){
        char ch;
        printf("Please enter the character you need print(q to quit):");
        while ((ch = getchar()) == '
    ')
            continue;
        while (getchar() != '
    ')
            continue;
        return ch;
    }
    
    int number(void){
        int num;
        char ch;
        while (scanf("%d", &num) != 1){
            while ((ch = getchar()) != '
    ') //处理错误输出***
                putchar(ch);
            printf("is not an number.
    ");
            printf("Please enter an number such as 3,5");
        }
        if (num <= 0){
            printf("Please enter the number than 0:");
            while ((ch = getchar()) != '
    ') //处理错误输出
                putchar(ch);
            num = number();
        }
        return num;
    }
    4.
    #include <stdio.h>
    double countdouwn_average(double num1, double num2);
    
    int main(void){
        double a = 10, b = 20, c;
        c = countdouwn_average(a,b);
        printf("%lf",c);
        return 0;
    }
    
    double countdouwn_average(double num1, double num2){
        double countdown1,countdown2,ctd_average;
        countdown1 = 1 / num1;
        countdown2 = 1 / num2;
        ctd_average = 1 / ((countdown1 + countdown2) / 2);
        return ctd_average;
    }
    5.
    #include <stdio.h>
    void larger_of(double * num1, double * num2);
    
    int main(void){
        double a = 10, b = 20;
        larger_of(&a,&b);
        printf("%.2lf  %.2lf", a, b);
        return 0;
    }
    void larger_of(double * num1, double * num2){
        double max;
        max = *num1 > *num2 ? *num1 : *num2;
        *num1 = max;
        *num2 = max;
        return;
    }
    6.
    #include <stdio.h>
    void larger_of(double * num1, double * num2, double * num3);
    
    int main(void){
        double a = 60, b = 39, c = 50;
        larger_of(&a,&b,&c);
        printf("%.2lf  %.2lf  %.2lf", a, b, c);
        return 0;
    }
    
    void larger_of(double * num1, double * num2, double * num3){
        double max,min,c;
        max = * num1;
        if (max < *num2)
            max = *num2;
        if (max < *num3)
            max = *num3;
        min = * num1;
        if (min > *num2)
            min = *num2;
        if (min > *num3)
            min = *num3;
        c = *num1 + *num2 + *num3 - min - max;
        *num1 = min;
        *num2 = c;
        *num3 = max;
    }
    7.
    #include <stdio.h>
    #include <ctype.h>
    int show_location(char ch);
    
    int main(void){
        char ch;
        printf("Please enter the charaster:");
        while ((ch = getchar()) != '
    '){  //ch = getchar()要框起来。
            printf("%c,%d
    ", ch,show_location(ch));
        }
    
        return 0;
    }
    //用isaplpa 和 toupper我是真不会。看了答案
    //发现答案真机智~
    int show_location(char ch){
        int result;
        if (isalpha(ch)){
            result = toupper(ch) - 'A' + 1;
        } else result = -1;
    
        return result;
    }
    8.
    #include <stdio.h>
    double power(double n, double p);
    
    int main(void){
        double a = 2,b = 2;
        printf("%lf", power(a,b));
        return 0;
    }
    
    double power(double n, double p){
        double pow = 1;
        int i;
        if (n == 0)
            return 0;
        if (p == 0)
            return 1;
        if (p > 0){
            for (i = 1;i <= p; i++) {
                pow = pow * n;
            }
            return pow;
        }
        if (p < 0){
            for (i = 1;i  <= -p ; i++) {
                pow = pow * n;
            }
            return 1 / pow;
        }
    }
    9.
    #include <stdio.h>
    double power(double n, double p);
    
    int main(void){
        double a = 2,b = -2;
        printf("%lf", power(a,b));
        return 0;
    }
    
    double power(double n, double p){
        double pow = 1;
        int i;
        if (n == 0)
            return 0;
        if (p == 0)
            return 1;
        if (p > 0){
            for (i = 1;i <= p; i++) {
                pow = pow * n;
            }
            return pow;
        }
        if (p < 0){
            return 1 / power(n, -p);
        }
    }
    10.
    #include <stdio.h>
    void to_binary(int a, int b);
    
    int main(void){
        int a,b;
        char ch;
        printf("Please enter an integer(q to quit):");
        while (scanf("%d", &a) == 1){
            printf("Please enter number base (2-10):");
            while (scanf("%d", &b) == 1 && b >= 2 && b <= 10){
                to_binary(a,b);
                putchar('
    ');
                break;
            }
            while ((ch = getchar()) != '
    '); //处理错误输入
            printf("Please enter an integer(q to quit):");
        }
        return 0;
    }
    
    void to_binary(int a, int b){
        int r;
        r = a % b;
        if (a > b)
            to_binary(a / b, b);
        printf("%d", r); //暂时不理解putchar('0' + r)是什么意思,所以就先用这个吧。
        return;
    }
  • 相关阅读:
    xcode12.2上运行平板IOS15.1
    发行版本崩溃,调试版本正常
    freetype安装使用
    码流的宽高和裁剪信息
    Xcode 沙盒选项
    【最新揭秘】百度快收权限如何获得,教你一周快速获得快收权限
    destoon提示http 403 forbidden解决方法
    他们口中的百度SEO快排发包参数到底是什么?
    destoon内容页调取相关内容标签
    destoon百度小程序主动推送插件
  • 原文地址:https://www.cnblogs.com/cccj/p/7667959.html
Copyright © 2020-2023  润新知