4 流程化结构
三种结构:顺序结构、选择结构、循环结构
if语句
贴一下 C++ 支持数学中的常用逻辑条件:
- 小于:a < b
- 小于或等于:a <= b
- 大于:a > b
- 大于或等于:a >= b
- 等于 a == b
- 不等于:a != b
int main()
{
int score = 0;
cout << "请输入您的分数(0-100):" << endl;
cin >> score;
cout << "您输入的分数是:" << score << endl;
if (score < 60)
{
cout << "成绩不及格,多多努力" << endl;
}
else if (score < 80)
{
cout << "您的成绩良好" << endl;
}
else
{
cout << "成绩优秀,继续保持" << endl;
}
system("pause");
return 0;
}
小作业:三至小猪称体重(判断那只最重)
思路:首先判断a与b,其次判断c于其中较大者
直接贴代码:
int main()
{
int a = 0;
cout << "请输入A的体重:" << a << endl;
cin >> a;
int b = 0;
cout << "请输入B的体重:" << b << endl;
cin >> b;
int c = 0;
cout << "请输入C的体重:" << c << endl;
cin >> c;
cout << "ABC的体重分别是:" << a << "、" << b << "、" << c << endl;
if (a > b)
{
if (a > c)
{
cout << "A最重" << endl;
}
else
{
cout << "C最重" << endl;
}
}
else
{
if (c > b)
{
cout << "C最重" << endl;
}
else
{
cout << "B最重" << endl;
}
}
system("pause");
return 0;
}
if简写之三元运算符
由三个操作数组成。它可用于替换多条线路 的代码与单行。它通常用于替换简单的 if else 语句:
语法:variable = (condition) ? expressionTrue : expressionFalse;
贴代码:
if循环
int time1 = 20;
if (time1 < 18) {
cout << "Good day.";
}
else {
cout << "Good evening.";
}
三元运算符
int time2 = 16;
string result = (time2 < 18) ? "Good day." : "Good evening.";
cout << result;
switch语句
语法:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
直接来个星期几的判断吧,上代码:
int main()
{
int day = 0;
cout << "请输入1-7任意数字:" << '\n';
cin >> day;
switch (day)
{
case(1):
cout << "Monday";
break;
case(2):
cout << "Tuesday";
break;
case(3):
cout << "Wedensday";
break;
case(4):
cout << "Thursday";
break;
case(5):
cout << "Friday";
break;
case(6):
cout << "Saturday";
break;
case(7):
cout << "Sunday";
break;
system("pause");
return 0;
}
c++循环
while,简单上代码:
int i = 0;
while (i < 5)
{
cout << i << "\n";
i++;
}
do while,上代码:
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
for循环
确切地知道要遍历 1 个块的次数时 代码,使用循环for而不是循环while
for (int i = 0; i <= 10; i = i + 2) {
cout << i << "\n";
}
foreach循环用于遍历数组(或其他数据集)中的元素:
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
c++中断
continue用来继续循环中的下一次迭代
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
break用于跳出循环
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break;
}
}
5 数组
创建数组及修改
数组用于在单个变量中存储多个值,而不是为每个变量声明单独的变量值。要声明数组,请定义变量类型,指定名称 数组后跟方括号,并指定它应该存储的元素数:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; //列表
//int num[3] = {10, 20, 30};
cars[0] = "Opel"; //修改列表元素
cout << cars[0];
// Now outputs Opel instead of Volvo
创建时还可以省略数字大小,但是不提倡:
string cars[] = {"Volvo", "BMW", "Ford"}; // Three array elements
遍历数组
利用for循环:
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << i << " = " << cars[i] << "\n";
}
还有一个“for-each 循环”(在 C++ 版本 11 (2011) 中引入),专门用于循环数组中的元素:
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
获取数组大小
要获取数组的大小,可以使用运算符:sizeof()
int myNumbers[5] = {10, 20, 30, 40, 50};
cout << sizeof(myNumbers);
//=20
sizeof()
运算符以字节为单位返回类型的大小。
要想找出数组有多少个元素,您必须将数组的大小除以 它包含的数据类型的大小:
int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(int);
cout << getArrayLength;
//=5
多维数组的创建和遍历:
string letters[2][2][2] = {
{
{ "A", "B" },
{ "C", "D" }
},
{
{ "E", "F" },
{ "G", "H" }
}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
cout << letters[i][j][k] << "\n";
}
}
}
6 结构
创建结构:
使用关键字并在大括号内声明其每个成员。
声明后,指定结构变量的名称(以下示例中的 myStructure):
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
访问成员:
// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
// Print members of myStructure
cout << myStructure.myNum << "\n";
cout << myStructure.myString << "\n";
多变量使用同一个结构:
struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here
// Put data into the first structure
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
// Put data into the second structure
myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;
// Print the structure members
cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";
7 引用
引用变量是对现有变量的“引用”,使用&运算符创建:
string food = "Pizza";
string &meal = food;
cout << food << "\n"; // Outputs Pizza
cout << meal << "\n"; // Outputs Pizza
引用的原理是符号&可以访问变量的内存地址:
string food = "Pizza";
cout << &food; // Outputs 0x6dfed4
8 指针
创建
指针是一种特殊的变量,用来储存变量内存地址:
string food = "Pizza"; // A food variable of type string
string* ptr = &food; // A pointer variable, with the name ptr, that stores the address of food
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";
取消
可以使用 “ * ”指针通过运算符获取变量的值
string food = "Pizza"; // Variable declaration
string* ptr = &food; // Pointer declaration
// Reference: Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";
// Dereference: Output the value of food with the pointer (Pizza)
cout << *ptr << "\n";
修改
可以通过指针修改变量的值
string food = "Pizza";
string* ptr = &food;
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Access the memory address of food and output its value (Pizza)
cout << *ptr << "\n";
// Change the value of the pointer
*ptr = "Hamburger";
// Output the new value of the pointer (Hamburger)
cout << *ptr << "\n";
// Output the new value of the food variable (Hamburger)
cout << food << "\n";