1. 程式人生 > >明解C語言中級篇練習題第二章

明解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 待做