C++ study notes

Understand the instructions

#include The main function of the preprocessor is to add or replace the corresponding precompiled instructions before the compiler compiles the source code, so that the system predefined functions and various identifiers called in the user source code can be correctly The compiler recognizes and compiles. #include< iostream> means to add the iostream header file to the current order source code. The iostream header file mainly contains the system's standard input and output functions and data declarations and definitions

Understand using namespace std;

The main function of the using precompiler is to indicate that the namespace used by the current source code file is std, and the namespace is in the C++ language. In order to solve the problem of writing large programs, independent C++ codes of multiple vendors may conflict during the identifier naming process. A solution for, that is, if a variable a represents 1 and 2 in two codes, an error will be reported during code integration. The code modules of different vendors have their own code modules, and the user is using This module also needs to clearly mark which manufacturer's code module you are using. The using precompiled instruction can achieve this function and solve the above problem.

pointer

Computer program storage information can be realized by pointers. A pointer is a variable and it is not a certain value, or the value it stores is the address of another value. To find the address of a regular variable, you can add it in front of it. The address operator &, see the following example:

Insert picture description here
The address is displayed in hexadecimal notation. If you have defined a pointer, if you add * (dereference operator) in front of the pointer, you can get the value stored at the address corresponding to the pointer variable, see below An example of, we learn how to define a pointer and understand the asterisk (exactly the same as the multiplication symbol, don't worry too much, C++ will determine whether to multiply or dereference according to the context):

#include<iostream>
using namespace std;
int main()
{
    
    
	int m=2;
	//int*n;
	//int *n;
	//int* n;
	int * n;
	n=&m;
	cout<<n<<' '<<*n<<endl;
	return 0;
}

Output:

0x6ffe04 2

The space before and after the asterisk is optional, that is, you can add it or not. The three lines commented out are correct. For the declaration pointer, you can use the same value as the address stored by adding an asterisk before it. The type is, in fact, what we define is another way of representing that value (*n). It is worth noting that an asterisk can only correspond to a pointer, namely:

int *n,h;

A pointer n and an integer variable h are defined. If h is also a pointer, you should:

int *n,*h;

?:

Statement:

z=x<y?x:y;

The meaning is to take the smaller value of x and y

Empty statement

example:

if(c>1);//空语句 ;
else
	cout<<"Good"<<endl;

switch statement

//结构
switch(<变量>)
{
    
    
	case <判断1>:
		<执行1>;
		break;//终止
	case <判断2>:
		<执行2>;
		break;
	...
	default://前面的都没有执行
		<执行n+1>;
		break;
}

A simple example, input the month of the average year, and output the corresponding number of days:

//输入月份,输出月份的天数
#include <iostream>
using namespace std;
int main() {
    
    
	int month;
	cout<<"Please input the month:"; //输出提示信息
	cin>>month; //输入月份
	if (month<1 || month >12) {
    
    
		cout<<"输入错误!"<<endl;
	return 1;
	}
	switch(month) {
    
    
		case 2:
			cout<<"28天"<<endl;
			break;
		case 4: case 6: case 9: case 11://多种情况同时判断
			cout<<"30天"<<endl;
			break;
		default://31天放在最后用 default 简化代码
			cout<<"31天"<<endl;//最后可以不用 break 了
	}
	return 0;
}

Without break, use the switch's multiple entries to solve the problem:
enter the year, month, and day, and output this is the day of the year:

#include<iostream>
#include<cstdio>
int main()
{
    
    
	int y,m,d,e,su=0;
	while(scanf("%d%d%d",&y,&m,&d) !=EOF){
    
    
		if(y%4==0 && y&100!=0 || y%400==0)
			e=29;
		else
			e=28;
		switch(m){
    
    
			case 12:
				su+=30;
			case 11:
				su+=31;
			case 10:
				su+=30;
			case 9:
				su+=31;
			case 8:
				su+=31;
			case 7:
				su+=30;
			case 6:
				su+=31;
			case 5:
				su+=30;
			case 4:
				su+=31;
			case 3:
				su+=e;
			case 2:
				su+=31;
			case 1:
				;
		}
		su+=d;
		printf("%d\n",su);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113729800