1. 程式人生 > >C primer plus 程式設計練習 9.11

C primer plus 程式設計練習 9.11

1.

#include <stdio.h>
double min(double, double);
int main(void)
{
    double x, y;
    printf("Enter two numbers (q to quit): ");
    while (scanf("%lf %lf", &x, &y) == 2)
    {
        printf("The smaller number is %f.\n", min(x,y));
        printf("Next two values (q to quit): ");
    }
    printf("Bye!\n");
    return 0;
}
double min(double a, double b)
{
    return a < b ? a : b;
}

3.

#include <stdio.h>
void chLineRow(char ch, int c, int r);
int main(void)
{
    char ch;
    int col, row;
    printf("Enter a character (# to quit): ");
    while ( (ch = getchar()) != '#')
    {
        if (ch == '\n')
            continue;
        printf("Enter number of columns and number of rows: ");
        if (scanf("%d %d", &col, &row) != 2)
            break;
        chLineRow(ch, col, row);
        printf("\nEnter next character (# to quit): ");
    }
    printf("Bye!\n");
    return 0;
}
// start rows and cols at 0
void chLineRow(char ch, int c, int r)
{
    int col, row;
    for (row = 0; row < r ; row++)
    {
        for (col = 0; col < c; col++)
            putchar(ch);
        putchar('\n');
    }
    return;
}

4.

#include <stdio.h>
double avg_two(double,double);

int main(void)
{
	double num1,num2;
	printf("enter two number: \n");
    while (scanf("%lf %lf",&num1,&num2) == 2)
    {
        printf("the answer is: %0.2lf\n",avg_two(num1,num2));
        printf("enter the next two number: \n");
    }
    printf("BYE!");
	return 0;
}
double avg_two(double a,double b)
{
	return 1 / ((1 / a + 1 / b) / 2);
}

5.

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

int main(void)
{
	double x,y;
	printf("enter two number: \n");
    while (scanf("%lf %lf",&x,&y) == 2)
    {
		printf("now,the x is %0.2lf,y is %0.2lf\n",x,y);
		larger_of(&x,&y);
		printf("now,the x is %0.2lf,y is %0.2lf\n",x,y);
        printf("enter the next two number: \n");
    }
    printf("BYE!");
	return 0;
}
void larger_of(double * a,double * b)
{
	if (*a > *b)
		*b = *a;
	else
		*a = *b;
}
// alternatively:
/*
void larger_of(double *a, double *b)
{
*a= *b = *a > *b ? *a : *b;
}
*/

6.

#include <stdio.h>
void rank_num(double *a,double *b,double *c);

int main(void)
{
	double x,y,z;
	printf("enter three numbers: \n");
    while (scanf("%lf %lf %lf",&x,&y,&z) == 3)
    {
		printf("The numbers you entered is: %0.2lf\t %0.2lf\t %0.2lf\t\n",x,y,z);
		rank_num(&x,&y,&z);
		printf("Now,the number you entered is: %0.2lf\t %0.2lf\t %0.2lf\t\n",x,y,z);
        printf("enter the next three numbers: \n");
    }
    printf("BYE!");
	return 0;
}
void rank_num(double * a,double * b,double * c)
{
	double num[3] = {*a,*b,*c};
	double temp;
	int i,j;
	for (i=0;i < 3;i++)
	{
		for (j = 0;j < 3 - i -1;j++)
		{
            if (num[j] > num [j+1])
            {
				temp = num[j];
				num[j] = num[j+1];
				num[j+1] = temp;
            }
		}
	}
	*a = num[0];
	*b = num[1];
	*c = num[2];
}
#include <stdio.h>
void rank_num(double *px,double *py,double *pz);

int main(void)
{
	double x,y,z;
	printf("enter three numbers: \n");
    while (scanf("%lf %lf %lf",&x,&y,&z) == 3)
    {
		printf("The numbers you entered is: %0.2lf\t %0.2lf\t %0.2lf\t\n",x,y,z);
		rank_num(&x,&y,&z);
		printf("Now,the number you entered is: %0.2lf\t %0.2lf\t %0.2lf\t\n",x,y,z);
        printf("enter the next three numbers: \n");
    }
    printf("BYE!");
	return 0;
}
void rank_num(double * px,double * py,double * pz)
{
    double temp;
	if (*px > *py)
	{
		temp = *px;
		*px = *py;
		*py = temp;
	}
	if (*pz < *px)
	{
		temp = *pz;
		*pz = *py;
		*py = *px;
		*px = temp;		
	}
	else if (*pz < *py)
	{
		temp = *pz;
		*pz = *py;
		*py = temp;
	}
}

7.

#include <stdio.h>
#include <ctype.h>
int juge_alpha(char ch);

int main(void)
{
	char ch;
	while ((ch = getchar()) != EOF)
	{
		printf("%d ",juge_alpha(ch));
	}

	return 0;
}
int juge_alpha(char ch)
{
	if (isalpha(ch))
	{
		toupper(ch);
		return ch - 'a' + 1;
	}
	else
	    return -1;
}

8.

#include <stdio.h>
double power(double a, int b); /* ANSI prototype */
int main(void)
{
    double x, xpow;
    int n;
    printf("Enter a number and the integer power");
    printf(" to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while (scanf("%lf%d", &x, &n) == 2)
    {
        xpow = power(x,n); /* function call */
        printf("%.3g to the power %d is %.5g\n", x, n, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");
    return 0;
}
double power(double a, int b) /* function definition */
{
    double pow = 1;
    int i;
    if (b == 0)
    {
        if (a == 0)
            printf("0 to the 0 undefined; using 1 as the value\n");
        pow = 1.0;
    }
    else if (a == 0)
        pow = 0.0;
    else if (b > 0)
        for(i = 1; i <= b; i++)
            pow *= a;
    else /* b < 0 */
        pow = 1.0 / power(a, - b);
    return pow; /* return the value of pow */
}

9.

/* Programming Exercise 9-10 */
#include <stdio.h>
void to_base_n(int x, int base);
int main(void)
{
    int number;
    int b;
    int count;
    printf("Enter an integer (q to quit):\n");
    while (scanf("%d", &number) == 1)
    {
        printf("Enter number base (2-10): ");
        while ((count = scanf("%d", &b))== 1 && (b < 2 || b > 10))
        {
            printf("base should be in the range 2-10: ");
        }
        if (count != 1)
        break;
        printf("Base %d equivalent: ", b);
        to_base_n(number, b);
        putchar('\n');
        printf("Enter an integer (q to quit):\n");
    }
    printf("Done.\n");
    return 0;
}
void to_base_n(int x, int base) /* recursive function */
{
    int r;
    r = x % base;
    if (x >= base)
        to_base_n(x / base, base);
    putchar('0' + r);
    return;
}

11.

#include <stdio.h>
unsigned long Fibonacci(unsigned n);

int main(void)
{
	unsigned n;
	printf("enter a interger:\n");
	while (scanf("%u",&n) == 1)
	{
        printf("the Fibonacci is:%u\n",Fibonacci(n));
        printf("enter the next interger:\n");
	}
    printf("bye\n");
    return 0;
}
unsigned long Fibonacci(unsigned n)
{
    int ans = 1;
    int ans_pre = 1;
    int i;
    if (n <= 2)
    {
        return 1;
    }
    else 
         for (i = 0;i < n - 2 ;i++ )
         {
             ans = ans + ans_pre;
             ans_pre = ans - ans_pre;
          }
    return ans;	
}