明解C语言 中级篇 第二章答案

练习2-1

/* 倒计时后显示程序运行时间 */

#include <time.h>
#include <stdio.h>

/*--- 等待x毫秒 ---*/
int sleep(unsigned long x)
{
    clock_t c1 = clock(), c2;

    do {
        if ((c2 = clock()) == (clock_t)-1)    /* 错误 */
            return 0;
    } while (1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}

int main(void)
{
    int     i;
    clock_t    c;

    for (i = 10; i > 0; i--) {        /* 倒数 */
        printf("\r%2d", i);
        fflush(stdout);
        sleep(1000);                /* 暂停1秒 */
    }
    printf("\r\aFIRE!!\n");

    c = clock();
    printf("程序开始运行后经过了%.1f秒。经过的时钟数是:%d\n",
        (double)c / CLOCKS_PER_SEC,c);
    return 0;
}

练习2-2

/* 倒计时后显示程序运行时间 */

#include <time.h>
#include <stdio.h>

/*--- 等待x毫秒 ---*/
void gput(const char* s, int speed){
    while (*s) {
        putchar(*s);
        fflush(stdout);
        sleep(speed);
        *(s++);
    }

}
int sleep(unsigned long x)
{
    clock_t c1 = clock(), c2;

    do {
        if ((c2 = clock()) == (clock_t)-1)    /* 错误 */
            return 0;
    } while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}

int main(void)
{
    int     i;
    clock_t    c;
    char s[128];
    printf("输入字符串:");
    scanf("%s", s);
    gput(s, 100);                 
    c = clock();
    printf("程序开始运行后经过了%.2f秒。经过的时钟数是:%d\n",
        (double) c / CLOCKS_PER_SEC, c);
    return 0;
}

练习2-3

/* 倒计时后显示程序运行时间 */

#include <time.h>
#include <stdio.h>

/*--- 等待x毫秒 ---*/
void bput(const char* s,int d,int e,int n){
    while (n>0)
    {
        printf("%s", s);
        fflush(stdout);
        sleep(d);
        printf("\r                            ");
        sleep(e);
        printf("\r"); 
        n--;
    }

}
int sleep(unsigned long x)
{
    clock_t c1 = clock(), c2;

    do {
        if ((c2 = clock()) == (clock_t)-1)    /* 错误 */
            return 0;
    } while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}

int main(void)
{
    int     i;
    clock_t    c;
    char s[128];
    printf("输入字符串:");

    scanf("%s", s);
    bput(s, 1000,1000,3);
    return 0;
}

练习2-4

/* 倒计时后显示程序运行时间 */

#include <time.h>
#include <stdio.h>

/*--- 等待x毫秒 ---*/
void telop(const char* s,int direction, int speed, int n) {
    int name_len = strlen(s);
    int cnt = 0;
    int i = 0;
    while (n > 0) {
        putchar('\r');
        for (i = 0; i < name_len; i++) {
            if (cnt + i < name_len)
                putchar(s[cnt + i]);
            else
                putchar(s[cnt + i - name_len]);
        }
        fflush(stdout);
        sleep(speed);
        if (direction == 1) {
            if (cnt > 0)
                cnt--;
            else
                cnt = name_len - 1;
        }
        else {
            if (cnt < name_len - 1)
                cnt++;
            else
                cnt = 0;
        }
        n--;
    }

    
}
int sleep(unsigned long x)
{
    clock_t c1 = clock(), c2;

    do {
        if ((c2 = clock()) == (clock_t)-1)    /* 错误 */
            return 0;
    } while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}


int main(void)
{
    int choice;
    clock_t    c;
    char s[128];
    printf("输入字符串:");
    scanf("%s", s);
    printf("选择从右向左----0.从左向右----1\n");
    scanf("%d", &choice);
    telop(s, choice,1000,3);
    return 0;
}

练习2-5

/* 同时训练扩大水平方向视野的心算训练 */

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int stage;
    int a, b, c;            /* 要进行加法运算的数值 */
    int x;                    /* 已读取的值 */
    int n;                    /* 空白的宽度 */
    clock_t    start, end;        /* 开始时间·结束时间 */
    double Time[11];
    double sum = 0;
    int i = 0;
    srand(time(NULL));        /* 设定随机数的种子 */

    printf("扩大视野心算训练开始!!\n");

    for (stage = 0; stage < 10; stage++) {
        start = clock();
        a = 10 + rand() % 10;        /* 生成10~99的随机数 */
        b = 10 + rand() % 10;        /*     〃     */
        c = 10 + rand() % 10;        /*     〃     */
        n = rand() % 17;            /* 生成0~16的随机数  */

        printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);

        do {
            scanf("%d", &x);
            if (x == a + b + c)
                break;
            printf("\a回答错误。请重新输入:");
        } while (1);
        end = clock();                    /* 计算结束 */
         Time[stage]=((double)(end - start) / CLOCKS_PER_SEC);
         sum += Time[stage];
    }
    for ( i = 0; i < stage; i++)
    {
        printf("第%d次运算用了%.1f秒\n", i+1, Time[i]);

    }
    printf("平均时间是%.1f秒", sum / 10);
        

    return 0;
}

练习2-6

/* 同时训练扩大水平方向视野的心算训练 */

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int stage;
    int a, b, c,choice;            /* 要进行加法运算的数值 */
    int x;                    /* 已读取的值 */
    int n;                    /* 空白的宽度 */
    clock_t    start, end;        /* 开始时间·结束时间 */
    double Time[11];
    double sum = 0;
    int i = 0;
    int y = 0;;
    srand(time(NULL));        /* 设定随机数的种子 */


    printf("扩大视野心算训练开始!!\n");

    for (stage = 0; stage < 10; stage++) {
        start = clock();
        a = 10 + rand() % 10;        /* 生成10~99的随机数 */
        b = 10 + rand() % 10;        /*     〃     */
        c = 10 + rand() % 10;        /*     〃     */
        n = rand() % 17;            /* 生成0~16的随机数  */
        choice = rand() % 4;
        if (choice==0) {
            printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            y = a + b + c;
        }
        if (choice == 1) {
            printf("%d%*s+%*s%d%*s-%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            y =a + b - c;
        }
        if (choice == 2) {
            printf("%d%*s-%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            y = a - b + c;
        }
        if (choice == 3) {
            printf("%d%*s-%*s%d%*s-%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            y = a - b - c;
        }

        do {
            scanf("%d", &x);
            if (x == y)
                break;
            printf("\a回答错误。请重新输入:");
        } while (1);
        end = clock();                    /* 计算结束 */
         Time[stage]=((double)(end - start) / CLOCKS_PER_SEC);
         sum += Time[stage];
    }
    for ( i = 0; i < stage; i++)
    {
        printf("第%d次运算用了%.1f秒\n", i+1, Time[i]);

    }
    printf("平均时间是%.1f秒", sum / 10);
        

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/nightswatch-candle/p/11980525.html