练习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. Appium+java --连接模拟器画面倒过来的问题

    引用文章:https://blog.csdn.net/testerYu/article/details/90024049 工具 夜神模拟器 appium 现象 通过代码运行截图操作,结果全是倒着的如下 ...

  2. RPA中房产证的 OCR 识别

    客户需求,识别一些证件内容,包括身份证.户口本.营业执照.银行卡以及房产证,前四个比较容易实现,不管是艺赛旗的 RPA 还是百度的 OCR 都有接口,直接调用即可,但是都没有房产证的 OCR 识别,只 ...

  3. Python 如何操作微信

    1.给文件传输助手发一条消息 import itchat itchat.auto_login(enableCmdQR=True) # 这里需要你人工手机扫码登录 itchat.send('Hello, ...

  4. [转]在.NET Core 2.x中将多个强类型设置实例与命名选项一起使用

    自1.0版之前,ASP.NET Core已使用“ 选项”模式配置强类型设置对象.从那时起,该功能获得了更多功能.例如,引入了ASP.NET Core 1.1 IOptionsSnapshot,它允许您 ...

  5. .NET Core CSharp初级篇 1-2 循环与判断

    .NET Core CSharp初级篇 1-2 本节内容循环与判断 循环 循环是一个在任何语言都是极为重要的语法,它可以用于很多东西,例如迭代数组等等.在C#中,语法层面的循环有:for , fore ...

  6. 关于yield和yield from

    一.简单示例 def yield_func(): for _ in range(2): yield "12" def yield_from_func(): for _ in ran ...

  7. PHP通过session判断防止表单重复提交实例

    PHP通过session判断防止表单重复提交实例,当用户提交表单后,为防止重复操作,通过session来判断是否为初次提交,否则让他返回到之前表单页面. 当前表单页面is_submit设为0 SESS ...

  8. Java自学-I/O 数据流

    Java 数据流DataInputStream ,DataOutputStream DataInputStream 数据输入流 DataOutputStream 数据输出流 步骤 1 : 直接进行字符 ...

  9. Initialize a Property After Creating an Object创建对象后初始化属性 即如何设置对象的默认值(EF)

    In this lesson, you will learn how to set the default value for a particular property of a business ...

  10. webpack代码分离CommonsChunkPlugin插件的使用(防止重复)

    1.webpack.config.js中添加: const path = require('path'); + const webpack = require('webpack'); const HT ...