Pascal与C++对照整理版.docx
《Pascal与C++对照整理版.docx》由会员分享,可在线阅读,更多相关《Pascal与C++对照整理版.docx(22页珍藏版)》请在冰豆网上搜索。
Pascal与C++对照整理版
Pascal/C/C++语句对比(补充版)
一、Helloworld
先看三种语言的样例:
Pascal
begin
writeln(‘Helloworld’);
end.
C
#include
intmain()
{
printf("Helloworld!
\n");
return0;
}
C++
usingnamespacestd;
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#includeusingnamespacestd;intmain(){……system(“pause”);return0;} 二、数据类型及定义这里只列出常用的类型。1、整型PascalC/C++范围shortint--128…127integershort-32768…32767longintInt-2147483648…2147483647int64longlong-9223372036854775808…9223372036854775807byte-0…255wordunsignedshort0…65535longwordunsignedint0…4294967295qwordunsignedlonglong0…184********709551615**当对longlong变量赋值时,后要加LLLonglongx=6327844632743269843LL**如果位移x<<2LL**Linux:printf(“%lld\n”,x);**Windows:printf(“%I64d\n”,x);2、实型PascalC/C++范围realfloat2.9E-39…1.7E38single-1.5E-45…3.4E38doubledouble5.0E-324…1.7E3083、字符即字符串字符在三种语言中都为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++中还可以用宏来定义常量,此时不注明类型。PascalC/C++consta=60;b=-a+30;d=‘‘;constinta=60;constintb=-a+30;conststringd=“”;defineMAXN501//这个是宏**宏定义其实就是直接在程序相应的位置替换:#definerandomizesrand(unsignedtime(NULL))#definewaitfor(intw=0;w<100000;w++)变量的定义,C/C++在定义的同时可以赋值:PascalC/C++vara,b:integer;c:char;d:string;inta,b=50;charc=‘A’;stringd;boolflag;三、输入输出C/C++中没有以回车作为结束的读入方式(就本人所知)。”\n”表示换行。常规输入输出:PascalCC++read(a);//读入变量areadln(a);//读入变a,回车结束write(a);//输出awriteln(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?(繁琐!!)需要加头文件#inludecout<(2)<cout<以下三个进制设定都是永久作用:cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
从这三个程序可以看到一些最基本的东西。
在Pascal中的begin和end,在C/C++里就是{};Pascal主程序没有返回值,而C/C++返回0(好像在C中可以为NULL)。
在C/C++中,main函数以前的是头文件,样例中C为stdio.h,C++除了iostream还有第二行的usingnamespacestd,这个是打开命名空间的,NOIP不会考这个,可以不管,只要知道就行了。
此外说明注释单行用//,段落的话Pascal为{},C/C++为/**/。
**常用头文件(模板)
……
system(“pause”);
二、数据类型及定义
这里只列出常用的类型。
1、整型
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、实型
real
float
2.9E-39…1.7E38
single
1.5E-45…3.4E38
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++中还可以用宏来定义常量,此时不注明类型。
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++在定义的同时可以赋值:
var
a,b:
integer;
c:
char;
d:
string;
inta,b=50;
charc=‘A’;
stringd;
boolflag;
三、输入输出
C/C++中没有以回车作为结束的读入方式(就本人所知)。
”\n”表示换行。
常规输入输出:
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?(繁琐!!)需要加头文件#inludecout<(2)<cout<以下三个进制设定都是永久作用:cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
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?(繁琐!!)需要加头文件#inludecout<(2)<cout<以下三个进制设定都是永久作用:cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
特别说明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<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
(2)<cout<以下三个进制设定都是永久作用:cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
cout<以下三个进制设定都是永久作用:cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
以下三个进制设定都是永久作用:
cout<cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
cout<cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
cout<例如:cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
cout<<12<输出:12c1414C的输入输出里面的字符串中%表示变量,%后面的字目表示变量类型。下面是类型表:%hd1个short型整数%d1个int型整数%u1个unsignedint型整数%I64d1个longlong型整数%c1个字符%s1个C字符串%f1个float型实数%lf1个double型实数%10.4f输出1个总宽度为10,保留4位小数的实数文件输入输出:Pascalassign(input,‘test.in’);assign(output,‘test.out’);reset(input);rewrite(output);read(a,b);writeln(a,b);close(input);close(output);CFILE*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++#includeusingnamespacestd;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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
输出:
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位小数的实数
文件输入输出:
assign(input,‘test.in’);
assign(output,‘test.out’);
reset(input);
rewrite(output);
read(a,b);
writeln(a,b);
close(input);
close(output);
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);
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可混用)#includeintmain(){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#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
fin.close();
fout.close();
因为C++的读入较慢,个人建议C++的话使用C的输入方式。
当然也有人用C的读入,C++的输出的,这种方式我们称之为城乡结合。
**中国计算机学会竞赛须知发布的C读写程序:
(C++也能用,cin,cout,scanf,printf可混用)
inta,b;
freopen(“sum.in”,”r”,stdin);
freopen(“sum.out”,”w”,stdout);
scanf(“%d%d”,&a,&b);
printf(“%d\n”,a+b);
或者:
ios:
:
sync_with_stdio(false);\\取消同步,cin,cout的速度就不慢了!
cin>>a>>b;
cout<return0; 以下扩充c/c++混用是可行的:#include#includeusingnamespacestd;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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
以下扩充c/c++混用是可行的:
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){...}Cwhile(scanf(%s%d",s,&n)!=EOF){...}四、赋值语句及运算符号一一对应的关系PascalC/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:
**如何判断文件结束(EOF)?
while(cin>>s>>n)
...
while(scanf(%s%d",s,&n)!
=EOF)
四、赋值语句及运算符号
一一对应的关系
赋值运算
赋值
=
基本运算
加
+
减
乘
*
除(实数)
/
/(double)
除法
取整
div
(int)/(int)
取余
mod
%
比较
等于
==
不等于
<>
!
大于
>
大于等于
>=
小于
<
小于等于
<=
逻辑
且
and
&&
或
or
||
非
not
位运算
左移(*2)
shl
<<
右移(/2)
shr
>>
&
|
~
异或
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:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1