C语言练习题(10)

1、编写程序:读入一个在字母C和X之间的字符,打印出该字母在中间的相邻五个字母。
如:输入F,则输出DEFGH.
函数原型:void func(char ch)

#include <stdio.h>
void func(char ch)
{
    printf("%c%c%c%c%c", ch - 2, ch - 1, ch, ch + 1, ch + 2);
}

int main(void)
{
    char f;
    printf("Input a character:");
    scanf("%c", &f);
    func(f);

    return 0;
}

2、一个球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第十次反弹多高。

#include <stdio.h>
int main(void)
{
    float l = 100;//只需要计算反弹高度,经过的高度是其两倍
    int i = 1;
    for (i = 1; i <= 10; i++)
        l /= 2;

    printf("10th:经过%.5f米,反弹%.5f米。\n", 2*l, l);

    return 0;
}

3、编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。
函数原型:PS:故意这么写的,别给乱换
void show_time(int *year, int *month, int *date, int *hour, int *minute, int *second)

#include <stdio.h>

int is_leap_year(int year)//四年一闰,百年不闰, 四百年再闰
{
    if ((year % 400) == 0)
        return 1;
    else if ((year % 4 == 0) && (year % 100 != 0))
            return 1;

    return 0;
}

int is_last_day(int year,int month,int day)//判断是不是该月的最后一天
{
    if (month == 2)
    {
        year = is_leap_year(year);
        if (year == 1)
        {
            if (day == 29)
                return 1;
            else
                return 0;
        }
        else
        {
            if (day == 28)
                return 1;
            else
                return 0;
        }
    }

    else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
        if (day == 31)
            return 1;
        else
            return 0;
    }

    else
        if (day == 30)
            return 1;
        else
            return 0;
}

void show_time(int *year, int *month, int *date, int *hour, int *minute, int *second)
{
    if ((*second + 1) == 60)
    {
        *second = 0;
        if ((*minute + 1) == 60)
        {
            *minute = 0;
            if ((*hour + 1) == 24)
            {
                *hour = 0;
                if (is_last_day(*year, *month, *date) == 1)
                {
                    *date = 1;
                    if (*month == 12)
                    {
                        *month = 1;
                        *year += 1;
                    }
                    else
                        *month += 1;
                }
                else
                    *date += 1;
            }
            else
                *hour += 1;
        }
        else
            *minute += 1;
    }
    else
        *second += 1;

    printf("%d %d %d %d %d %d\n", *year, *month, *date, *hour, *minute, *second);
}

int main(void)
{
    int year=0, month=0, date=0, hour=0, minute=0, second=0;

    printf("Input data:");
    scanf("%d %d %d %d %d %d", &year, &month, &date, &hour, &minute, &second);

    show_time(&year, &month, &date, &hour, &minute, &second);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值