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

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秒。\n",
		(double)c / CLOCKS_PER_SEC);
	printf("程序开始运行后经过了%d时%d分%.1f秒。\n",
		(int)((double)c / CLOCKS_PER_SEC) / 360,
		(int)((double)c / CLOCKS_PER_SEC) / 60 % 60,
		(double)c / CLOCKS_PER_SEC);
	return 0;
}

2

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <time.h>
void sleep(unsigned long int 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 qput(const char *s, int speed)
{
	char a;
	while ((a = *s++) != '\0')
	{
		printf("%c", a);
		sleep(speed);
	}
}
int main()
{
	char arr[100];
	scanf("%s", arr);
	qput(arr, 100);
}

3

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <time.h>
void sleep(unsigned long int 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;
	for (i = 0; i < n;i++)
	{
    printf("%s", s);
	sleep(d);
	printf("\r                                                            \r");
	sleep(e);
	}
}
int main()
{
	char arr[100];
	scanf("%s", arr);
	bput(arr,1000,1000,5);
}

 4

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
void sleep(int 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)
{
	char arr[100];
	int i, m, k = 0, l = 0;
	while (*(s + k) != '\0')
		k++;
	if (direction)
	{
		for (i = 0; i < n; i++)
		{
			for (m = 0; m < k; m++)
			{
				printf("%c", s[l%k]);
				l++;
			}
			sleep(speed);
			printf("\r                                                      \r");
			l++;
		}
	}
	else
	{
		for (i = 0; i < k; i++)
		arr[i] = s[i];
		for (i = 0; i < n; i++)
		{
			for (i = 0; i < k; i++)
				printf("%c", arr[i]);
			for (m = 0; m < k; m++)
			{
				arr[k - m] = arr[k - m - 1];
			}
			arr[0] = arr[k];

			sleep(speed);
			printf("\r                                                      \r");
		}
	}
}
int main()
{
	char arr[100];
	scanf("%s", arr);
	telop(arr,0,1000,50);
}

5

#define _CRT_SECURE_NO_WARNINGS 1
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

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

	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)
				break;
			printf("\a回答错误。请重新输入:");
		} while (1);
			end = clock();
			arr[stage] = (end-start) / CLOCKS_PER_SEC;
	}

				/* 计算结束 */
	for (stage = 0; stage < 10; stage++)
	{
      printf("第%d次 用时%.1f秒。\n", stage+1,arr[stage]);
	  arr[10] += arr[stage];
	}
	printf("每道题平均用时%f秒\n", arr[10] / 10);
	return 0;
}

6

#define _CRT_SECURE_NO_WARNINGS 1
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

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

	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();
		switch (rand()%4)
		{
		case 0:
			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);
			break;
		case 1:
			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);
			break;
		case 2:
			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);
			break;
		case 3:
			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);
			break;
		}
		end = clock();
		arr[stage] = (end - start) / CLOCKS_PER_SEC;
	}

	/* 计算结束 */
	for (stage = 0; stage < 10; stage++)
	{
		printf("第%d次 用时%.1f秒。\n", stage + 1, arr[stage]);
		arr[10] += arr[stage];
	}
	printf("每道题平均用时%f秒\n", arr[10] / 10);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值