AcWing Grammar Basic Class 1.1 Variables, Input and Output, Expressions and Sequential Statements

Preliminary knowledge

First, let's take a look at the simplest C++ code.
All code operations in this article are in AC Editor of AcWing

#include <iostream>

using namespace std;

int main(){
    
    
    cout << "Hello World" << endl;
    
    return 0;
}

Then use compile (click debug, then click run code, and finally click run button)

The code is mainly divided into several parts

  1. Header file area ( #include<**********> ) For example:
#include<cstdio>
#include<iostream>
  1. declare a namespace
using namespace std;

Namespaces are used to solve the problem of variable name conflicts in large projects, and variable names in different namespaces can be the same.

  1. Program execution entry
int main(){
    
    

return 0;
}

The above three parts are the common basic structure of C++, and must have the structure

Grammar Basics

1. Definition of variables

Variables are similar to the unknowns of equations in mathematics.
When defining a variable, assign the variable type.
In C++, variables have the following types:

At the same time, we must also consider the issue of bytes:
bit (bit, also known as "bit"): the abbreviation of bit is b, which is the smallest data unit in a computer (belonging to the category of binary, which is actually 0 or 1)
Byte (byte) : The abbreviation of Byte is B, which is the basic calculation unit of computer file size. For example, a character is 1Byte, if it is a Chinese character, it is 2 Byte.
In computer storage, B and b have different meanings.

B: Byte (byte)
b: bit (bit)
1 Byte = 8 bits (one byte is eight bits)
1 KB (Kilobyte) = 1024 B
1 MB (Megabyte) = 1024 KB
1 GB (Gigabyte) = 1024 MB
1TB (Terabyte) = 1024GB

The memory size occupied by different variables is different, and different memory spaces will be occupied according to different variable types.
Divided into integer, floating point, character, string, Boolean and escape characters

Integer (Integer variables represent integer type data.)

type of data take up space Ranges
short (short integer) 2 bytes (-2^15 ~ 2^15)
int (integer) 4 bytes (-2^31 ~ 2^31)
long (long integer) 4 bytes (Windows system) (-2^31 ~ 2^31)
long long 8 bytes (-2^63~ 2^63)

Floating-point type (floating-point variables represent data of decimal type)

type of data take up space
float 4 bytes
double 8 bytes

When C++ outputs multiple decimals, it will display 6 significant figures by default.
.2f means to output two decimal places, and .5f means to output five decimal places.

Character type (a character type variable can represent a single character)

type of data take up space
char 1 byte

A character variable stores the corresponding ASCII code in memory, not the character itself.
ASCII code table

string type

(1) C language: char variable name [] = "string value";
(2) C++ language: string variable name = "string value";

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    
	//C
	char c[] = "hello c!";
	cout << c << endl;

	//C++
	string cpp = "hello cpp!";
	cout << cpp << endl;
	
	return 0;
}

Boolean type (the Boolean data type represents a value of true or false)

type of data take up space
bool 1 byte

(1) true: True (the essence is 1)
(2) false: False (the essence is 0)
Note: In the bool type of C++, true or any non-zero value means true ; false or 0 value means false .

Escape character (represents some special ASCII characters that cannot be displayed directly)

Start with a backslash (\n, \t, \r, etc.)
The escape character \n is used for line breaks in C, and endl is used for line breaks in C++.

declare variables

So how to define variables?
The way of variable definition:
declare variable type + variable name

int a;
char b='a';
float c = 1.5, s=1.235e2;
double d;
int q, w = 2;
bool i = true;

Variables must be defined before they can be used. The name cannot be duplicated.
A complete variable definition program is as follows:

#include <iostream>

using namespace std;

int main()
{
    
    
    int a = 5;
    int b, c = a, d = 10 / 2;

    return 0;
}

2. Variable input and output

In the program, input and output are very important, and there are often different input and output methods for different variable types.
Below, we learn through some simple programs.

Integer input and output:

#include <iostream>

using namespace std;

int main()
{
    
    
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;//endl表示回车
    cout << a * b << endl;
    return 0;
}

String input and output:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    
    string str;
    cin >> str;
    cout << str;
    return 0;
}

Input and output multiple variables of different types:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    
    int a, b;
    string str;

    cin >> a;
    cin >> b >> str;

    cout << str << " !!! " << a + b << endl;

    return 0;
}

The direction of the arrow can be understood in this way:
the keyboard transfers the value to the cin function, and cin then transfers the value to the variable, so the direction of the input arrow points from cin to the variable, that is, >>
transfers the value from the variable to the cout function, and then cout is output to the display, so the output The arrows point to cout from the variable, that is, <<
cin and cout will filter spaces (a______b and a_b are input from cin, the result is the same)
%c will read spaces, you need to pay attention.

At the same time, scanf and printf in C language can also be used in C++ language .
The usage of these two functions is as follows:

scanf("%d%d",&a,&b);
printf("%d%d",a,b);

%d is a type control character.
insert image description here
To assign values ​​to two integer variables, write two %d, and then write two corresponding "address variables" in "input parameters"; to assign values ​​to three integer variables, write three %d, and then "input parameters" Correspondingly write three "address variables" in the middle, and keep the correspondence in quantity.

When scanf is inputting data from the keyboard, the values ​​assigned to multiple variables must be separated by spaces, carriage returns or tab keys to distinguish the values ​​​​assigned to different variables. And there is no limit to the number of space, carriage return or tab keys, as long as there are. Usually a space is used.
The complete simple program reference is as follows:

#include<cstdio>
#include<iostream>

using namespace std;

int main(){
    
    

	int a;
	int b;
	scanf("%d%d",&a,&b);
	printf("%d%d", a, b);
	
	return 0;
	
}

3. Common operation expressions

Addition, subtraction, multiplication and division

insert image description here

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    
    int a = 6 + 3 * 4 / 2 - 2;

    cout << a << endl;

    int b = a * 10 + 5 / 2;

    cout << b << endl;

    cout << 23 * 56 - 78 / 3 << endl;

    return 0;
}

In the remainder operation of C++, the positive or negative of the result depends on the first number, namely:
5 % 2 = 1
5 % (-2) = 1
-5 % (-2) = -1
-5 % 2 = -1
Note that the remainder operation is only for integer operations.

Integer increment and decrement operations :

#include <iostream>

using namespace std;

int main()
{
    
    
    int a = 1;
    int b = a ++ ;

    cout << a << ' ' << b << endl;

    int c = ++ a;

    cout << a << ' ' << c << endl;

    return 0;
}

a++: Assign first, then increment
++a: Autoincrement first, then assign If
the plus sign comes first, it will auto-increment first, and after the plus sign, it will auto-increment afterwards
a– and –a have the same principle

Variable type conversion:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    
    float x = 123.12;

    int y = (int)x;

    cout << x << ' ' << y << endl;

    return 0;
}

Guess you like

Origin blog.csdn.net/s1hjf/article/details/130155822