1、
Pascal/C/C++语句对比(补充版)
一、Hello world
先看三种语言的样例:
Pascal
begin
writeln(‘Hello world’);
end.
C
#include
2、
从这三个程序可以看到一些最基本的东西。在Pascal中的begin和end,在C/C++里就是{};Pascal主程序没有返回值,而C/C++返回0(好像在C中可以为NULL)。在C/C++中,main函数以前的是头文件,样例中C为stdio.h,C++除了iostream还有第二行的using namespace std,这个是打开命名空间的,NOIP不会考这个,可以不管,只要知道就行了。
此外说明 注释单行用//,段落的话Pascal为{},C/C++为/* */。
** 常用头文件(模板)
#include 3、io>
#include 4、 2147483647
int64
long long
-9223372036854775808 … 9223372036854775807
byte
-
0 … 255
word
unsigned short
0 … 65535
longword
unsigned int
0 … 4294967295
qword
unsigned long long
0 … 18446744073709551615
** 当对long long 变量赋值时,后要加LL
Long long x=6327844632743269843LL
** 如果位移 x<<2L 5、L
** Linux: printf(“%lld\n”,x);
** Windows: printf(“%I64d\n”,x);
2、实型
Pascal
C/C++
范围
real
float
2.9E-39 … 1.7E38
single
-
1.5E-45 … 3.4E38
double
double
5.0E-324 … 1.7E308
3、字符即字符串
字符在三种语言中都为char,C里没有字符串,只有用字符数组来代替字符串,Pascal和C++均为string。Pascal中字符串长度有限制,为255,C++则没有。
6、 字符串和字符在Pascal中均用单引号注明,在C/C++中字符用单引号,字符串用双引号。
4、布尔类型
Pascal 中为 boolean,C/C++ 为 bool。值均为True 或 False。C/C++中除0外bool都为真。
5、定义
常量的定义均为 const,只是在C/C++中必须要注明常量的类型。在C/C++中还可以用宏来定义常量,此时不注明类型。
Pascal
C/C++
const
a = 60;
b = -a + 30;
d = ‘‘;
const int a = 60;
const int b = - a 7、 + 30;
const string d = “”;
define MAXN 501 //这个是宏
** 宏定义其实就是直接在程序相应的位置替换:
#define randomize srand(unsigned time(NULL))
#define wait for(int w=0;w<100000;w++)
变量的定义,C/C++在定义的同时可以赋值:
Pascal
C/C++
var
a, b : integer;
c : char;
d : string;
int a, b = 50;
char 8、 c = ‘A’;
string d;
bool flag;
三、输入输出
C/C++中没有以回车作为结束的读入方式(就本人所知)。”\n”表示换行。常规输入输出:
Pascal
C
C++
read(a); //读入变量a
readln(a); //读入变a,回车结束
write(a); // 输出a
writeln(a); //输出a 并换行
scanf(“%d”, &a);
printf(“%d”, a);
printf(“%d\n”, a);
cin >> a;
cout << a;
cout << a << endl;
特别 9、说明C++中cin一个字符的话会自动跳过空格和回车,Pascal和C则会读入空格和回车。在Pascal中writeln(a:n:m) 表示在n个字符宽的输出域上输出a保留m位小数。
例如:pascal write(a:6) c/c++ printf(“%6d”,a)
Pascal write(a:6:2) c/c++ printf(“%6.2f”,a)
C++ 如果用 cout ? (繁琐!!)
需要加头文件 #inlude 10、cout < 11、型。下面是类型表:
%hd
1个short型整数
%d
1个int型整数
%u
1个unsigned int型整数
%I64d
1个long long型整数
%c
1个字符
%s
1个C字符串
%f
1个float型实数
%lf
1个double型实数
%10.4f
输出1个总宽度为10,保留4位小数的实数
文件输入输出:
Pascal
assign(input, ‘test.in’);
assign(output, ‘test.out’);
reset(input);
rewrite(output);
read(a, b) 12、
writeln(a, b);
close(input);
close(output);
C
FILE *fin = fopen(“test.in”, “r”);
FILE *fout = fopen(“test.out”, “w”);
fscanf(fin, “%d%d”, &a, &b);
fprintf(fout, “%d%d”, a, b);
fclose(fin);
fclose(fout);
C++
#include 13、out(“test.out”);
fin >> a >> b;
fout << a << b << endl;
fin.close();
fout.close();
因为C++的读入较慢,个人建议C++的话使用C的输入方式。当然也有人用C的读入,C++的输出的,这种方式我们称之为城乡结合。
**中国计算机学会竞赛须知发布的C读写程序:
(C++ 也能用,cin,cout,scanf,printf 可混用)
#include 14、
freopen(“sum.out”,”w”,stdout);
scanf(“%d%d”,&a,&b);
printf(“%d\n”,a+b);
return 0;
}
或者:
freopen(“sum.in”,”r”,stdin);
freopen(“sum.out”,”w”,stdout);
ios::sync_with_stdio(false); \\取消同步,cin,cout的速度就不慢了!!
cin>>a>>b;
cout<






