C++ entry basic learning summary Chapter01

This project uses VS2019 for learning and use.
1. First, File-New-Project
Insert image description here
. 2. After opening the page, it will output "Hello World!" after the default creation.
Insert image description here
3. Enter the code part to learn
1. Output: Output in the C++ program Generally, "cout" is used, and "cin" is used for input. It should be noted that both input and output streams need to reference the namespace std.
Example 1:

using namespace std;
int main()
{
  	int num;//定义一个变量
   	cin >> num;//等待用户输入
   	cout << num << endl;//输出num变量的值
   	return 0;//表示程序无异常
 }

For example: the user input is: 97214520 and
the output result of this code is:97214520 97214520

Example 2:

 int main()
    {
    	int num1 = 10, num2 = 8;
    	cout << num1 << "人生不如意十有八九" << endl
		 << num2 << "能与言者无二三" << endl;
		 cin.get();//卡住程序
		 return 0;//表示程序无异常
    }

The output of this code is:

10人生不如意十有八九
8能与言者无二三

PS: Here are a few points:
(1) The function of endl: clear the buffer (print all buffer cache data), newline (equivalent to the function of "\n") (2) The
+ operation cannot be used for the connector during output Character connection, can only be connected with <<
(3) cin.get(); and getchar(); wait for the user to press a key, which can block the program. Generally, the setting will prevent the program from closing after executing the program, so that the output content can be seen.

2. Variable name naming method
(1) The variable name can only be composed of letters (AZ, az) and numbers (0-9) or underscores (_)
(2) The first character must start with a letter or an underscore
(3) It cannot Use C++ keywords to name variables to avoid conflicts
(4) Variable names are case-sensitive

For example:
my_name(commonly used in C/C++)
MyName(commonly used in high-level languages)

3. Data type
(1) Data types are mainly divided into "numeric" and "non-numeric". "Numeric" is divided into "integer" ( char[8bit], char16_t[16bit], char32_t[32bit], int, short, long, long long) and "floating type". "Point type" ( float[Suffix F/f, 4bit], double[No suffix, 8bit], long double[Suffix L/l, 12bit])
(2) There is also an unsigned integer type of data type. The following is only a brief introduction: unsigned

size_t: typedef unsigned int (unsigned integer)
size_t iis equivalent to unsigned int i;

Example:

size_t abc = 19;
cout << abc << endl;

4. typedef: define aliases for functions
. For example:

typedef string my_string;
my_string name = '自己定义的字符串类型';

If you use string again here, you can use my_string to define it. my_string is the alias of string.

5. Single-precision floating-point exercise
Question: A cylinder has a radius of 4.5cm and a height of 90cm. What is the volume of this cylinder?

#include <cmath>
	int main()
        {
        	//定义了一个float类型常量
			const float PI = 3.14f;
			float h = 90.0f;
			float radius = 4.5f;
			double volume = PI * pow(radius, 2) * h;
			cout << "体积是:" << volume << endl;
        }

6. Multi-precision floating point
exercise example: Make the output value not displayed in scientific notation, and control the number of digits after the decimal point that can be displayed

#include <iomanip>
int main(){
//输出double类型数据(不控制精度最多输出5位)
	double doublename = 10.0 / 3.0;
	
	//控制cout的显示精度(作用于范围为后面的所有输出)
	//1、强制以小数的方式显示
	cout << fixed;
	//2、控制显示的精度(#include <iomanip>)
	//控制输出小数点后n位
	cout << setprecision(10);
	double doublename = 10.0 / 3.0;
	cout << doublename * 10000 << endl;
	cout << "|" << setw(8) << 3.14 << "|" << endl;
 }

PS: Here are a few points:
(1) cout << fixed;The function is: to force display in decimal form
(2) cout << setprecision(n);The function is: to control the accuracy of the display (control the output of n digits after the decimal point)
When using this sentence, you need to quote: #include <iomanip>
(3) cout << setw(n)Used in the output stream, it means that the next character will be output with a width of 8. If it is not long enough, it will be automatically filled with spaces. Its function is to set the width of the output character (4). Its function is: used to measure the length of the data type
. sizeofExample
:

cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;

7. Comparison of floating-point precision issues
Question: Demonstrate the precision issues of single-precision floating-point and double-precision floating-point types

float numFloat = 10 / 3.0;
double numDouble = 10 / 3.0;	
cout << fixed;//让浮点型固定以数字的方式显示
cout << "numFloat = " << numFloat * 10000 << endl;
cout << "numDouble = " << numDouble * 10000 << endl;

Output result:

numFloat = 33333.332031
numDouble = 33333.333333

8. Summary of some bits and pieces of knowledge:
(1) Change the console name:

#include <Windows.h>
int main(
{
	SetConsoleTitleW(L"示例:这是我做的小Demo");
}

(2) Encapsulate comment names for parameters:
All three comment methods can add comments to the current character. This comment can be seen when the mouse enters the variable name.

/** 伤害 */
double value_attack = 57.88;
/* 伤害成长值 */
double value_attack_growth = 4.5;
// 攻击距离
double value_attack_distance = 172;

(3) Solve for the maximum value: (all uppercase is a constant)
//Print the maximum value of int type
//#define macro definition
//Import header file #include

#include <climits>
int main()
{
	cout << INT_MAX << endl;
}

Guess you like

Origin blog.csdn.net/qq_43036676/article/details/99676383