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

上传人:b****1 文档编号:1421322 上传时间:2023-04-30 格式:DOCX 页数:23 大小:25.56KB
下载 相关 举报
Pascal与C++对照整理版.docx_第1页
第1页 / 共23页
Pascal与C++对照整理版.docx_第2页
第2页 / 共23页
Pascal与C++对照整理版.docx_第3页
第3页 / 共23页
Pascal与C++对照整理版.docx_第4页
第4页 / 共23页
Pascal与C++对照整理版.docx_第5页
第5页 / 共23页
Pascal与C++对照整理版.docx_第6页
第6页 / 共23页
Pascal与C++对照整理版.docx_第7页
第7页 / 共23页
Pascal与C++对照整理版.docx_第8页
第8页 / 共23页
Pascal与C++对照整理版.docx_第9页
第9页 / 共23页
Pascal与C++对照整理版.docx_第10页
第10页 / 共23页
Pascal与C++对照整理版.docx_第11页
第11页 / 共23页
Pascal与C++对照整理版.docx_第12页
第12页 / 共23页
Pascal与C++对照整理版.docx_第13页
第13页 / 共23页
Pascal与C++对照整理版.docx_第14页
第14页 / 共23页
Pascal与C++对照整理版.docx_第15页
第15页 / 共23页
Pascal与C++对照整理版.docx_第16页
第16页 / 共23页
Pascal与C++对照整理版.docx_第17页
第17页 / 共23页
Pascal与C++对照整理版.docx_第18页
第18页 / 共23页
Pascal与C++对照整理版.docx_第19页
第19页 / 共23页
Pascal与C++对照整理版.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

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

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

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:

=xelsex:

=-x;

五、条件语句

1、if

C/C++中if语句的条件必须要用括号括起来,后面不使用then。

Pascal

C/C++

ifa>bthenflag:

=true

elseflag:

=false;

if(a>b)flag=true;

elseflag=false;

2、多种分支

C/C++中为switch,Pascal为case:

Pascal

C/C++

casexof

1:

inc(x);

2:

dec(x);

elsex:

=x*x;

end;

switch(x)

{

case1:

x++;break;

case2:

x--;break;

default:

x*=x;

}

切记C/C++中一定要写break,后果你可以去掉break,运行看看就知道了。

六、循环语句

1、for

Pascal

C/C++

for变量名:

=初始值to(downto)终止值do

for(变量名=初始值;条件;改变方式)

fori:

=5to10dodec(a);

//终止值大于初始值用to

fori:

=5downto1dodec(a);

//终止值小于于初始值用downto

for(i=5;i<=10;i++)a--;

for(i=5;i>=1;i--)a--;

/*只要i满足条件就会一直循环。

C/C++中i是实数、指针都可以*/

C/C++中for的特殊用法:

//变量为实数

for(doublei=1;i<=2;i*=1.01)

k++;

//变量为指针,->符号为间接引用,后面会提到。

for(type1*p=head->next;p;p=p->next)

printf(“%d”,p->k);

2、while

Pascal

C/C++

while条件do

while(条件)

whilei<>0dodec(i);

while(i!

=0)i--;

//也可写作while(i)i--;

//在C/C++中非0即为真。

3、repeat-until&do-while

Pascal

C/C++

repeat语句until结束条件;

do{}while(运行条件)

repeatint(i)untili>100;

do{i++;}while(i<=100);

七、数组

Pascal中数组的下标可以随意定义,而C/C++下标始终为从0开始到(数组大小–1)。

Pascal

C/C++

定义

a:

array[1..100]ofinteger;

b:

array[1..10,1..10]ofint64;

inta[100];

intb[10][10];

含义

a为大小为100的integer数组,合法下标为1到100

b为大小为10*10的int64数组,合法下标为1,1到10,10

a为大小为100的int数组,合法下标为0到99

b为大小为10*10的int数组,合法下标为0,0到9,9;

使用

inc(a[21]);

b[2,2]:

=b[1,1]+b[1,2]+b[2,1];

a[21]++;

b[1][1]=b[0][1]+b[0][0]+b[1][0];

数组清零

Pascal

C/C++

Fillchar(a,sizeof(a),0);

memset(a,0,sizeof(a));

//头文件包含string.h

**如果要填最大:

memset(a,127,sizeof(a))(但达不到INT_MAX)

如果要填最小:

memset(a,128,sizeof(a))(但达不到INT_MIN)

如果填0:

memset(a,0,sizeof(a))

如果填-1:

memset(a,-1,sizeof(a))

八、字符串

C风格的字符串就是字符数组。

C++和Pascal的字符串使用基本相同,只是C++中字符串下标以0开始,Pascal以1开始。

字符串处理很多这里不一一列举,只写最常用的几个。

 

Pascal

C(包含

定义用:

chars[]

C++(包含

定义用:

strings

输入

Readln(s);

Writeln(s);

Scanf(“%s”,s);

Printf(“%s\n”,s);

注:

不能输入输出c++的字符串

Cin>>s;

Cout<

注:

可以输入输出c的字符串

查找

pos(‘a’,s);//不存在返回0

没有

s.find(‘a’);//不存在返回-1

len=length(s);

Strlen(s)

len=s.size();或

Len=s.length();

复制

copy(st,pos,num);

st:

=‘abcde’;

s:

=copy(st,3,2);

//s=‘cd’

Strcpy(s1,s2)

全部复制

Strncpy(s1,s2,n)

前n个复制

但没有从第几个开始的!

substr(pos,n)//返回从pos开始的长度为n的子串;

strings1=“abcde”,s2;

s2=s1.substr(2,2);

//s2=“cd”

插入

insert(obj,target,pos);

st:

=‘helloworld’;

st:

=insert(‘‘,st,6);

//st=‘helloworld’

没有

insert(pos,s)//在pos位置处插入字符串s;

strings1=“0123”;

s1.insert(1,“XYZ”);//s1=“0XYZ123”

删除

delete(st,pos,num);

st:

=‘helloworld’;

st:

=delete(st,6,1);

//st=‘helloworld’

没有

erase(pos,n)//从pos位置开始删除n个字符;

strings1="abcdefghi";

s1.erase(5,3);//得到"abcdei"

C++还有以下功能:

用s.replace(2,2,"ttt")可以部分替换

用s.empty()判断是否为空

可访问s[i],位置从0算起

可以s1+s2

可以s1=s2

可以比较s1==s2当然><=>=<=!

=都可以比较。

C++字符串整串读入:

getline(cin,s)和cin>>s的区别:

getline(cin,s)

cin>>s

一次性整行读入,直至行末尾。

只读入一个“单词”,遇空格和行末停止。

例如输入;Howareyou?

s=”Howareyou?

读入整串含空格

例如输入;Howareyou?

s=”How”

如果三个都读:

cin>>s1>>s2>>s3

**C++数字与数值之间的转换:

#include

#include

#include//必须加入

usingnamespacestd;

intmain()

{

stringtext="152";

intnumber;

stringstreamss;

ss<

ss>>number;//string->int

cout<

ss<string

stringstr=ss.str();

return0;

}

九、过程和函数

1、过程

在C/C++中没有过程,但可以把返回值为“空”的函数理解为过程。

Pascal

C/C++

无参过程

procedure过程名;

说明部分

begin语句部分end;

//说明部分、begin、end语句部分统称为过程体

void函数名();

{

主体部分;

return;

}

带参过程

procedure过程名(形参表)

过程体

void函数名(形参表)

过程体

值传和址传:

当一个参数是值传时,形参在子过程中相当于一个局部变量,对它的改变不影响实在的参数值。

址传则会影响。

下例中a为值传,b为址传。

初始a=5,b=5,运行后a=5,b=10;

Pascal

C/C++

vara,b:

integer;

proceduredoit(a:

integer;varb:

integer);

begin

b:

=a+b;

a:

=a+b;

end;

begin

a:

=5;

b:

=5;

doit(a,b);

writeln(a,‘‘,b);

end.

voiddoit(inta,int&b)

{\\a认为值参,b认为变量传参

b+=a;

a+=b;

return;

}

intmain()

{

inta=5,b=5;

doit(a,b);

cout<

return0;

}

**用若干地址传参可以给调用者传回若干值

Voidtryit(int&x,int&y,int&z)

调用时:

tryit(a,b,c),可以传回a,b,c的值。

**用数组名(也是地址)传参可以传回整组的数据

Voidtryit(inta[])

调用时:

tryit(x),可以传回整个数组。

例如:

voidtryit(inta[])

{

for(inti=0;i<=10;i++)a[i]=i*2;

return;

}

intmain()

{

intx[10];

tryit(x);

for(inti=0;i<=10;i++)

cout<

system("pause");

return0;

}

**用指向函数的指针作为参数,可以执行指定的函数。

(略)

STL的两个应用:

**C++快排函数

#include

Boolcom(inta,intb)

{

Returna>b;

}

Intmain()

{

Inta[10]={5,7,3,2,6,8,4,3,5,7};

Sort(a,a+10,com);//如果升序可以省略com.

For(inti=0;i<10;i++)

Cout<

}

 

**优先队列(以堆排为例)

#include

#include

usingnamespacestd;

pri

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2