C Primer Plus第九章课后答案

其他章节答案

/*Project 1*/
#include
double min(double,double);
int main(void)
{
    double x,y;
    printf("请输入一个double类型的值:");
    scanf("%lf",&x); /*double类型要用%lf*/
    printf("请再输入一个double类型的值:");
    scanf("%lf",&y);  
    printf("%g较小。",min(x,y)); 
    return 0;
 } 
 double min(double x,double y)
 {  
    return x > y ? y : x;
 }
/*Project 2、3*/
/*2题和3题一样*/
#include
int chline(char,int,int);
int main(void)
{
    char ch;
    int i,j;
    printf("请输入你想打印的字符: ");
    ch = getchar();
    printf("请输入你想打印的行数:") ;
    scanf("%d",&j);
    printf("请输入你想打印的列数:") ;
    scanf("%d",&i);
    chline(ch,i,j);
    return 0;
}
int chline(char ch,int i,int j)
{
    for(int m = 1; m <= j; m++)
    {
        for(int n = 1; n <= i; n++)
            printf("%c",ch);
        printf("\n");
    }
    return 0;
}
/*Project 4*/
#include
double tiaohe(double,double);
int main(void)
{
    double a,b;
    printf("请输入一个double类型的数字: ");
    scanf("%lf",&a);
    printf("请再输入一个double类型的数字: ");
    scanf("%lf",&b);
    printf("两数的调和平均数为:%g",tiaohe(a,b));
    return 0;
}
double tiaohe(double a,double b)
{
    return 1 / (((1 / a) + (1 / b)) / 2);
}
/*Project 5*/
#include
double larger_of(double,double);
int main(void)
{
    double a,b;
    printf("请输入一个double类型的数字: ");
    scanf("%lf",&a);
    printf("请再输入一个double类型的数字: ");
    scanf("%lf",&b);
    a = b = larger_of(a,b);
    printf("x = y = %g",a);
    return 0;
}
double larger_of(double x,double y)
{   
    return (x > y) ? x : y;
}
/*Project 6*/
#include
double larger_of(double*,double*,double*);
int main(void)
{
    double a,b,c;
    printf("请输入第一个double类型的数字: ");
    scanf("%lf",&a);
    printf("请输入第二个double类型的数字: ");
    scanf("%lf",&b);
    printf("请输入第三个double类型的数字: ");
    scanf("%lf",&c);
    larger_of(&a,&b,&c);
    return 0;
}
double larger_of(double* a,double* b,double* c)
/*声明变量时,“double* a”的意思就是“a是double类型变量的地址”,这时是把地址储存到了a里,而不是储存到了“*a”。*/
{   
    double m;
    m = *a;
    *a = ((*a < *b) ? *a : *b);
    *b = ((m > *b) ? m : *b);
    m = *b;
    *b = ((*b < *c) ? *b : *c);
    *c = ((m > *c) ? m : *c);
    m = *a;
    *a = ((*a < *c) ? *a : *c);
    *c = ((m > *c) ? m : *c);
    printf("%g %g %g",*a,*b,*c);
    return 0;
}
/*Project 7*/
#include
#include
int alpha(char);
int main(void)
{
    printf("请输入一个字符:");
    printf("这个字符的位置是:%d",alpha(getchar()));
    return 0;
 } 
 int alpha(char ch)
 {
    if(islower(ch))
        return ch - 'a' + 1;    //这个地方ch不能加单引号 
    else if (isupper(ch))
        return ch - 'A' + 1;
    else
        return -1;
 }
/*Project 8*/
#include
double power(double,int);
int main(void)
{
    double di;
    int mi;
    printf("请输入底数 整数幂数:"); 
    while(scanf("%lf %d",&di,&mi) == 2)
    {
        printf("%g的%d次幂为%g\n",di,mi,power(di,mi));
        printf("请再次输入底数 整数幂数:");
    }
    return 0;
 } 
 double power(double di,int mi)
 {
    double jieguo = 1;
    if(mi > 0)
    {
        for(int n = 1;n <= mi;n++)
            jieguo *= di;   //注意*和=的顺序不要弄反了
        return jieguo;
    }
    else if(mi < 0)
    {
        for(int n = 1;n <= -mi;n++)     //这里负号可以直接写在前面
            jieguo = jieguo * di;
        return 1 / jieguo;  
    }
    else
    {
        if(di == 0)
            printf("0的0次幂未定义,处理该值为1!\n");
        return 1;
    }
 }
/*Project 9*/
#include
double power(double,int);
int main(void)
{
    double di;
    int mi;
    printf("请输入底数 整数幂数:"); 
    while(scanf("%lf %d",&di,&mi) == 2)
    {
        printf("%g的%d次幂为%g\n",di,mi,power(di,mi));
        printf("请再次输入底数 整数幂数:");
    }
    return 0;
 } 
 double power(double di,int mi)
 {
    int i;
    double jieguo = 1;
    if(mi > 0)
    {
        for(i = 1;i <= mi;i++)
        jieguo *= di;
    }
    else if(mi < 0)
    {
        jieguo = 1 / power(di,-mi); //递归不一定是每次都递归,可以只调用一次
    }
    else
    {
        if(di == 0)
            printf("0的0次幂未定义,处理该值为1!\n");
        return 1;
    }
    return jieguo;
 }
/*Project 10*/
#include
#include
float to_base_n(int,int);
int main(void)
{
    int base,n;
    printf("请输入要转换进制的数:");
    scanf("%d",&base);
    printf("请输入进制:");
    scanf("%d",&n);
    printf("%d的%d进制数为:%g",base,n,to_base_n(base,n));
    return 0;
}
float to_base_n(int base,int n)
{
    int a,b;
    int i = 0;
    float s = 0;
    do
    {
        a = base / n;
        b = base % n;   //百分号是取余 ,不是整除 。
        //当base小于n时,余数是base本身,a为0. 
        s = b * pow(10,i) + s;
        i++;
        base = a;
    }
    while(a != 0);
    return s;
}
/*Project 11*/
#include
unsigned long Fibonacci(unsigned);
int main(void)
{
    unsigned n;
    printf("请输入n:");
    while(scanf("%u",&n))
    {
        printf("斐波那契数值为:%lu\n",Fibonacci(n));
        printf("请输入n:");
    }
    return 0;
}
unsigned long Fibonacci(unsigned n)
{
    unsigned long f;
    unsigned long f1 = 1;
    unsigned long f2 = 1;
    if(n > 2)
    {
        for(int i = 3;i <= n;i++)
        {
            f = f1 + f2;
            f1 = f2;
            f2 = f;
        }
        return f;
    }
    else
        return 1;
}

你可能感兴趣的:(C Primer Plus第九章课后答案)