编程语言:所有的编程语言都是事和物。
物:数据(内存)
事:main开始 从上往下 从左往右(CPU)
判断:if else switch
循环:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n = 0;
goon:
/*任何标识符加冒号称之为标签,
标签对于程序没有任何影响,就像马路的
路牌一样,对于马路没有任何影响,只是
用来标识当前所处的位置*/
printf("%d",n);
n++;
if(n <= 9){
goto goon;//跳转到标签位置
}
printf("\n");
while(1);//让程序停顿
return 0;
}
1.goto:无条件跳转,可以用来实现循环
2.while();:只管紧跟在后面的一条语句,警惕空循环和死循环。
while(1);用于让程序停顿,相当于 system(“pause”);建议使用后者,因为空循环虽然没有消耗内存,但是一直在消耗CPU(让CPU持续做运算)。
{}:把多条语句整合成一条
3.for(表达式1;表达式2;表达式3){}
表达式1:在整个循环开始前执行1次;
表达式2:每次循环开始前判断,为真,循环继续,为假,循环结束。
表达式3:每次循环结束前执行一次;
4.do{
}while()
#include "pch.h"//双引号表示先在当前文件夹下找,找不到再去系统文件夹下找
#include <stdio.h>//尖括号表示在系统文件夹下找
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//打印地图
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
printf("# ");
}
printf("\n");
Sleep(1000);//延时1000毫秒=1秒 该函数在windows.h头文件中
}
#if 0
特别注意:
printf("i:%d j:%d", i, j);
C89不允许在for()的第一个表达中定义变量,C99则允许,但是定义的变量的作用域和生命周期仅限于循环内部。
#endif
return 0;
}
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
/*打印下三角形(求三角形的线性规划方程 i =j 即可)
框架为:
两层for循环中
if(线性规划方程)
printf("*");
else
printf();
*/
#if 0
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i >= j)
printf("*");
else
printf(" ");
}
printf("\n");
Sleep(1000);//延时1000毫秒=1秒 该函数在windows.h头文件中
}
#endif
#if 0
//打印倒三角
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i >= 10-j) // 把方阵看成倒着的xy坐标系即可,线性规划方程为:i+j=10
printf("*");
else
printf(" ");
}
printf("\n");
Sleep(1000);//延时1000毫秒=1秒 该函数在windows.h头文件中
}
#endif
//打印等腰三角形
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i >= j && i >= 10 - j) // 把方阵看成倒着的xy坐标系即可,线性规划方程为:i+j=10
printf("*");
else
printf(" ");
}
printf("\n");
Sleep(1000);//延时1000毫秒=1秒 该函数在windows.h头文件中
}
#if 0
特别注意:
printf("i:%d j:%d", i, j);
C89不允许在for()的第一个表达中定义变量,C99则允许,但是定义的变量的作用域和生命周期仅限于循环内部。
#endif
return 0;
}
干扰循环的关键字:
continue:在循环中遇到此关键字,立刻开始下一次循环。
break:在循环中遇到此关键字,立即结束。
return:在函数中遇到此关键字,立即结束。main函数中遇到此关键字,程序结束。
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//一只猴子 蹭 两只猴子 蹭蹭 三只猴子 蹭蹭蹭 四只猴子蹭蹭蹭蹭
for (int i = 0; i < 6; i++) {
printf("%d只猴子:",i + 1);
Sleep(1000);
for (int j = 0; j <6; j++) {
if (j <= i) {
printf("蹭");
if (i >= 3) continue;//当变到4只猴子的时候加快速度
Sleep(2000);
}
}
printf("\n");
}
return 0;
}
函数:本质代码的复用
运算符本质就是函数,它的左右操作数就是参数,只不过运算符的参数有限。
返回类型名 函数名(参数1,...) ...表示可变长函数
{
函数体语句;
return 返回值;
}
//函数的声明
返回类型名 函数名(参数1,...);
//函数的定义
返回类型名 函数名(参数1,...) ...表示可变长函数
{
函数体语句;
return 返回值;
}
//函数的调用
函数名(参数1,...)
形式参数:定义函数时,参数列表中的参数(变量名)
实际参数:调用函数时,参数列表的参数。
实际参数和形式参数的关系:在调用函数的一刹那,实际参数赋值给形式参数。实参和形参必须类型匹配,如果不匹配需要类型转换。
隐式类型转换:编译器自动改变表达式的类型。
显式(强制)类型转换:表达式前加(),小括号里写想要改成的类型名。
变量的生命周期和作用域:
局部变量:在函数中定义的变量,生命周期和函数调用相同,作用域:只在函数中可见。
main函数中定义的变量,生命周期和main()函数相同。
全局变量:生命周期和应用程序相同,定义了整个源程序文件中可见。
实参赋值给形参:
1.普通值传递 简单赋值
2.地址传递 指针赋值
引用:使用变量名,就是使用变量名绑定的内存段
解引用:解引用指针变量名,就是通过指针变量名绑定的内存段中保存的地址,去使用这个地址指明的内存段。
作业:
十进制转化为二进制
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void toBinary(int a);
int main(int argc, char *argv[]) {
int n;
scanf_s("%d", &n);
toBinary(n);
return 0;
}
void toBinary(int a) {
int i, shu[32] = {0};
for (i = 0; a != 0 ; i++, a /= 2) {
shu[i] = a % 2;
}
for (int j = 31; j >= 0; j--) {
printf("%d", shu[j]);
if (j % 4 == 0)
printf(" ");
}
}
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]){
int n;
printf("请输入一个整数:");
scanf_s("%d", &n);//每个int类型的数据都是32位,把它的每一位打印出来需要32次
for (int i = 0; i < 32; i++) {
//每次获取最高位
if ((n & 0x80000000) == 0x80000000) {//如果最高位为1
printf("1");
}
else {
printf("0");
}
n <<= 1;//左移相当于把32位的最高位去掉,在最右边补0;
}
printf("\n");
return 0;
}
要求输入一个整数,输出一个边长为这个整数实心空心菱形
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void paint(int n);
int main(int argc, char *argv[]) {
int n;
scanf_s("%d", &n);
paint(n);
return 0;
}
void paint(int n) {
for (int y = 0; y <= 2*n; y++) {
for (int x = 0; x <= 2*n; x++) {
if (x+y==n || y == x-n || y == x+n || y+x==3*n) printf("*");
else printf(" ");
}
printf("\n");
}
}
输入一个数判断是不是素数,用goto语句实现:
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
bool isPrime(int n);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
int count = 0;
beg:
printf("请输入整数:");
scanf_s("%d", &n);
if (n == 99999) goto end;
if (isPrime(n)) count++;
goto beg;
end:
printf("总共有%d个素数!\n", count);
return 0;
}
bool isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int isPrime(int n);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int n,count = 0;
int main(int argc, char *argv[]){
n = getchar();
goon:if (isPrime(n)) {
count++;
}
if ((n = getchar()) != EOF) {
goto goon;
}
printf("您输入的数中,有%d个素数",count);
return 0;
}
int isPrime(int n){
for (int i = 2; i < n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}