Lesson18 PoEdu培训第二课 C语言篇(10) 运算符的使用 随堂作业
文章类别: 培训作业 0 评论

Lesson18 PoEdu培训第二课 C语言篇(10) 运算符的使用 随堂作业

文章类别: 培训作业 0 评论

老师未批改

1. 假设所有的变量都为 int 类型, 请写出下列各项变量中的值:

y = x = (2 + 3) / 4;

x = 1;
y = 1;

y = 4 8 / (xx = 2 8);

xx = 16;
y = 2;

x = (int)3.3 + 3.111;

x = 6;

y = 30.0 / 4.0 * 5.0;

y = 37;

x = 30 / 4.0 * 5;

x = 37;

2. 请写出程序打印出来的值

int num = 10;
printf("%d", ++num);
printf("%d", num++);
printf("%d", ++num);
printf("%d", num--);
printf("%d", num);
11, 11, 13, 13, 12

3. 请写出下面程序的打印出来的结果:

#include <stdio.h>
#define LENGTH 10

int main() {
    int num = 0;
    while(num++ < LENGTH) {
        printf("%5d", num);
    }
    return 0;
}
// 使用_代替空格进行表示
____1
____2
____3
____4
____5
____6
____7
____8
____9
___10

4. 编程题

1). 让用户不停的输入分钟, 程序自动换算为小时分钟的表现形式(不能出现魔数).当用户输入0或小于0时程序结束.

/*************************************************************************
    > 文件名: lesson18.hw.01.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-21 23:37:24
 ************************************************************************/

#include <stdio.h>

#define BASE_CONVERT 60
#define TRUE 1
#define EXIT_VAL 0

int main() {
    
    int iInputMins = 0;

    do {
        printf("请输入分钟数(输入<=0的数退出):\n");
        scanf("%d", &iInputMins);
        if (iInputMins <= EXIT_VAL) {
            break;
        }
        printf("您输入的是[%.2g]小时, [%d]分钟!\n", 
                (double)iInputMins / BASE_CONVERT, iInputMins);
        iInputMins = 0;
    } while (TRUE);
    return 0;
}

运行效果:

Alt 运行效果1

2). 让用户不停的输入天数, 程序自动换算为周数加天数的表现形式(不能出现魔数).当用户输入0或者小于0时程序结束.

/*************************************************************************
    > 文件名: lesson18.hw.02.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-21 23:47:41
 ************************************************************************/

#include <stdio.h>

#define WEEK_TO_DAY 7
#define TRUE 1
#define EXIT_VAL 0

int main() {
    
    int iInputDays = 0;

    do {
        printf("请输入天数(输入<=0的数退出):\n");
        scanf("%d", &iInputDays);
        if (iInputDays <= EXIT_VAL) {
            break;
        }
        printf("您输入的是[%.2g]周, [%d]天!\n", 
                (double)iInputDays / WEEK_TO_DAY, iInputDays);
        iInputDays = 0;
    } while (TRUE);
    return 0;
}

运行效果:

Alt 运行效果2

3). 请写出一个程序, 要求用户输入:

        总里程(公里)
        跑过的距离(公里)
        跑步用时(分钟)
        请计算并显示出跑完全程还需要的时间(小时: 分钟: 秒:)
/*************************************************************************
    > 文件名: lesson18.hw.03.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-21 23:53:39
 ************************************************************************/

#include <stdio.h>

#define BASE_CONVERT 60

int main() {
    
    int iAllLens = 0, iPassLens = 0, iPassMins = 0;
    printf("请依次输入总里程(公里), 已跑距离(公里), 跑步用时(分钟):\n");
    scanf("%d%d%d", &iAllLens, &iPassLens, &iPassMins);

    double dSpeed = (double)(iPassLens / iPassMins);
    double dNeedMins = (double)(iAllLens - iPassLens) / dSpeed;
    printf("跑完全程还需要[%.2g]小时, [%.2g]分钟, [%d]秒.\n",
            dNeedMins / BASE_CONVERT, dNeedMins, (int)(dNeedMins * BASE_CONVERT));

    return 0;
}

运行效果:

Alt 运行效果3

4). 看故事写程序

    传说西塔发明了国际象棋而使国王十分高兴, 他决定要重赏西塔.西塔说:"我不要你的重赏, 陛下,
    只要你在我的棋盘上赏一些麦子就行了.
    在棋盘的第一个格子里放1粒, 在第二个格子里放2粒, 在第三个格子里放4粒, 在第四个格子里放8粒,
    以此类推, 以后每个格子里放的麦粒数都是前一个格子里放的麦粒数的两倍, 直到放满第64个格子就行了".
    "区区小数, 几粒麦子, 这有何难, 来人!". 国王令人如数付给西塔.
    请计算最后国王需要给西塔多少粒小麦, 一共有多少吨.(假设1粒=1克)
/*************************************************************************
    > 文件名: lesson18.hw.04.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-22 0:13:58
 ************************************************************************/

#include <stdio.h>
#include <math.h>

#define T_TO_G 10000000

int main() {
    int iGird = 0;
    double dSum = 0.0;

    for (iGird = 0; iGird < 64; iGird++) {
        dSum += pow(2, iGird);
    }
    printf("共有[%.0lf]粒米, 共有[%lf]吨.\n", dSum, (double)(dSum / T_TO_G));
    return 0;
}

运行效果:

Alt 运行效果4

如有错误,请提出指正!谢谢.

回复