Pascal to C ++
Introduce
On November 1, 2016, the Chinese Computer Society released the "CCF Announcement on the Change of Programming Language of NOI Series", which reads as follows:
根据国际信息学奥林匹克竞赛(IOI)的相关决议并考虑到我国目前程序设计语言的具体情况,CCF决定:
1.2020年开始,除NOIP以外的NOI系列其他赛事(包括冬令营、CTSC、APIO、NOI)将不再支持Pascal语言和C语言;
2.从2022年开始,NOIP竞赛也将不再支持Pascal语言。即从NOIP2022开始,NOI系列的所有赛事将全部取消Pascal语言。
在无新增程序设计语言的情况下,NOI系列赛事自NOIP2022开始将仅支持C++语言。
此通知。
中国计算机学会
2016年11月1日
So many Pascal players started to switch to C ++.
Prepare and compare
1. C ++ is more complicated and harder to learn, but more powerful.
2. After you have learned Pascal, learning C ++ is relatively simple. (C to C ++ is easier)
3. The difference between the C ++ versions is very large, and it is easy to compile errors.
It is recommended that you use Dev-cpp 5.6.1, which is gcc / g ++ 4.8.1.
The version closest to the game.
The version of the game is 4.8.4 ( details of the version of the game ).
Never use Dev-cpp 5.9.x or 5.11.x (many big guys explode to zero).
4. The suffix name of the
C ++ code. Cpp cpp = C Plus Plus = C ++
Simple conversion
1. C ++ is composed of functions.
2. C ++ braces {and} replace begin and end.
3. The types of functions and variables are written in the front, int is a longint type.
4. Each sentence ends with a semicolon. If it encounters certain brackets (braces, parentheses in the function parameter table, etc.), the semicolon is omitted.
5. The last sentence of the main program is
return 0;
Best to add , there are certain benefits.
head File
It is generally recommended that beginners use C ++ universal header files
#include <bits/stdc++.h>
The C ++ universal header file contains almost all header files.
input Output
Input sentence
输入语句 cin>>变量1>>变量2>>变量3;
Output statement
cout<<变量1<<变量2<<...;
cout<<"hello world" << endl;
//endl是换行
cin and cout are in the <iostream>
header file, and the standard namespace is also requiredusing namespace std;
Field width:
cout<<setw(5)<<a;
Keep a few decimal places:
cout<<fixed<<setprecision(3)<<a;
The function is in the <iomanip>
header file.
Other input and output sentences are scanf()
sum, printf()
etc. Generally, it is not recommended for beginners to learn, and you must master it when you are proficient .
a + b code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
Comment
Comments
In C ++ code, there are two ways to write comments:
1 In-line comments
Start with //, and all the content after the line are comments.
//这里添加注释
2 The comment block
starts with / * and ends with * /. The contents in the middle are all comments and can span lines.
/*
这里添加注释1
这里添加注释2
*/
Comments have no effect on the operation of the program and can be used to explain the meaning of the program.
In project development, notes can be used for future maintenance and reading by others.
In OI, few people write many comments, but comments can make it easier to clarify ideas when writing code, or to review later. Moreover, if you want to write a solution, a tutorial, the appropriate amount of comments can facilitate the reader to read and understand the intent of the code.
variable
Types of variables
整数:
int (longint/int32)
long long (int64)
unsigned int (dword)
unsigned long long (qword)
小数:
float (real)
double (double)
Character:
char (char)
Boolean type:
bool (boolean)
String:
string (string)
No type of
void (?)
Definition of variables
int a,b;
char ch='c';
double f;
int a[100];
char mat[50][50];
Array:
int a [100];
// indicates that a [0] -a [99] are all int types.
Two-dimensional array int a [100] [100];
// defines a [0] [0]- a [99] [99]
initialization:
int a[5]={1,2,3,4,5};
Global and local variables
Inside the function are local variables, and outside the function are global variables.
The function includes the main function main ().
Scope
Scopes are blocks of code where variables can play a role.
Variables are divided into global variables and local variables, the meaning has been explained above.
The scope of the global variable is the entire file. Once the global variable is declared, it is available in the entire program.
C ++ can define variables anywhere, and the scope of variables is only below it.
constant
Constants are fixed values and will not change during program execution.
The value of the constant cannot be modified after it is defined. Just add a const keyword when you declare.
int const N=105;
or
const int N=105;
Assignment statement
a=b; ( a:=b )
i++ 或 ++i ( inc(i) )
i+=a ( inc(i,a) )
i-- 或 i-- ( dec(i) )
i-=a ( inc(i,a) )
if condition judgment statement
Format 1:
if (条件) 语句;
if (条件) {
语句1
语句2
......
}
Format 2:
if (条件) 语句1;
else 语句2;
if (条件) {
......
} else {
......
}
Boolean operator:
and
Is &&
or
is ||
not
is!
Comparison operators:
> 大于
>= 大于等于
< 小于
<= 小于等于
== 等于
!= 不等于
loop statement
for loop
format:
for (初始操作; 循环的条件; 变化) {
语句1;
语句2;
}
step:
Example:
Find 1 + 2 + …… + 100
int s=0;
for(int i=1; i<=100; i++) s+=i;
while loop
while:
while(条件) {
语句1;
语句2;
}
do while :
do {
语句1;
语句2;
} while(条件)
function
Mathematical functions (own functions)
abs () absolute value of integer
fabs () absolute value of decimal
ceil () rounded up
floor () rounded down
Are in <cmath>
or <math.h>
header files.
Sort function
Sort a [1] to a [n] from small to large
sort(a+1,a+n+1);
a + 1 is the memory address of a [1], and the end must be 1 more.
If a [0] -a [n-1] is sorted:
sort(a,a+n);
sort () is a quick sort, in the <algorithm>
header file.
Custom function
Beginners generally recommend writing above the main function.
类型 函数名(参数表) {
...
return 答案;
}
Examples:
int plus(int a,int b) {
int ans;
ans=a+b;
return ans;
}
Untyped functions:
void 函数名(参数表) { //void就是空,无类型=
.....
不需要return
}
Questions about characters and ASCII codes
ASCII code table: https://blog.csdn.net/Ljnoit/article/details/102649314
int a;
char x='A';
a=x; //a就是65了
x=66; //x就是'B'
C ++ can be forced to convert:
(类型)变量
cout<<(int)x<<endl;
String
Character array
A one-dimensional character array can be regarded as a string. For
example: char s [105];
Read a string cin >> s;
Note: When cin is read, it is read from one visible character to another visible character
Spaces and carriage returns are not read
If you read a whole line with gets (s)
Output character
cout<<s;
puts(s); //他会换行
Character is single quoted string is double quoted
The string has a terminator.
If s reads abc,
then s [0] = 'a'
s [1] = 'b'
s [2] = 'c'
s [3] = '\ 0';
'\ 0' means empty character (not space)
1. Find the length strlen
int x=strlen(s);
//只计算从s[0]到\0有几个字母
2. Assignment
strcpy(s1,s2);
//把s2 赋值给s1
3. Connection operation
strcat(s1,s2);
//把s2接到s1的后面
4. Compare sizes
strcmp(s1,s2)
The return value is greater than 0, indicating that the dictionary order s1> s2
is equal to 0, indicating that the
result of s1 = s2 is less than 0, indicating that the dictionary order s1 <s2
Other functions are written by yourself.
string type
(Generally not used because it is too slow)
Miscellaneous
In C ++, 0 is false, and non-zero is true.
Congratulations on your successful transformation and upgrading!
Talk about Pascal to C ++: https://blog.csdn.net/Ljnoit/article/details/104960875