明解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	start,end;
	start = clock();
	for (i = 10; i > 0; i--) {		/* 倒数 */
		printf("\r%2d", i);
		printf("  时钟数 = %d",clock());
		fflush(stdout);
		sleep(1000);				/* 暂停1秒 */
	}
	printf("\r\aFIRE!!");
	printf("  时钟数 = %d", clock());
	end = clock();
	printf("\n程序开始运行后经过了%.1f秒。\n",
		(double)(end - start) / CLOCKS_PER_SEC);
	return 0;
}

练习2-2

#include <time.h>
#include <stdio.h>
#include <string.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;
}
//开头逐一显示字符
void gput(const char *s, int speed)
{
	int i;
	for (i = 0; i < strlen(s); i++)
	{
		printf("\r%c",s[i]);
		sleep(speed);
	}
	return;
}
int main(void)
{
	int a;
	printf("input 1 to start:");
	scanf("%d",&a);
	sleep(500);
	if(a==1)
		gput("ABC",100);
	return 0;
}

练习2-3

#include <time.h>
#include <stdio.h>
#include <string.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;
}
//闪烁显示字符串
void bput(const char *s, int d,int e,int n)
{
	while (n--)
	{
		printf("\r%s",s);
		sleep(d);
		printf("\r              ");
		sleep(e);
	}
	return;
}
int main(void)
{
	int a;
	printf("input 1 to start:");
	scanf("%d",&a);
	sleep(500);
	if(a==1)
		bput("Welcome!",200,300,5);
	printf("\n");
	return 0;
}

练习2-4

#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;
	int  cnt = 0;					/* 第几个字符在最前面 */
	//char name[] = "BohYoh ";		/* 要显示的字符串 */
	int  s_len = strlen(s);	/* 字符串name的字符数 */
	if (direction == 0)
	{
		while (n--)
		{
			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 (cnt < s_len - 1)
				cnt++;					/* 下次从后一个字符开始显示 */
			else
				cnt = 0;				/* 下次从最前面的字符开始显示 */
		}
	}
	else if (direction == 1)
	{
		while (n--)
		{
			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 (cnt > 0)
				cnt--;
			else
				cnt = s_len - 1;			/* 下次从最前面的字符开始显示 */
		}
	}
	return;
}
int main(void)
{
	char s[1024];
	int direction;
	int speed;
	int repeat_count;
	printf("input string:");
	scanf("%s",s);
	printf("input direction:");
	scanf("%d",&direction);
	printf("input speed,unit ms:");
	scanf("%d",&speed);
	printf("intput repeat count:");
	scanf("%d",&repeat_count);
	telop(s,direction,speed,repeat_count);
	printf("\n");
	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;		/* 开始时间·结束时间 */
	clock_t istart, iend;
	int clock_sum = 0;

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

	printf("扩大视野心算训练开始!!\n");
	start = clock();				/* 开始计算 */

	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的随机数  */
		printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
		do 
		{
			istart = clock();
			scanf("%d", &x);
			if (x == a + b + c)
			{
				iend = clock();
				printf("这道题用时%.1f秒\n",(double)(iend-istart)/CLOCKS_PER_SEC);
				clock_sum += iend - istart;
				break;
			}
				
			printf("\a回答错误。请重新输入:");
		} while (1);
	}
	end = clock();					/* 计算结束 */
	printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
	printf("平均每道题用时%.1f秒",(double)(clock_sum)/CLOCKS_PER_SEC);
	return 0;
}

练习2-6 待做


猜你喜欢

转载自blog.csdn.net/wofreeo/article/details/80733224