[学习笔记]C++17学习第2天
```#include<iostream>
// c++17 c++20
using namespace std;
int main()
{
//变量 是命名的内存段,只能存储相对应的数据
// 基本数据类型 包含 整型、浮点型、字符型、布尔型等数值类型
//整型
int apple_count{52}; // 定义并初始化
// 浮点型
double pi {3.1415926};
// 字符
char ch = 'a';
// 布尔型
bool flag(false);
cout << apple_count << pi << ch << flag << endl;
}
```
```
#include <iostream>
// c++17
using namespace std;
int main()
{
// 初始化
int apple_count {15};
int orange_count {}; // 初始化为0
int total(apple_count + orange_count) ; //括号初始化
double weight = 2.3; // 赋值号初始化;
auto baskets = 10.0;// 类型推断
const int box {10}; // 固定值
constexpr double pi { 3.1415926 }; // 固定值
cout << total << endl;
cout << weight << endl;
cout << baskets << endl;
cout << box<< endl;
cout << pi << endl;
}
```
```
#include <iostream>
// c++17
using namespace std;
int main()
{
// 数值计算
long width{4};
long length{5};
long area{width*length};
long perimeter{2*width + length * 2};
cout << "area = " << area << ", perimeter = " << perimeter << endl;
}
```
```
#include <iostream>
// c++ 17
using namespace std;
int main()
{
int yards{}, feet{}, inches{};
cout << "Enter a distance as yards, feet and inches "
<< "with the three values separated by spaces: " << endl;
cin >> yards >> feet >> inches;
constexpr int feet_per_yard {3};
constexpr int inches_per_foot {12};
int total_inches { inches + feet * inches_per_foot+ yards * feet_per_yard * inches_per_foot };
cout << "The distances corresponds to " << total_inches << " inches." << endl;
cout << "Enter a distance in inches: ";
cin >> total_inches ;
inches = total_inches % inches_per_foot;
feet = total_inches / inches_per_foot;
yards = feet / feet_per_yard;
feet = feet % feet_per_yard;
cout << "The distance corresponds to " << yards << " yards, " << feet << " feet, " << inches << " inches." << endl;
}
```
从入门到精通系列
页:
[1]