天天看點

C Primer Plus(第六版)第9章 程式設計練習答案

題目字太多了略🤣有問題歡迎留言讨論~

IDE: Xcode

9-1

#include <stdio.h>

double min(double x, double y)
{
    if(x < y) return x;
    else return y;
}

int main()
{
    double a, b;
    printf("please enter 2 double number:");
    scanf("%lf %lf", &a, &b);
    //scanf當中double必須是%lf,而printf當中可以用%f也可以用%lf
    printf("the min of these number is %lf\n", min(a,b));
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-2

#include <stdio.h>

void chline(char ch, int i, int j)
{
    for(int row = 1; row <= j; row++){
        for(int column = 1; column <= i ; column++){
            printf("%c", ch);
        }
        printf("\n");
    }
}

int main()
{
    chline('*', 3, 5);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-3

#include <stdio.h>

void print_char(char ch, int times, int row)
{
    for(int i = 1; i <= row; i++){
        if(i == row){
            for(int j = 1; j <= times ; j++){
                printf("%c", ch);
            }
        }
        printf("\n");
    }
}

int main()
{
    print_char('x', 7, 3);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-4

#include <stdio.h>

double harmonic_average(double a, double b){
    double har_avg;
    har_avg = 1 / ((1 / a + 1 / b) / 2);
    return har_avg;
}

int main()
{
    printf("%lf\n", harmonic_average(2, 3));
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-5

#include <stdio.h>

void lager_of(double *x, double *y)
{
    if(*x > *y){
        *y = *x;
    }else{
        *x = *y;
    }
}

int main()
{
    double a, b;
    scanf("%lf %lf", &a, &b);
    lager_of(&a, &b);
    printf("a=%f b=%f\n", a, b);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-6

#include <stdio.h>
void sort(double *x, double *y, double *z)
{
    double temp;
    if(*x > *y) {
        temp = *x;
        *x = *y;
        *y = temp;
    }
    if(*y > *z){ //将最大的值賦給z
        temp = *y;
        *y = *z;
        *z = temp;
    }
    if(*x > *y) { //再把較大的值賦給y,最簡單的冒泡排序想法
        temp = *x;
        *x = *y;
        *y = temp;
    }
}

int main()
{
    double a, b, c;
    scanf("%lf %lf %lf", &a, &b, &c);
    sort(&a, &b, &c);
    printf("a = %f, b = %f, c = %f\n", a, b, c);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-7

#include <stdio.h>

int letter_place(char letter);

int main()
{
    char c;
    int place;
    while((c = getchar()) && c != EOF){ //檔案結尾,ctrl+d兩次
    //while((scanf("%c", &c)) && c != EOF){ //Xcode這樣寫就會return error code,為什麼?
        place = letter_place(c);
        if(place == -1)
            printf("\n%c is not a letter.", c);
        else
            printf("\n%c is a letter at No.%d in the alphabet.", c, place);
    }
    printf("\nEnd.");
    return 0;
}

int letter_place(char letter)
{
    int location;
    if(letter >= 'a' && letter <= 'z'){
        location = letter - 'a' + 1;
        return location;
    }else if(letter >= 'A' && letter <= 'Z'){
        location = letter - 'A' + 1;
        return location;
    }else{
        return -1;
    }
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-8

#include <stdio.h>

double power(double n, int p) //n的p次方
{
    double pow = 1;
    int i;
    int signal = 1;
    if(n == 0 && p == 0){
        printf("0 to the 0 is undefined,so the value is treated as 1.\n");
        return 1;
    }else if(n == 0){
        return 0;
    }else if(p == 0){
        return 1;
    }else if(p < 0){
        signal = -1;
    }

    for(i = 0; i < p * signal; i++){ //若處理負幂,先按照正幂求值
        pow *= n;
    }

    if(signal == -1) //再求倒數
        pow = 1 / pow;

    return pow;
}

int main()
{
    double a, b;
    int n;
    scanf("%lf %d", &a, &n);
    b = power(a, n);
    printf("%f to the %d is %f\n", a, n, b);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-9

#include <stdio.h>

double power(double n, int p) //n的p次方
{
    double pow = 1;
    int i;
    int signal = 1;
    if(n == 0 && p == 0){
        printf("0 to the 0 is undefined,so the value is treated as 1.\n");
        return 1;
    }else if(n == 0){
        return 0;
    }else if(p == 0){
        return 1;
    }else if(p < 0){
        signal = -1;
    }

    pow = n * power(n, p * signal - 1);

    return pow;
}

int main()
{
    double a, b;
    int n;
    scanf("%lf %d", &a, &n);
    if(n > 0)
        b = power(a, n);
    else{
        b = power(a, -n);
        b = 1 / b;
    }
    printf("%f to the %d is %f\n", a, n, b);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-10

#include <stdio.h>

void to_base_n(int n, int system)
{
    int r;

    if(system > 10 || system < 2){
        printf("Wrong system.\n");
        return;
    }

    r = n % system;
    if(n >= system)
        to_base_n(n / system, system);

    printf("%d", r);
    return;
}


int main()
{
    int a, base;
    scanf("%d %d", &a, &base);
    printf("Convert %d to base %d is ", a, base);
    to_base_n(a, base);
    printf(".\n");
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

9-11

#include <stdio.h>

int Fibonacci(int n)
{
    int i;
    int fib = 0;
    int fib_a = 0, fib_b = 0;
    for (i = 1; i <= n; i ++){
        if(i == 1 || i == 2){
            fib = 1;
        }else{
            fib = fib_a + fib_b; //f(n)=f(n-1)+f(n-2)
        }
        fib_a = fib_b; //f(n-2)
        fib_b = fib; //f(n-1)
        printf("f(%d)=%d ", i, fib);
    }
    printf("\n");
    return fib;
}

int main()
{
    int n;
    int fn;
    scanf("%d", &n);
    fn = Fibonacci(n);
    printf("f(%d)=%d\n", n, fn);
    return 0;
}
           

Output:

C Primer Plus(第六版)第9章 程式設計練習答案

繼續閱讀