明解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; ]; ...
随机推荐
- 用户和登录的ID、Name和SID
SQL Server的安全主体主要分为Login.User和Role,不仅有ID属性,还有Name属性和SID属性,SID是指Security ID.在查看用户和登录的时候,受到模拟上下文的影响.当执 ...
- python基础(10):文件操作
1. 初识文件操作 使⽤python来读写⽂件是非常简单的操作.我们使⽤open()函数来打开⼀个⽂件,获取到⽂ 件句柄,然后通过⽂件句柄就可以进⾏各种各样的操作了,根据打开⽅式的不同能够执⾏的操 作 ...
- Qt Creator单步调试快捷键F10经常失灵问题
使用Qt Creator调试程序的时候经常会遇到F10单步调试快捷键不响应的问题. 打开调试菜单如下:有两个快捷键为F10的调试菜单项,于是快捷键冲突了! 解决办法:废话不说,直接上图 由于Start ...
- System 类初探
System 类 操作方法 取得当前的系统时间 currentTemiMillis() public static long currenTimeMillis() ; 实例: 统计某些操作的执行时间 ...
- DDoS攻击工具
DDoS攻击工具 综合性工具 综合性工具除了可以进行DDoS攻击外,还可用于其他的用途,例如:端口扫描.安全审计.防火墙等.实际上,大部分综合性工具开发的原始目的并不是用于DDoS,而是"网 ...
- java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
请求验证码时后台报错:java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment ...
- 区块链社交APP协议分析预告
2017年,比特币的火热,直接导致了代币市场的繁荣: 2018年,作为信用体系的未来解决方案,区块链引发了互联网原住民的淘金热. 作为风口上的引流神器,区块链技术与社交网络结合起来,产生了一系列区块链 ...
- Python—虚拟环境的创建与管理(virtualenv与virtualenvwrapper)
virtualenv 1.安装virtualenv包 [root@localhost ~]# pip install virtualenv 2.创建虚拟环境,位置不固定,可以在项目的目录下,也可以不在 ...
- Linxu:磁盘分区
了解磁盘分区 磁盘的物理组成: 圆形的磁盘盘(主要记录数据的部分): 机械手臂,与在机械手臂上的磁盘读取头(可擦写磁盘盘上的数据): 主轴马达,可以转动磁盘盘,让机械手臂的读取头在磁盘盘上读写数据. ...
- zip unzip 压缩解压缩命令
直接上例子: mkdir test1 touch test1/1.txt touch test1/2.txt zip -r test1.zip test1 #-r 参数是包含文件夹下的文件 un ...