明解C语言中级篇练习代码------第二章

这些天真的好忙啊,真的太讨厌早8的课了,特别是早8的外专课!(´-ι_-`)

练习2-1

//List 2-5 是一个用秒数来表示程序开始后经过的时间的程序。请改写程序,令其不仅能用秒数,还能用时钟数来表示时间。

#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秒。\n时钟数为%.1f。\n",
									  (double)c / CLOCKS_PER_SEC,(double)c);
	return 0;
}

练习2-2

//编写一个函数,令其能从字符串开头逐一显示字符。 void gput (const char *s, int speed);

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

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

    do {
        if ( (c2=clock())==(clock_t)-1 )           //(clock_t)-1 是 clock_t型的-1
            return 0;
    } while (1000.0*(c2-c1)/CLOCKS_PER_SEC < x);   //注意是CLOCKS不是CLOCK!!!

    return 1;
}

void gput (const char *s, int speed)
{
    int len=strlen(s);
    int i;

    for (i=0; i<len; i++) {
        putchar(s[i]);
        fflush(stdout);
        sleep(speed);
    }
}

int main(void)
{
    char a[]="Ice Bear Loves Isabel!";
    int speed = 500;

    gput(a,speed);

	return 0;
}

练习2-3

//编写一个闪烁显示字符串的函数。 void bput (const char *s, int d, int e, int n);

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

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;
}

void bput (const char *s, int d, int e, int n)
{
    int i,j;
    int len = strlen(s);

    for (i=0; i<n; i++) {
        printf("\r%s",s);          //一次性显示字符串,注意输出\r回到本行开头
        fflush(stdout);
        sleep(d);

        putchar('\r');             //一次性显示相等长度的空格覆盖,注意输出\r回到本行开头
        for (j=0; j<len; j++) {   
            printf(" ");
            fflush(stdout);
        }

        sleep(e);
    }
}

int main(void)
{
    char a[]="Ice Bear Loves Isabel!";
    int d = 100;
    int e = 100;
    int n = 100;

    bput(a,d,e,n);

	return 0;
}

练习2-4

//编写一个如字幕般显示字符串的函数。void telop (const char *s, int direction, int speed, int n);

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

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;
}

void telop (const char *s, int direction, int speed, int n)
{
    int i,num=0,cnt=0;
    int s_len = strlen(s);

    while(1) {
        putchar('\r');

        for (i=0; i < s_len; i++) {
            if (cnt+i < s_len)
                putchar(s[cnt+i]);
            else
                putchar(s[cnt+i-s_len]);
        }

        fflush(stdout);
        sleep(speed);

        if (direction == 0) {             //从右往左
            if(cnt < s_len-1)
                cnt ++;
            else {
                cnt = 0;
                num ++;
            }
        }
        else {                            //从左往右
            if(cnt > 0)
                cnt --;
            else {
                cnt = s_len-1;
                num ++;
            }
        }
        if (num > n)                     //num为计数变量,每次下标cnt归零是num++,num超过n时即跳出循环
            break;
    }
}

int main(void)
{
    char s[]="Ice Bears Wants To Sleep ";               //注意多加一个空格
    int direction;
    int speed = 500;
    int n = 5;

zmfx:
    printf("字幕滚动方向:0---从右往左 1---从左往右:");
    scanf("%d",&direction);
    if (direction != 0 && direction != 1) {
        printf("error!\n");
        goto zmfx;
    }

    telop(s,direction,speed,n);

	return 0;
}

练习2-5

//List 2-3 的“心算训练”程序显示的是进行10次加法运算所需要的时间。改写程序,令其能显示每次运算所需要的时间和运算的平均时间。

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

int main(void)
{
	int stage;
	int a, b, c;			/* 要进行加法运算的数值 */
	int x;					/* 已读取的值 */
	int n;					/* 空白的宽度 */
	clock_t	start, end;		/* 开始时间·结束时间 */
	float time1,time_sum=0;

	srand(time(NULL));		/* 设定随机数的种子 */

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

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

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

		do {
			scanf("%d", &x);
			if (x == a + b + c) {
                end = clock();
				break;
			}
			printf("\a回答错误。请重新输入:");
		} while (1);

		time1 = (double)(end - start) / CLOCKS_PER_SEC;
		time_sum += time1;
        printf("此次用时%.1f秒。\n\n",time1);
	}

    printf("恭喜通关,共用%.1f秒,平均每题%.1f秒。\n",time_sum, time_sum/10);

	return 0;
}

练习2-6

//把上面的程序改写成能进行加法和减法运算的程序,每次随机决定进行哪种运算。也就是说,假设三个值是a、b、c,每次都通过随机数来从下列组合中选一个进行出题。

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

int main(void)
{
	int stage;
	int a, b, c;
	int x;
	int n, m, answer;
	clock_t	start, end;
	float time1,time_sum=0;

	srand(time(NULL));

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

	for (stage = 0; stage < 10; stage++) {
		a = 10 + rand() % 90;
		b = 10 + rand() % 90;
		c = 10 + rand() % 90;
		n = rand() % 17;
		m = rand() % 4;           //生成0~3的随机数

		printf("\n%d\n",m);

		if (m == 0) {
            printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            answer = a+b+c;
		}
        else if (m == 1) {
            printf("%d%*s+%*s%d%*s-%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            answer = a+b-c;
		}
        else if (m == 2) {
            printf("%d%*s-%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            answer = a-b+c;
		}
        else {
            printf("%d%*s-%*s%d%*s-%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
            answer = a-b-c;
		}

		start = clock();

		do {
			scanf("%d", &x);
			if (x == answer ) {
                end = clock();
				break;
			}
			printf("\a回答错误。请重新输入:");
		} while (1);

		time1 = (double)(end - start) / CLOCKS_PER_SEC;
		time_sum += time1;
        printf("此次用时%.1f秒。\n\n",time1);
	}

    printf("恭喜通关,共用%.1f秒,平均每题%.1f秒。\n",time_sum, time_sum/10);

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值