Pascal与C++对照整理版.docx

上传人:b****2 文档编号:2166693 上传时间:2022-10-27 格式:DOCX 页数:22 大小:25.60KB
下载 相关 举报
Pascal与C++对照整理版.docx_第1页
第1页 / 共22页
Pascal与C++对照整理版.docx_第2页
第2页 / 共22页
Pascal与C++对照整理版.docx_第3页
第3页 / 共22页
Pascal与C++对照整理版.docx_第4页
第4页 / 共22页
Pascal与C++对照整理版.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

Pascal与C++对照整理版.docx

《Pascal与C++对照整理版.docx》由会员分享,可在线阅读,更多相关《Pascal与C++对照整理版.docx(22页珍藏版)》请在冰豆网上搜索。

Pascal与C++对照整理版.docx

Pascal与C++对照整理版

Pascal/C/C++语句对比(补充版)

一、Helloworld

先看三种语言的样例:

Pascal

begin

writeln(‘Helloworld’);

end.

C

#include

intmain()

{

printf("Helloworld!

\n");

return0;

}

C++

#include

usingnamespacestd;

intmain()

{

cout<<"Helloworld!

"<

return0;

}

从这三个程序可以看到一些最基本的东西。

在Pascal中的begin和end,在C/C++里就是{};Pascal主程序没有返回值,而C/C++返回0(好像在C中可以为NULL)。

在C/C++中,main函数以前的是头文件,样例中C为stdio.h,C++除了iostream还有第二行的usingnamespacestd,这个是打开命名空间的,NOIP不会考这个,可以不管,只要知道就行了。

此外说明注释单行用//,段落的话Pascal为{},C/C++为/**/。

**常用头文件(模板)

#include

#include

#include

#include

#include

#include

usingnamespacestd;

intmain()

{

……

system(“pause”);

return0;

}

 

二、数据类型及定义

这里只列出常用的类型。

1、整型

Pascal

C/C++

范围

shortint

-

-128…127

integer

short

-32768…32767

longint

Int

-2147483648…2147483647

int64

longlong

-9223372036854775808…9223372036854775807

byte

-

0…255

word

unsignedshort

0…65535

longword

unsignedint

0…4294967295

qword

unsignedlonglong

0…184********709551615

**当对longlong变量赋值时,后要加LL

Longlongx=6327844632743269843LL

**如果位移x<<2LL

**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++则没有。

字符串和字符在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=‘‘;

constinta=60;

constintb=-a+30;

conststringd=“”;

defineMAXN501//这个是宏

**宏定义其实就是直接在程序相应的位置替换:

#definerandomizesrand(unsignedtime(NULL))

#definewaitfor(intw=0;w<100000;w++)

变量的定义,C/C++在定义的同时可以赋值:

Pascal

C/C++

var

a,b:

integer;

c:

char;

d:

string;

inta,b=50;

charc=‘A’;

stringd;

boolflag;

三、输入输出

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<

cout<

特别说明C++中cin一个字符的话会自动跳过空格和回车,Pascal和C则会读入空格和回车。

在Pascal中writeln(a:

n:

m)表示在n个字符宽的输出域上输出a保留m位小数。

例如:

pascalwrite(a:

6)c/c++printf(“%6d”,a)

Pascalwrite(a:

6:

2)c/c++printf(“%6.2f”,a)

C++如果用cout?

(繁琐!

需要加头文件#inlude

cout<

(2)<

cout<

以下三个进制设定都是永久作用:

cout<

cout<

cout<

例如:

cout<<12<

输出:

12c1414

C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。

下面是类型表:

%hd

1个short型整数

%d

1个int型整数

%u

1个unsignedint型整数

%I64d

1个longlong型整数

%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);

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

usingnamespacestd;

ifstreamfin(“test.in”);

ofstreamfout(“test.out”);

fin>>a>>b;

fout<

fin.close();

fout.close();

因为C++的读入较慢,个人建议C++的话使用C的输入方式。

当然也有人用C的读入,C++的输出的,这种方式我们称之为城乡结合。

**中国计算机学会竞赛须知发布的C读写程序:

(C++也能用,cin,cout,scanf,printf可混用)

#include

intmain()

{

inta,b;

freopen(“sum.in”,”r”,stdin);

freopen(“sum.out”,”w”,stdout);

scanf(“%d%d”,&a,&b);

printf(“%d\n”,a+b);

return0;

}

或者:

freopen(“sum.in”,”r”,stdin);

freopen(“sum.out”,”w”,stdout);

ios:

:

sync_with_stdio(false);\\取消同步,cin,cout的速度就不慢了!

cin>>a>>b;

cout<

return0;

 

以下扩充c/c++混用是可行的:

#include

#include

usingnamespacestd;

intmain()

{

inta,b,c,d;

freopen("sum.in","r",stdin);

freopen("sum.out","w",stdout);

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

cin>>c>>d;

printf("%d\n",a+b);

cout<

return0;

}

**如何判断文件结束(EOF)?

C++

while(cin>>s>>n)

{

...

}

C

while(scanf(%s%d",s,&n)!

=EOF)

{

...

}

四、赋值语句及运算符号

一一对应的关系

Pascal

C/C++

赋值运算

赋值

:

=

=

基本运算

+

+

-

-

*

*

除(实数)

/

/(double)

除法

取整

div

(int)/(int)

取余

mod

%

比较

等于

=

==

不等于

<>

!

=

大于

>

>

大于等于

>=

>=

小于

<

<

小于等于

<=

<=

逻辑

and

&&

or

||

not

!

位运算

左移(*2)

shl

<<

右移(/2)

shr

>>

and

&

or

|

not

~

异或

xor

^

其他

增一

inc(x)

x++

减一

dec(x)

x--

在C/C++中对某个变量自身进行运算可以简写为

变量名运算符号=改变量

如x+=8就表示x=x+8,即inc(x,8)。

在C/C++里还存在一种三目运算

变量名=条件?

值A:

值B

如x=x>0?

x:

-x;//表示若x>0则取x,否则取–x,

同ifx>0thenx:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1