C++入门详细笔记(共八章)(中)

简介: C++入门详细笔记(共八章)

第三章 运算符


作用:用于执行代码的运算



3.1 算数运算符


作用:用于处理四则运算



1. 除 /


例1:整数相除只输出整数


#include <iostream>
using namespace std;
int main()
{
  int a1 = 10;
  int b1 = 3;
  cout << a1 / b1 << endl;
  int a2 = 10;
  int b2 = 20;
  cout << a2 / b2 << endl;
  system("pause");
  return 0;
}


3
0
请按任意键继续. . .


注意:除数不能为0


错误示例:


int a1 = 10;
  int b1 = 0;
  //cout << a1 / b1 << endl;


例2:小数相除,除不尽则输出小数


#include <iostream>
using namespace std;
int main()
{
  double d1 = 0.5;
  double d2 = 0.2;
  double d3 = 0.1;
  cout << d1 / d2 << endl;
  cout << d2 / d3 << endl;
  system("pause");
  return 0;
}


2.5
2
请按任意键继续. . .


2. 求余 %


注意:两个小数不能取模


总结:只有整型变量可以进行取模运算


#include <iostream>
using namespace std;
int main()
{
  int a1 = 10;
  int b1 = 3;
  int c1 = 20;
  int d1 = 0;
  cout << a1 % b1 << endl;
  cout << a1 % c1 << endl;
  //cout << a1 % d1 << endl;
  system("pause");
  return 0;
}


1
10
请按任意键继续. . .


3. a++ 和 ++a


++a:前置递增 先让变量+1 然后进行表达式的运算


a++:后置递增 先进行表达式的运算 后让变量+1


#include <iostream>
using namespace std;
int main()
{
  //1、前置递增
  int a = 10;
  ++a;
  cout << "a=" << a << endl;
  //2、后置递增
  int b = 10;
  b++;
  cout << "b=" << b << endl;
  //3、前置和后置的区别
  //前置递增  先让变量+1  然后进行表达式的运算
  //后置递增  先进行表达式的运算  后让变量+1
  int a2 = 10;
  int b2 = ++a2 * 10;
  cout << "a2=" << a2 << endl;
  cout << "b2=" << b2 << endl;
  int a3 = 10;
  int b3 = a3++ * 10;
  cout << "a3=" << a3 << endl;
  cout << "b3=" << b3 << endl;
  system("pause");
  return 0;
}


a=11
b=11
a2=11
b2=110
a3=11
b3=100
请按任意键继续. . .


3.2 赋值运算符


作用:用于将表达式的值赋给变量



这里是和Python一样的,例如:


a+=2意思就是a=a+2,其他运算符一个意思。


3.3 比较运算符


作用:用于表达式的比较,并返回一个真值或假值


只会输出0(假)和1(真)



#include <iostream>
using namespace std;
int main()
{
  int a = 1;
  int b = 2;
  cout << (a <= b) << endl;
  cout << (a != b) << endl;
  cout << (a > b) << endl;
  system("pause");
  return 0;
}


1
1
0
请按任意键继续. . .


3.4 逻辑运算符


作用:用于根据表达式的值返回真值或假值


只会输出0(假)和1(真)



#include <iostream>
using namespace std;
int main()
{
  int a = 1;
  int b = 2;
  cout << (!a) << endl;
  cout << (!!a) << endl;
  cout << (a && b) << endl;
  cout << (a || b) << endl;
  system("pause");
  return 0;
}


0
1
1
1
请按任意键继续. . .


第四章 程序流程结构


C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构


  • 顺序结构:程序按顺序执行,不发生跳转


  • 选择结构:依据条件是否满足,有选择的执行相应功能


  • 循环机构:依据条件是否满足,循环多次执行某段代码


4.1 选择结构


4.1.1 if语句


四种形式:


  • 单行格式if语句


  • 多行格式if语句


  • 多条件if语句


  • 嵌套if语句


例1:单行格式


格式:if(条件){条件满足执行语句}



#include <iostream>
using namespace std;
int main()
{
  int score = 0;
  cout << "请输入分数:" << endl;
  cin >> score;
  if (score >= 90)           // 这里不要加分号
  { cout << 'a' << endl; }
  system("pause");
  return 0;
}


请输入分数:
92
a
请按任意键继续. . .


注意:if条件表达式后不要加分号


例2:多行格式


格式:if(条件){条件满足执行的语句}else{条件不满足执行的语句}



#include <iostream>
using namespace std;
int main()
{
  int score = 0;
  cout << "请输入分数:" << endl;
  cin >> score;
  if (score >= 90)           // 这里不要加分号
  { 
    cout << 'a' << endl; 
  }
  else
  {
    cout << 'b' << endl;
  }
  system("pause");
  return 0;
}


请输入分数:
89
b
请按任意键继续. . .


例3:多条件if语句


格式:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句} ... else{都不满足执行的语句}



#include <iostream>
using namespace std;
int main()
{
  int score = 0;
  cout << "请输入分数:" << endl;
  cin >> score;
  if (score >= 90)           // 这里不要加分号
  { 
    cout << 'a' << endl; 
  }
  else if (score >= 80)
  {
    cout << 'b' << endl;
  }
  else if (score >= 70)
  {
    cout << 'c' << endl;
  }
  else
  {
    cout << 'd' << endl;
  }
  system("pause");
  return 0;
}


请输入分数:
62
d
请按任意键继续. . .


例4:嵌套if语句


#include <iostream>
using namespace std;
int main()
{
  int score = 0;
  cout << "请输入分数:" << endl;
  cin >> score;
  if (score >= 60)           // 这里不要加分号
  { 
    cout << "恭喜你及格了" << endl; 
    if (score >= 90) 
    {
      cout << "你的等级为a" << endl;
    }
    else
    {
      cout << "你的等级为b" << endl;
    }
  }
  else
  {
    cout << "再接再厉" << endl;
  }
  system("pause");
  return 0;
}


请输入分数:
92
恭喜你及格了
你的等级为a
请按任意键继续. . .


练习:三只小猪称体重



#include <iostream>
using namespace std;
int main()
{
  // 三只小猪称体重,判断哪个最重
  int a = 0;
  int b = 0;
  int c = 0;
  cout << "请输入小猪a的体重:" << endl;
  cin >> a;
  cout << "请输入小猪b的体重:" << endl;
  cin >> b;
  cout << "请输入小猪c的体重:" << endl;
  cin >> c;
  if ((a > b) && (a > c))
  {
    cout << "小猪a最重" << endl;
  }
  else if ((b > a) && (b > c))
  {
    cout << "小猪b最重" << endl;
  }
  else if ((c > b) && (c > a))
  {
    cout << "小猪c最重" << endl;
  }
  system("pause");
  return 0;
}


请输入小猪a的体重:
150
请输入小猪b的体重:
200
请输入小猪c的体重:
300
小猪c最重
请按任意键继续. . .


4.1.2 三目运算符


作用:通过三目运算符实现简单的判断


语法:表达式1 ? 表达式2 : 表达式3


解释:


  • 如果表达式1的值为真,执行表达式2,并返回表达式2的结果


  • 如果表达式1的值为假,执行表达式3,并返回表达式3的结果


#include <iostream>
using namespace std;
int main()
{
  // 把a、b中最大的赋值给c
  int a = 10;
  int b = 20;
  int c = 0;
  c = (a > b ? a : b);
  cout << "c=" << c << endl;
  // 三目运算符返回的是变量,可以继续进行赋值
  (a > b ? a : b) = 100;
  cout << "a=" << a << endl;
  cout << "b=" << b << endl;
  system("pause");
  return 0;
}


c=20
a=10
b=100
请按任意键继续. . .


4.1.3 switch语句


执行多条件分支语句


语法:


switch(表达式)
{
   case 结果1:执行语句; break;
   case 结果2:执行语句; break;
   ...
   default:执行语句; break;
}


例:


#include <iostream>
using namespace std;
int main()
{
  int score = 0;
  cout << "请输入分数:" << endl;
  cin >> score;
  switch(score)
  {
  case 10:
    cout << 'a' << endl; break;
  case 9:
    cout << 'b' << endl; break;
  case 8:
    cout << 'c' << endl; break;
  case 7:
    cout << 'd' << endl; break;
  default:
    cout << 'f' << endl; break;
  }
  system("pause");
  return 0;
}


请输入分数:
5
f
请按任意键继续. . .


if和switch的区别:


  • switch缺点:判断时候只能是整型或字符型不可以是一个区间


  • switch优点:结构清晰,执行效率高


4.2 循环结构


4.2.1 while循环语句


作用:满足循环条件,执行循环语句


语法:while(循环条件) {循环语句}


解释:只要循环条件的结果为真,就执行循环语句



例1


#include <iostream>
using namespace std;
int main()
{
  int i = 0;
  int n = 0;
  while(i<=5)
  {
    n = ++i;
    cout << n << endl;
  }
  system("pause");
  return 0;
}


1
2
3
4
5
6
请按任意键继续. . .


例2


int main()
{
  int j = 0;
  int m = 0;
  while (j <= 5)
  {
    m = j++;
    cout << m << endl;
  }
  system("pause");
  return 0;
}


0
1
2
3
4
5
请按任意键继续. . .


练习案例:猜数字小游戏


rand()用法:


rand()%100   //随机生成数  0~99


例:


#include <iostream>
using namespace std;
int main()
{
  // 猜数字游戏
  //rand()%100   //随机生成数  0~99
  int num = rand() % 100 + 1;
  int number = 0;
  while (number != num)
  {
    cout << "请输入数字:" << endl;
    cin >> number;
    if (number > num)
    {
      cout << "猜测过大" << endl;
    }
    else if (number < num)
    {
      cout << "猜测过小" << endl;
    }
  }
  cout << "恭喜你猜对了" << endl;
  system("pause");
  return 0;
}


请输入数字:
52
猜测过大
请输入数字:
26
猜测过小
请输入数字:
45
猜测过大
请输入数字:
40
猜测过小
请输入数字:
42
恭喜你猜对了
请按任意键继续. . .


可是这个版本每次运行程序生成的随机数是固定的,下次再运行程序直接猜对


请输入数字:
42
恭喜你猜对了
请按任意键继续. . .


例2:解决方法: 添加随机数种子


#include <iostream>
using namespace std;
#include <ctime>
int main()
{
  // 添加随机数种子 作用利用当前系统时间随机生成数,防止每次随机数都一样
  srand((unsigned int)time(NULL));
  // 猜数字游戏
  //rand()%100   //随机生成数  0~99
  int num = rand() % 100 + 1;
  int number = 0;
  while (number != num)
  {
    cout << "请输入数字:" << endl;
    cin >> number;
    if (number > num)
    {
      cout << "猜测过大" << endl;
    }
    else if (number < num)
    {
      cout << "猜测过小" << endl;
    }
  }
  cout << "恭喜你猜对了" << endl;
  system("pause");
  return 0;
}


产生的随机数再也不是42了


请输入数字:
42
猜测过小
请输入数字:
56
猜测过小
请输入数字:
78
猜测过大
请输入数字:
70
猜测过大
请输入数字:
65
猜测过小
请输入数字:
67
猜测过小
请输入数字:
69
猜测过大
请输入数字:
68
恭喜你猜对了
请按任意键继续. . .


4.2.2 do…while循环语句


语法:do{循环语句] while(循环条件);


注意:与while的区别在于do…while会先执行一次循环语句,再判断循环条件



#include <iostream>
using namespace std;
int main()
{
  int a = 0;
  do
  {
    cout << a << endl;
    a++;
  } 
  while (a < 10);
  system("pause");
  return 0;
}


0
1
2
3
4
5
6
7
8
9
请按任意键继续. . .


练习案例:水仙花数


案例描述:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身


例如:1^3 + 5^3 + 3^3 =153


请利用do…while语句,求出所有33位数的水仙花数


#include <iostream>
using namespace std;
int main()
{
  int n = 100;
  int a = 0;
  int b = 0;
  int c = 0;
  do
  {
    a = n / 100;
    b = (n - a * 100) / 10;
    c = n - a * 100 - b * 10;
    if (n == (a*a*a + b*b*b + c*c*c))
    {
      cout << n << endl;
    }
    n++;
  } 
  while (n < 1000);
  system("pause");
  return 0;
}


153
370
371
407
请按任意键继续. . .


注意:这里不可以打


n == a ^ 3 + b ^ 3 + c ^ 3;


4.2.3 for循环语句


作用:满足循环条件,执行循环语句


语法:for(起始表达式;条件表达式;末尾表达式){循环语句};


#include <iostream>
using namespace std;
int main()
{
  for (int i = 0; i < 10; i++)
  {
    cout << i << endl;
  }
  system("pause");
  return 0;
}


0
1
2
3
4
5
6
7
8
9
请按任意键继续. . .


注意:for循环中的表达式,要用分号进行分隔


例2


#include <iostream>
using namespace std;
int main()
{
  int i = 0;
  for (; ; )
  {
    if (i >= 10)
    {
      break;
    }
    cout << i << endl;
    i++;
  }
  system("pause");
  return 0;
}


0
1
2
3
4
5
6
7
8
9
请按任意键继续. . .


练习案例:敲桌子


案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。



#include <iostream>
using namespace std;
int main()
{
  int i = 0;
  int a = 0;
  int b = 0;
  int c = 0;
  while(i<100)
  {
    a = i / 10;
    b = i - a * 10;
    c = i / 7;
    if ((a == 7) || (b == 7) || ((i == c * 7) && (c>0)))
    {
      cout << "敲桌子" << endl;
    }
    else
    {
      cout << i << endl;
    }
    i++;
  }
  system("pause");
  return 0;
}



4.2.4 嵌套循环


#include <iostream>
using namespace std;
int main()
{
  for (int i = 0; i < 10; i++)
  {
    for (int j = 0; j < 10; j++)
    {
      cout << "* ";
    }
    cout << endl;
  }
  system("pause");
  return 0;
}


外层循环一次,内层循环一周



练习案例:九九乘法表


#include <iostream>
using namespace std;
int main()
{
  for (int i = 1; i < 10; i++)
  {
    for (int j = 1; j < i+1; j++)
    {
      cout << j << "*" << i << "=" << i * j<<'\t';
    }
    cout << endl;
  }
  system("pause");
  return 0;
}



4.3 跳转语句


4.3.1 break语句


作用:用于跳出选择结构或者循环结构


break使用的时机:


  • 出现在switch条件语句中,作用是终止case并跳出switch


  • 出现在循环语句中,作用是跳出当前的循环语句


  • 出现在嵌套语句中,跳出最近的内层循环语句


例1:switch语句中


#include <iostream>
using namespace std;
int main()
{
  cout << "请选择副本难度" << endl;
  cout << "1、普通" << endl;
  cout << "2、中等" << endl;
  cout << "3、困难" << endl;
  int select = 0;
  cin >> select;
  switch (select)
  {
  case 1:
    cout << "您选择的是普通难度" << endl; break;
  case 2:
    cout << "您选择的是中等难度" << endl; break;
  case 3:
    cout << "您选择的是困难难度" << endl; break;
  default:
    break;
  }
  system("pause");
  return 0;
}


请选择副本难度
1、普通
2、中等
3、困难
1
您选择的是普通难度
请按任意键继续. . .


例2:在for循环中


#include <iostream>
using namespace std;
int main()
{
  for (int i = 0; i < 10; i++)
  {
    if (i == 5)
    {
      break;
    }
    cout << i << endl;
  }
  system("pause");
  return 0;
}


0
1
2
3
4
请按任意键继续. . .


例3:在嵌套for循环中退出内层循环


#include <iostream>
using namespace std;
int main()
{
  for (int i = 0; i < 10; i++)
  {
    for (int j = 0; j < 10; j++)
    {
      if (j==5)
      {
        break;
      }
      cout << "* ";
    }
    cout << endl;
  }
  system("pause");
  return 0;
}



4.3.2 continue语句


作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环


#include <iostream>
using namespace std;
int main()
{
  for (int i = 0; i < 10; i++)
  {
    if ((i == 5)||(i == 8))
    {
      continue;
    }
    cout << i << endl;
  }
  system("pause");
  return 0;
}


0
1
2
3
4
6
7
9
请按任意键继续. . .


注意:continue并没有使整个循环终止,而break会跳出循环


4.6.3 goto语句


作用:可以无条件跳转语句


语法:goto 标记


解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置


#include <iostream>
using namespace std;
int main()
{
  cout << "1" << endl;
  goto FLAG;                // 标记
  cout << "2" << endl;
  cout << "3" << endl;
  cout << "4" << endl;
  FLAG:                   // 从标记处跳到这里
  cout << "5" << endl;
  system("pause");
  return 0;
}


1
5
请按任意键继续. . .


注意:在程序中不建议使用goto语句,以免造成程序流程混乱

目录
相关文章
|
9天前
|
编译器 C语言 C++
【C++】学习笔记——C++入门_2
【C++】学习笔记——C++入门_2
19 6
|
9天前
|
安全 编译器 C语言
【C++】学习笔记——C++入门_3
【C++】学习笔记——C++入门_3
20 4
|
9天前
|
程序员 编译器 C语言
【C++】学习笔记——C++入门_1
【C++】学习笔记——C++入门_1
22 4
|
9天前
|
安全 编译器 程序员
【C++初阶】--- C++入门(上)
【C++初阶】--- C++入门(上)
12 1
|
10天前
|
安全 编译器 C语言
C++练级之路——C++入门
C++练级之路——C++入门
11 1
|
15天前
|
C++
C++类和类模板——入门
C++类和类模板——入门
13 1
|
16天前
|
存储 C++ 容器
C++一分钟之-变量与数据类型入门
【6月更文挑战第18天】**C++编程基础:变量与数据类型概览** 了解变量(存储数据的容器)和数据类型是编程入门的关键。声明变量如`int age = 25;`,注意初始化和类型匹配。基本数据类型包括整型(int等)、浮点型(float、double)、字符型(char)和布尔型(bool)。理解类型范围和精度,使用字面量后缀增强可读性。深入学习数组、指针、结构体和类,以及动态内存管理,避免数组越界和内存泄漏。不断实践以巩固理论知识。
23 1
|
9天前
|
存储 安全 编译器
【C++初阶】--- C++入门(下)
【C++初阶】--- C++入门(下)
8 0
|
9天前
|
存储 编译器 Linux
【C++初阶】--- C++入门(中)
【C++初阶】--- C++入门(中)
11 0
|
20天前
|
JavaScript 前端开发 编译器
【C++初阶】C++模板编程入门:探索泛型编程的奥秘
【C++初阶】C++模板编程入门:探索泛型编程的奥秘
17 0