明解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)-) /* 错误 */
return ;
} while (1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
return ;
} int main(void)
{
int i;
clock_t c; for (i = ; i > ; i--) { /* 倒数 */
printf("\r%2d", i);
fflush(stdout);
sleep(); /* 暂停1秒 */
}
printf("\r\aFIRE!!\n"); c = clock();
printf("程序开始运行后经过了%.1f秒。经过的时钟数是:%d\n",
(double)c / CLOCKS_PER_SEC,c);
return ;
}
练习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)-) /* 错误 */
return ;
} while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
return ;
} int main(void)
{
int i;
clock_t c;
char s[];
printf("输入字符串:");
scanf("%s", s);
gput(s, );
c = clock();
printf("程序开始运行后经过了%.2f秒。经过的时钟数是:%d\n",
(double) c / CLOCKS_PER_SEC, c);
return ;
}
练习2-3
/* 倒计时后显示程序运行时间 */ #include <time.h>
#include <stdio.h> /*--- 等待x毫秒 ---*/
void bput(const char* s,int d,int e,int n){
while (n>)
{
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)-) /* 错误 */
return ;
} while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
return ;
} int main(void)
{
int i;
clock_t c;
char s[];
printf("输入字符串:"); scanf("%s", s);
bput(s, ,,);
return ;
}
练习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 = ;
int i = ;
while (n > ) {
putchar('\r');
for (i = ; 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 == ) {
if (cnt > )
cnt--;
else
cnt = name_len - ;
}
else {
if (cnt < name_len - )
cnt++;
else
cnt = ;
}
n--;
} }
int sleep(unsigned long x)
{
clock_t c1 = clock(), c2; do {
if ((c2 = clock()) == (clock_t)-) /* 错误 */
return ;
} while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
return ;
} int main(void)
{
int choice;
clock_t c;
char s[];
printf("输入字符串:");
scanf("%s", s);
printf("选择从右向左----0.从左向右----1\n");
scanf("%d", &choice);
telop(s, choice,,);
return ;
}
练习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[];
double sum = ;
int i = ;
srand(time(NULL)); /* 设定随机数的种子 */ printf("扩大视野心算训练开始!!\n"); for (stage = ; stage < ; stage++) {
start = clock();
a = + rand() % ; /* 生成10~99的随机数 */
b = + rand() % ; /* 〃 */
c = + rand() % ; /* 〃 */
n = rand() % ; /* 生成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 ();
end = clock(); /* 计算结束 */
Time[stage]=((double)(end - start) / CLOCKS_PER_SEC);
sum += Time[stage];
}
for ( i = ; i < stage; i++)
{
printf("第%d次运算用了%.1f秒\n", i+, Time[i]); }
printf("平均时间是%.1f秒", sum / ); return ;
}
练习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[];
double sum = ;
int i = ;
int y = ;;
srand(time(NULL)); /* 设定随机数的种子 */ printf("扩大视野心算训练开始!!\n"); for (stage = ; stage < ; stage++) {
start = clock();
a = + rand() % ; /* 生成10~99的随机数 */
b = + rand() % ; /* 〃 */
c = + rand() % ; /* 〃 */
n = rand() % ; /* 生成0~16的随机数 */
choice = rand() % ;
if (choice==) {
printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
y = a + b + c;
}
if (choice == ) {
printf("%d%*s+%*s%d%*s-%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
y =a + b - c;
}
if (choice == ) {
printf("%d%*s-%*s%d%*s+%*s%d:", a, n, "", n, "", b, n, "", n, "", c);
y = a - b + c;
}
if (choice == ) {
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 ();
end = clock(); /* 计算结束 */
Time[stage]=((double)(end - start) / CLOCKS_PER_SEC);
sum += Time[stage];
}
for ( i = ; i < stage; i++)
{
printf("第%d次运算用了%.1f秒\n", i+, Time[i]); }
printf("平均时间是%.1f秒", sum / ); return ;
}
明解C语言 中级篇 第二章答案的更多相关文章
- 明解C语言 入门篇 第二章答案
练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...
- 明解C语言 中级篇 第一章答案
练习1-1 #include <stdio.h> #include<stdlib.h> int main() { srand(time()); ; printf("您 ...
- 明解C语言 入门篇 第一章答案
练习1-1 #include <stdio.h> int main() { int a; a = 15; int b; b = 37; int c; c = a - b; printf(& ...
- 明解C语言 入门篇 第九章答案
练习9-1 /* 将字符串存储在数组中并显示(其2:初始化) */ #include <stdio.h> int main(void) { char str[] = "ABC\0 ...
- 明解C语言 中级篇 第四章答案
练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...
- 明解C语言 中级篇 第三章答案
练习3-1 /* 猜拳游戏(其四:分割函数/显示成绩)*/ #include <time.h> #include <stdio.h> #include <stdlib.h ...
- 明解C语言 入门篇 第八章答案
练习8-1 #include<stdio.h> #define diff(x,y)(x-y) int main() { int x; int y; printf("x=" ...
- 明解C语言 入门篇 第十章答案
练习10-1 #include <stdio.h> void adjust_point(int*n) { ) *n = ; ) *n = 0; } int main() { int x; ...
- 明解C语言 入门篇 第五章答案
练习5-1 /* 依次把1.2.3.4.5 赋值给数组的每个元素并显示(使用for语句) */ #include <stdio.h> int main(void) { int i; ]; ...
随机推荐
- 数据库——SQL SERVER Transact-SQL 程序设计
什么是Transact-SQL? 标准SQL不支持过程化控制, 不能完成复杂的功能.T-SQL是过程化SQL语言, 是SQL的扩展 增加了过程化语句 (变量,赋值,分支,循环...)是数据库服务器 ...
- cmd命令详解
这几天用了一下Windows系统的“黑框”,即win+R键,发现有些命令都忘了,还得查,就总结了一下: cmd命令 CMD命令:开始->运行->键入cmd或command(在命令行里可以看 ...
- ASP.NET Core基于K8S的微服务电商案例实践--学习笔记
摘要 一个完整的电商项目微服务的实践过程,从选型.业务设计.架构设计到开发过程管理.以及上线运维的完整过程总结与剖析. 讲师介绍 产品需求介绍 纯线上商城 线上线下一体化 跨行业 跨商业模式 从0开始 ...
- FileFilter(),文件过滤器操作
package seday03; import java.io.File;import java.io.FileFilter; /*** listFiles的重载方法允许我们传入一个文件过滤器:Fil ...
- centOS服务器安装mongodb
1.为服务器添加mongodb的包管理工具,这就相当于在windows中安装npm,以便能用npm安装各种依赖.添加了这个包管理工具,才能在后面对mongodb做一系列操作. touch /etc/y ...
- iOS安全攻防(一):Hack必备的命令与工具
转自:http://blog.csdn.net/yiyaaixuexi/article/details/8288077 你的应用正在被其他对手反向工程.跟踪和操作!你的应用是否依旧裸奔豪不防御? 郑重 ...
- 执行插件的替代方式:用JS调用操作
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复229或者20161028可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
- Gatech OMSCS的申请和学习之奥妙
https://zhuanlan.zhihu.com/p/54680585 我写东西一向希望能给大家带来正能量,提供有价值的信息,不辱没母校的厚德价值观. 我是传统没落工科毕业后开发软件,但是一直没下 ...
- ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6)
ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6) APPLIES TO: Oracle Databa ...
- [Linux] 解决nginx: [emerg] directive "rewrite" is not terminated by ";"
解决nginx: [emerg] directive "rewrite" is not terminated by ";"nginx的rewite规则有时候没注 ...