C Primer Plus_第9章_函数_编程练习

1.题略

/*返回较小值,设计驱动程序测试该函数*/
#include <stdio.h>
double min (double a, double b);

int main (void)
{
    double x, y;

    printf ("Please enter two numbers: \n");
    scanf ("%lf %lf", &x, &y);
    printf ("The smaller one is %lf\n", min (x, y));
    return 0;
}

double min (double a, double b)
{
    return (a < b) ? a: b;
}

2.题略

/**/
#include <stdio.h>
void chline (char, int, int);

int main (void)
{
    int i, j;
    char ch;

    printf ("Please enter a char and two int: \n");
    scanf ("%c %d %d", &ch, &i, &j);
    chline (ch, i, j);
    return 0;
}

void chline (char ch, int i, int j)
{
    int count;
    for (count = 1; count <= j; count++)
    {
        if (count < i)
            putchar (' ');
        if (count >= i && count <= j)
            putchar (ch);
        if (count < 0 || count > j)
            break;
    }
    putchar ('\n');
}

3.题略

/**/
#include <stdio.h>
void p_ch (char, int, int);

int main (void)
{
    int i, j;
    char ch;

    printf ("Please enter a char you like: \n");
    scanf ("%c", &ch);
    printf ("How many of them do you want to see in a line: \n");
    scanf ("%d", &i);
    printf ("How many lines do you want to have: \n");
    scanf ("%d", &j);
    p_ch (ch, i, j);
    printf ("Is it what you want? \n");
    return 0;
}

void p_ch (char ch, int cols, int rows)
{
    int i, j;
    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= cols; j++)
            putchar (ch);
        printf ("\n");
    }
}

4.题略

/**/
#include <stdio.h>
double calculate (double, double);

int main (void)
{
    double a, b;
    printf ("input two doubles: ");
    scanf ("%lf %lf", &a, &b);
    printf ("1 / ((1/x + 1/y) / 2) = %.3f\n", calculate (a, b));
    return 0;
}

double calculate (double x, double y)
{
    return 1 / ((1/x + 1/y) / 2);
}

5.题略

/**/
#include <stdio.h>
void larger_of (double *, double *);

int main (void)
{
    double a, b;
    printf ("Please enter two numbers: ");
    scanf ("%lf %lf", &a, &b);
    larger_of (&a, &b);
    printf ("Now the a is %.2f and b is %.2f", a, b);
    return 0;
}

void larger_of (double * x, double * y)
{
    *x = *y = (*x > *y) ? *x : *y;
}

6.题略

/**/
#include <stdio.h>
void Judge(int ch);

int main(void)
{
    int ch;

    printf("enter some txt to be analyzed(ctrl+z to end):\n");
    while ((ch = getchar()) != EOF)
        Judge(ch);
    printf("Done\n");
}

void Judge(int ch)
{
    if (ch>=65 && ch<=90) printf("%c is number %d\n", ch, ch-64);
    else if (ch>=97 && ch<=122) printf("%c is number %d\n", ch, ch-96);
    else printf("%c is not a letter\n",ch);
}

7.题略

#include <stdio.h>
double power(double n, int p);

int main (void)
{
    double Do;
    int In;

    printf("Please enter a double and a int(to calculate pow):\n");
    while (scanf("%lf %d",&Do, &In))
    {
        printf("%lf\'s %d mi is %lf\n", Do, In, power(Do, In));
    }
    printf("Done!\n");
}

double power(double n, int p)
{
    int i;
    double pow=1;

    if (n==0)
        return 0;

    if (p==0)
        return 1;
    else if (p > 0)
    {
        for (i=0; i<p; i++)
            pow*=n;
        return pow;
    }
    else if (p < 0)
    {
        for (i=0; i<(-p); i++)
            pow*=n;
        return  1/pow;
    }
}

8.题略

主函数与7题相同,关键是幂函数(递归形式),代码如下,还是挺考验逻辑的。

double power(double n, int p)
{
    double ans;

    if (p < 0)
    {
        ans = power(n,-p);
        ans = 1/ans;
    }
    else if (p > 0)
        ans = n * power(n,p-1);
    else if (p == 0)
        ans = 1;
    return ans;
}

9.题略

/*binary.c 以二进制形式输出整数*/
#include <stdio.h>
void Binary(int x, int y);

int main(void)
{
    int x, y;

    printf("Please enter a int (>=0)\n");
    while (scanf("%d %d", &x,&y) == 2)
    {   
        if (y>10 || y<2) continue;
        Binary(x,y);
        printf("\nPlease enter 2 int(q to quit):\n");
    }
    printf("Done!\n");
}

void Binary(int x, int y)
{
    int z;
    z = x % y;
    if (x/y != 0)
        Binary(x/y, y);
    printf("%d",z);
}

10.题略

关键点:利用循环的方式生成斐波那契数列,从下往上计算。即通过f(0) + f(1) 得到f(2), 再由f(1) + f(2)得到f(3)....直到计算出f(n),每次都必须从底层算起,然后三个变量相互赋值,迭代计算。

int Fibonacci( unsigned int n )
{
    unsigned int FibN, FibNOne, FibNTwo;
    unsigned int i;

    int result[2] = { 1, 1 };
    if( n < 3 )
        return result[n-1];

    FibNOne = 1;
    FibNTwo = 1;
    FibN = 0;
    for( i = 3; i <= n; i++ )
    {                                    /*以第一次循环执行过程为例*/
        FibN = FibNOne + FibNTwo;        /*f(2) = f(0) + f(1)*/
        FibNOne = FibNTwo;               /*f(1)*/
        FibNTwo = FibN;                  /*f(2)*/
    }
    return FibN;
}

 

posted @ 2016-08-31 08:35  行动救赎  阅读(323)  评论(0编辑  收藏  举报