目录
●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!
视频动态演示测试字符汇聚输出(“Hello,World!”)
前言:
●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!
getchar()、putchar()等函数的功能.(参见MSDN)
getchar getchar读取失败的时候返回EOF int main() { scanf() int ch = getchar();//'a' - 97 printf("%c\n", ch); putchar(ch); return 0; } int main() { int ch = 0; while ((ch = getchar()) != EOF) { putchar(ch); } return 0; }
scanf()的读取及位置:
scanf遇到\n结束输入和读取,遇到空格或tab停止读取(但输入正常)
int main() { char input[20] = { 0 }; printf("请输入密码:>"); scanf_s("%s", input);//input是数组,不用加& //如果输入abcdef,再回车——>abcdef\n //scanf拿走abcdef //getchar();//拿走\n //清理缓冲区 int tmp = 0; while ((tmp = getchar()) != '\n') { ;//空语句 } //scanf遇到\n结束输入和读取,遇到空格或tab停止读取(但输入正常) //如果输入abcdef hehe, 再回车—— > abcdef hehe\n //while判断最后的字符是否为\n printf("请确认密码(Y/N):>"); int ch = getchar(); if (ch == 'Y') { printf("确认成功\n"); } else { printf("确认失败\n"); } return 0; }
#include <stdio.h> int main() { int ch = 0; while ((ch = getchar()) != EOF) { if (ch < '0' || ch>'9') continue; putchar(ch); } return 0; }
//这个代码的作用是:只打印数字字符,跳过其他字符的
break作用:
#include <stdio.h> int main() { int i = 0; for (i = 1; i <= 10; i++) { if (5 == i) { break; } printf("%d ", i); } return 0; }
continue作用:
#include <stdio.h> int main() { int i = 0; for (i = 1; i <= 10; i++) { if (5 == i) { continue; } printf("%d ", i); } return 0; }
for循环:
#include <stdio.h> int main() int i = 0; int j = 0; //这里打印多少个hehe? for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { printf("hehe\n"); } } return 0; }
1. 不可在 for 循环体内修改循环变量,防止 for 循环失去控制。2. 建议 for 语句的循环控制变量的取值采用 “ 前闭后开区间 ”写法。![]()
![]()
for循环体省略示例及测试输出:
//代码2 #include <stdio.h> int main() { int i = 0; int j = 0; //这里打印多少个hehe? for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { printf("hehe\n"); } } }
易错题提要:
#include <stdio.h> int main() { //请问循环要循环多少次? int i = 0; int k = 0; for (i = 0, k = 0; k = 0; i++, k++) k++; return 0; }
题目求解n(n有范围,尽量不要太大)的阶乘:
#include <stdio.h> int main() { int n= 0; scanf("%d", &n); int ret = 1; int i = 0; for (i=1; i <= n; i++) { ret = ret * i; } printf("%d\n", ret); return 0; }
题目-阶乘相加:
#include <stdio.h> int main() { int n = 0; //scanf("%d", &n); // 计算 1!+ 2!+ 3!+ …… + 10! int i = 0; int ret = 1; int sum = 0; for (n = 1; n <= 10; n++) { ret = 1; for (i = 1; i <= n; i++) { ret = ret * i; } sum += ret; } printf("%d\n", sum); return 0; }
//优化// 计算 1!+ 2!+ 3!+ …… + 10! #include <stdio.h> int main() { int n = 0; int i = 0; int ret = 1; int sum = 0; for (n = 1; n <= 10; n++) { ret = ret * n; sum += ret; } printf("%d\n", sum); return 0; }
题目-二分法查找数组中的数:
#include <stdio.h> int main() //在一个有序数组中查找具体的某个数字n。(讲解二分查找) { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //0~9 int k = 7; int i = 0; for (i = 0; i < 10; i++) { if (arr[i] == k) { printf("找到目标,下标为%d\n", i); break; } } if (i == 10) { printf("未找到目标\n"); } return 0; } 代码优化——二分法 #include <stdio.h> int main() //在一个有序数组中查找具体的某个数字n。(讲解二分查找) { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //0~9 int sz = sizeof(arr) / sizeof(arr[0]); //40 /4 =10 int k = 17; int left = 0; int right = sz - 1; while (left <=right) { int mid = (left + right) / 2; if(arr[mid] < k) { left = mid + 1; } else if (arr[mid] > k) { right = mid - 1; } else { printf("找到目标,下标为:%d\n", mid); break; } } if (left > right) { printf("未找到目标\n"); } return 0; }
题目:数字塔输出:
#include <stdio.h> void pyramid(int n) { int i;//行数 int j;//列数 for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) //输出空格 { printf(" \n"); } for (j = 1; j <= i; j++) { printf("%d ", i); } putchar('\n'); } } int main() { int n; void pyramid(int n); printf("Enter n:"); scanf("%d", &n); pyramid(n); return 0; }
题目-动态演示字符汇聚("Hello,World!")
#include <stdio.h> #include <string.h> #include <windows.h> int main() { char arr[] = "abc"; //[a b c \0] // 0 1 2 char arr1[] = "Hello World!!!!!!!!"; char arr2[] = "###################"; // "H##############!"; int left = 0; int right = strlen(arr1) - 1; while (left <= right) { arr2[left] = arr1[left]; arr2[right] = arr1[right]; printf("%s\n", arr2); Sleep(1000);//睡眠函数-单位是毫秒 //system("cls");//执行系统命令的清空屏幕 使用次语句会值显示一行动态变化 left++; right--; } printf("%s\n", arr2); printf("Hello,World!"); return 0; }
总结:
通过动态演示,可以直观清晰地理解程序运行步骤,仍需多加努力,才能熟练掌握!