Skip to content

Latest commit

 

History

History
411 lines (328 loc) · 6.42 KB

l_01.md

File metadata and controls

411 lines (328 loc) · 6.42 KB

Lesson 1



1.变量 常量

  • 变量驼峰命名法则
int todayIsSaturday;
  • #define #undef & const

宏作用范围: #define#undef 结束

只替换 不分配内存

#define PI_VALUE 3.14
#undef PI_VALUE

const 分配内存 和变量类型有关

const int a = 10; 

2.printf & scanf

变量名 格式转换说明符
char&unsigned char %c
short %hd
unsigned short %hu
long %ld
unsigned long %lu
int %d
unsigned int %u
float %f %g
double %lf %lg
  • scanf的返回值

返回值类型为 int
返回输入正确的值的个数

int a;
int input = scanf("%d%d", &a,&a);
printf("%d\n", input);

3.算数运算符 逻辑运算符 位运算符

  • 算式返回值类型

取决于算式内最高精度的类型

std::cout << typeid(3.0 + 1 * sqrt(6.25)).name() << std::endl;
//double类型
  • 三目运算符
int a = 1, b = 2;
int c = a > b ? a : b;
a > b ? a : b = 3;

真 正确 true 1 非0即真

假 错误 false 0

int a = false;
  • 逻辑短路

||或 遇到第一个真 就停止运算

&&且 遇到第一个假 就停止运算

int a = 0;
a++ || a++ || a++;//2
a = 0;
++a || ++a || ++a;//1
  • 位运算 | & ~ ^ 或 且 非 异或

二进制运算

交换律 结合律 成立

int a = 4;
int b = 5;

a | b
    100
|   101
    101
int a = 4;

a & b
    100
&   101
    100
int a = 4;

~a
`   0100
    1011 非 补码
    1010 -1
    0101 非
    -5
int a = 4;
int b = 5;

a^b
    100
^   101
    001

//相同的数字异或为0
    100
^   100
    000

//和0异或还是本身
    100
^   000
    100
  • 6677889 找到缺失的数字
int a = 6, b = 6;
int c = 7, d = 7;
int e = 8, f = 8;
int g = 9;
int number;
number = a ^ b ^ c ^ d ^ e ^ f ^ g;
printf("%d\n", number); // 9

4.判断 循环 跳转

  • if嵌套 & if多条件
    if (true) {
      printf("true\n");
      if (false) {
         printf("false\n");
      }
      if (true) {
         printf("true\n");
         if (false) {
            printf("false\n");
         }
      }
   }

    if (1 > 2 || 3 > 2 || 4 > 5) {
        printf("true\n");
    }

    if (1 > 2) {
      printf("true\n");
    } else {
      printf("false");
    }
  • for(){}

循环10次

   for (int i = 0; i < 10; i++) {
      printf("1 ");
   }

在括号内定义的变量 循环体外不可使用

    for (int i = 0; i < 10; i++) {
       printf("%d ",i);
    }
    //printf("%d ", i); //error

每次循环结束后 执行一次 i++ 包括最后一次循环

   int i;
   for (i = 0; i < 10; i++) {
      printf("%d ", i);
   }
   printf("%d ", i); //10
  • while(){}
   int i=10;
   while(i) {
      printf("%d ", i);
      i--;
   }

输出结果 10 9 8 7 6 4 3 2 1

先执行一次循环体 再判断

  • do{}while()
   int i=10;
    do {
      printf("%d ", i);
      i--;
   } while(i);

输出结果 10 9 8 7 6 5 4 3 2 1

  • switch(){}
   int select;
   scanf("%d", &select);
   switch (select) {
      case 1:
         printf("select is %d", select);
         break;
      case 2:
         printf("select is %d", select);
         break;
      case 3:
         printf("select is %d", select);
         break;
      default:
         printf("aaa");
         break;
   }

等同于

   if (select == 1) {
      printf("select is %d", select);
   } else if (select == 2) {
      printf("select is %d", select);
   } else if (select == 3) {
      printf("select is %d", select);
   } else {
      printf("aaa");
   }
  • break 跳出循环
   int i=10;
   while(i) {
      printf("%d ", i);
      i--;
      break;
   }

输出结果 10

  • continue 进入下一次循环
int i = 10;
while(i){
    if(i == 5){
        i--;
        continue;
    }
    printf("%d ", i);
    i--;
}

输出结果 10 9 8 7 6 4 3 2 1

输出从0到10的奇数

    for (int i = 0; i <= 10; i++) {
      if (i % 2 == 1) {
         printf("%d", i);
      } else {
         continue;
      }
    }
  • goto 不推荐使用
   printf("1\n");
   printf("2\n");
   printf("3\n");
   goto FLAG;
   printf("4\n");
   printf("5\n");
   printf("6\n");
   printf("7\n");
FLAG:
   printf("8\n");
   printf("9\n");

输出结果 1 2 3 8 9


案例1 猜数字

生成一个1-100随机数,用户输入数字,共10次机会。

完成一次后给出提示 “猜大了/猜小了”,直到输入正确。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
   srand((unsigned int)time(NULL)); // 随机数种子
   int x = rand() % 100 + 1;  // 1-100
   int index = 10;
   int number;
   while (index) {
      printf("input a number from 1 to 100\n");
      scanf("%d", &number);
      if (number > x) {
         printf("big, input again\n");
      } else if (number < x) {
         printf("small, input again\n");
      } else {
         printf("win! the number is %d\n", x);
         break;
      }
      index--;
   }
   return 0;
}

案例2 乘法口诀表

用循环语句输出乘法口诀表

#include <stdio.h>

int main() {
   for (int i = 1; i <= 9; i++) {
      for (int j = 1; j <= i; j++) { //第一个乘数 <= 第二个乘数
         printf("%d*%d=%d\t", j, i, i * j);
      }
      printf("\n");
   }

    return 0;
}

输出结果

1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81

郑昊 编辑