练习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. 查询安装webpack4.0是否成功时提示无法找到的解决方法

        最近使用webpack -v 查询webpack版本时提示无法找到         然后我试着重新全局安装webpack,提示还需要安装webpack-cli 选择yes后虽能成功安装webp ...

  2. 使用Vuejs 开发chrome 插件的注意事项

    chrome 插件的开发其实并不难,web开发者可以使用 html, css, javascript 轻松的开发实用的 chrome 插件. 一个好的 chrome 插件可以提高我们的开发效率,甚至方 ...

  3. 数据库——SQL-SERVER练习(4) 建表及数据完整性

    1. 输入下图的语句, 建立学生表STU. CREATE TABLE STU ( SNO NUMERIC() PRIMARY KEY, SNANE CHAR() NOT NULL, SSEX CHAR ...

  4. [IDA]系统注释给改掉

    IDA系统自动注释的,如果按 ';',则不会修改成功. 应该按 Shift + ;  这样才可以修改.

  5. 从程序集加载类型,遇到 ReflectionTypeLoadException 的处理办法

    处理办法 catch ReflectionTypeLoadException ,然后从里面读取 Types 数据(成功加载的类型)就可以了. 参考 ReflectionTypeLoadExceptio ...

  6. Docker是什么、为什么是一种趋势

    Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来.并且各种各样的货物被集装箱标准化了,集装箱和集装箱之间不会互相影响.那么我就不需要专门运送水果的船和专门运 ...

  7. UI设计中,文本底部添加下换线

    最近在开发项目,针对小程序前端,显示文本需要添加下划线问题,起初是通过border-bottom:1rpx solid #eee; 进行编写,但是总是觉得线的高度过于粗,移动端最小单位值是 1  即: ...

  8. python库的tkinter带你进入GUI世界(计算器简单功能)

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 一个处女座的程序猿 PS:如有需要Python学习资料的小伙伴可以加 ...

  9. ojdbc.jar在maven中报错(下载不了)

    一.问题 由于oracle的版权问题,java连接oracle的jar(ojdbc.jar)在maven的中央仓库下载不到,然后导致maven项目报错. 二.解决 第一步:下载ojdbc.jar 由于 ...

  10. 苹果 iOS13.2.2 正式版修复闷杀后台问题了?别担心,PerfDog 帮你来检测!

    导语 苹果于上周推送了iOS 13.2版本,带来了用户备受期待的图像处理系统深度融合(Deep Fusion),新增70多个表情.HomeKit安全视频.Siri隐私设置和支持AirPods Pro等 ...