练习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语言 中级篇 第二章答案的更多相关文章

  1. 明解C语言 入门篇 第二章答案

    练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...

  2. 明解C语言 中级篇 第一章答案

    练习1-1 #include <stdio.h> #include<stdlib.h> int main() { srand(time()); ; printf("您 ...

  3. 明解C语言 入门篇 第一章答案

    练习1-1 #include <stdio.h> int main() { int a; a = 15; int b; b = 37; int c; c = a - b; printf(& ...

  4. 明解C语言 入门篇 第九章答案

    练习9-1 /* 将字符串存储在数组中并显示(其2:初始化) */ #include <stdio.h> int main(void) { char str[] = "ABC\0 ...

  5. 明解C语言 中级篇 第四章答案

    练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...

  6. 明解C语言 中级篇 第三章答案

    练习3-1 /* 猜拳游戏(其四:分割函数/显示成绩)*/ #include <time.h> #include <stdio.h> #include <stdlib.h ...

  7. 明解C语言 入门篇 第八章答案

    练习8-1 #include<stdio.h> #define diff(x,y)(x-y) int main() { int x; int y; printf("x=" ...

  8. 明解C语言 入门篇 第十章答案

    练习10-1 #include <stdio.h> void adjust_point(int*n) { ) *n = ; ) *n = 0; } int main() { int x; ...

  9. 明解C语言 入门篇 第五章答案

    练习5-1 /* 依次把1.2.3.4.5 赋值给数组的每个元素并显示(使用for语句) */ #include <stdio.h> int main(void) { int i; ]; ...

随机推荐

  1. 【Linux命令】常用系统工作命令11个(echo、date、reboot、poweroff、wget、ps、top、pidof、kill、killall、pkill)

    目录 echo命令 date命令 reboot命令 poweroff命令 wget命令 ps命令 top命令 pidof命令 kill命令 killall命令 pkill命令 一.echo命令 ech ...

  2. pytest框架优化——清理历史截图图片和allure报告文件

    痛点分析: 当我们每次执行完用例的时候,如果出现bug或者是测试脚本出了问题,一般会通过测试报告.异常截图.日志来定位分析,但是我们发现运行次数多了之后,异常截图和测试报告会不停地增多,对我们定位分析 ...

  3. 一、Mybatis配置详解

    Mybatis配置详解 XML配置文件层次结构 下图展示了mybatis-config.xml的全部配置元素 properties元素 properties是一个配置属性的元素,让我们能在配置文件的上 ...

  4. Linux 下编写一个 PHP 扩展

        假设需求 开发一个叫做 helloWord 的扩展. 扩展里有一个函数,helloWord(). echo helloWord('Tom'); //返回:Hello World: Tom 本地 ...

  5. linux 安装 nvm, node.js, npm

    vscode在wsl中开发node应用,如何安装nvm? git clone git@github.com:nvm-sh/nvm.git ~/.nvm 设置淘宝registry npm config ...

  6. hello tensorflow,我的第一个tensorflow程序

    上代码: import tensorflow as tf if __name__=='__main__': g = tf.Graph() # add ops to the user created g ...

  7. Redis设置密码,保护数据安全

    1.设置密码方法 在 redis.conf 的配置文件中修改参数 requirepass 的值为需要设置的密码, 保存配置文件后,重启Redis就可以. 建议 设置比较复杂和长的密码,防止被暴力破解. ...

  8. CAD总记不住?设计达人给你支招,最强口诀40条玩转设计

    绘图界有这样一个准则:绘图越快,玩的越6 相反的,CAD玩的很6 ,你的绘图效率一定不会差到哪里去,虽然不能说的太绝对,但你就操作如果玩转,一定你就操作能给你的绘图带来很多效率的提升. 当然后面就你就 ...

  9. RPM包安装——手动安装

    RPM包安装 手动安装 挂载光盘 首先查看光盘是否挂载(使用mount命令) RPM包存放位置 在你光盘挂载点的Packages目录下 使用 ll | grep 关键字 可以快速找到你想要的RPM包 ...

  10. 从HTML开始

    <html>:做网页,是一种超文本标记语言. 超文本:既有添加文本的能力,还可以添加图片,视频等多媒体元素. 标记:由标签组成.不同的标签有不同的效果.        开始标签,结束标签. ...