Chapter2 专注于显示
//2-1输出\a和\n #include<stdio.h> int main(){ printf("你好。\n初次见面。\n"); printf("\a警告。\n\n"); printf("\a\a这次是第二次警告。\n"); return 0; }
//2-2\b使用实例,每个1秒消去一个字符 #include<time.h> #include<stdio.h> int sleep(unsigned long x){ //等待x毫秒 clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ int i; printf("ABCDEFG"); for(i = 0; i < 7; i++){ sleep(1000); printf("\b \b"); fflush(stdout); //清空缓存 } return 0; }
//2-3\r回车符的使用示例:重写行 #include<time.h> #include<stdio.h> int sleep(unsigned long x){ //等待x毫秒 clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ printf("My name is BohYoh."); fflush(stdout); sleep(2000); printf("\rHow do you do? "); fflush(stdout); sleep(2000); printf("\rThanks. "); return 0; }
//2-4\'\"使用示例 #include<stdio.h> int main(){ printf("关于字符串常量和字符常量。\n"); printf("双引号"); putchar('"'); printf("用双引号括起来的\"ABC\"是字符串常量。\n"); printf("单引号"); putchar('\''); printf("用双引号括起来的'A'是字符常量。\n"); return 0; }
//2-5倒计时后显示程序运行的时间 #include<time.h> #include<stdio.h> int sleep(unsigned long x){ clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ int i; clock_t c; for(i = 10; i > 0; i--){ printf("\r%2d", i); fflush(stdout); sleep(1000); } printf("\r\aFIRE!!\n"); c = clock(); printf("程序开始后经过了%.1f秒。\n", (double)c / CLOCKS_PER_SEC); return 0; }
//2-6心算训练,连加三个整数的时间 #include<time.h> #include<stdio.h> #include<stdlib.h> int main(){ int a, b, c; int x; clock_t start, end; double req_time; srand(time(NULL)); a = 100 + rand() % 900; b = 100 + rand() % 900; c = 100 + rand() % 900; printf("%d + %d + %d等于多少:", a, b, c); start = clock(); while(1){ scanf("%d", &x); if(x == a + b + c) break; printf("\a回答错误!!\n请重新输入:"); } end = clock(); req_time = (double)(end - start) / CLOCKS_PER_SEC; printf("用时%.1f秒。\n", req_time); if(req_time > 30.0) printf("花太长时间了吧。\n"); else if (req_time > 17.0) printf("还行吧。\n"); else printf("真快啊。\n"); return 0; }
//2-7即那个sleep函数 //2-8逐个显示字符,显示完后再逐个从后往前消除,反复执行此操作 #include<time.h> #include<stdio.h> #include<string.h> int sleep(unsigned long x){ clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ int i; char name[] = "BohYoh Shibata"; int name_len = strlen(name); while(1){ for(i = 0; i < name_len; i++){ //从头开始逐个显示字符 putchar(name[i]); fflush(stdout); sleep(500); } for(i = 0; i < name_len; i++){ //从未开始逐个消去字符 printf("\b \b"); fflush(stdout); sleep(500); } } return 0; }
//2-9从右往左滚动字符 #include<time.h> #include<stdio.h> #include<string.h> int sleep(unsigned long x){ clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ int i; int cnt = 0; //第几个字符显示在最前面 char name[] = "BohYoh "; int name_len = strlen(name); while(1){ putchar('\r'); for(i = 0; i < name_len; i++){ if(cnt + i < name_len) putchar(name[cnt + i]); else putchar(name[cnt + i - name_len]); } fflush(stdout); sleep(500); if(cnt < name_len - 1) cnt++; else cnt = 0; } return 0; }
//2-10从左往右滚动字符 #include<time.h> #include<stdio.h> #include<string.h> int sleep(unsigned long x){ clock_t c1 = clock(), c2; do{ if((c2 = clock()) == (clock_t) - 1) return 0; } while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main(){ int i; int cnt = 0; //第几个字符显示在最前面 char name[] = "BohYoh "; int name_len = strlen(name); while(1){ putchar('\r'); for(i = 0; i < name_len; i++){ if(cnt + i < name_len) putchar(name[cnt + i]); else putchar(name[cnt + i - name_len]); } fflush(stdout); sleep(500); if(cnt > 0) //与2-9只有这个if不同 cnt--; else cnt = name_len - 1; } return 0; }
//2-11把数字字符每次偏移1位显示 #include<stdio.h> int main(){ int i, j; int x; printf("要显示多少行:"); scanf("%d", &x); for(i = 1; i <= x; i++){ for(j = 1; j < i; j++) putchar(' '); //显示i-1个空白字符 printf("%d\n", i % 10); } return 0; }
//2-12把数字字符每次偏移1位显示 第二版 #include<stdio.h> int main(){ int i; int x; printf("要显示多少行:"); scanf("%d", &x); for(i = 1; i <= x; i++) printf("%*d\n", i, i % 10); //第二个参数放入* return 0; }
//2-13同时训练扩大水平方向视野的心算训练 #include<time.h> #include<stdio.h> #include<stdlib.h> int main(){ int stage; int a, b, c; int x; int n;//空白的宽度 clock_t start, end; srand(time(NULL)); printf("扩大视野心算训练开始!!\n"); start = clock(); for(stage = 0; stage < 10; stage++){ a = 10 +rand() % 90; b = 10 +rand() % 90; c = 10 +rand() % 90; n = rand() % 17; 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(1); } end = clock(); printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC); return 0; }