《c primer plus》编程练习回顾-第九章

1.

#include<stdio.h>
double min(double a, double b);
int main(void)
{
    double x, y;
    scanf("%lf%lf", &x, &y);
    printf("%lf", min(x, y));
    return 0;
}
double min(double a, double b)
{
    return a < b ? a : b;
}

2.

#include<stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
    char ch;
    int a, b;
    scanf("%c%d%d", &ch, &a, &b);
    chline(ch, a, b);
    return 0;
}
void chline(char ch, int i, int j)
{
    for (int k = 1; k < i; k++)
        printf(" ");
    for (; i <= j; i++)
        printf("%c", ch);
}

3

#include<stdio.h>
void fun(char ch, int a, int b);
int main(void)
{
    char ch;
    int x, y;
    scanf("%c%d%d", &ch, &x, &y);
    fun(ch, x, y);
    return 0;
}
void fun(char ch, int a, int b)
{
    for (int i = 0; i < a; i++)
    {
        for (int i = 0; i < b; i++)
            printf("%c", ch);
        printf("\n");
    }
}

4.

#include<stdio.h>
double fun(double a, double b);
int main(void)
{
    double x, y;
    scanf("%lf%lf", &x, &y);
    printf("%lf", fun(x, y));
    return 0;
}
double fun(double a, double b)
{
    double c;
    c = 1 / ((1 / a + 1 / b) / 2);
    return c;
}

5.

#include<stdio.h>
void larger_of(double *a, double *b);
int main(void)
{
    double x, y;
    scanf("%lf%lf", &x, &y);
    larger_of(&x, &y);
    printf("%lf %lf\n", x, y);
    return 0;
}
void larger_of(double *a, double *b)
{
    *a = *b = *a > *b ? *a : *b;
}

6.

#include<stdio.h>
#include<ctype.h>
int fun(char ch);
int main(void)
{
    char ch;
    scanf("%c", &ch);
    printf("%c\'s position is %d\n", ch, fun(ch));
    return 0;
}
int fun(char ch)
{
    int a;
    char ch1;
    if (isalpha(ch))
    {
        printf("%c is a alpha.\n", ch);
        ch1 = toupper(ch);
        a = ch1 - 65 + 1;
        return a;
    }
    else
        return -1;
}

7.

#include<stdio.h>
#include<math.h>
double power(double x, int y);
int main(void)
{
    double a;
    int b;
    while (scanf("%lf%d", &a, &b) == 2)
    {
        printf("%lf\n", power(a, b));
    }
    return 0;
}
double power(double x, int y)
{
    double c = 1;
    if (y > 0)
    {
        for (int i = 0; i < y; i++)
            c *= x;
        return c;
    }
    else if (y < 0)
    {
        for (int i = 0; i < -y; i++)
            c /= x;
        return c;
    }
    else if (x != 0)
        return 1;
    else
        return 1 / x;

}

8.

*

9.

这里写代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值