c++习题Word文件下载.docx
- 文档编号:17747210
- 上传时间:2022-12-09
- 格式:DOCX
- 页数:26
- 大小:24.81KB
c++习题Word文件下载.docx
《c++习题Word文件下载.docx》由会员分享,可在线阅读,更多相关《c++习题Word文件下载.docx(26页珍藏版)》请在冰豆网上搜索。
for(inti=2;
i<
=n;
i++)
fac*=i;
returnfac;
"
Pleaseinputn:
"
intn;
cin>
>
n;
Resultoff("
<
n<
)is"
factorial(n)<
return0;
C++assignment
(2)
(1)Inspectthisprogram:
charc;
for(c=-128;
c<
127;
c++){
("
(int)c<
"
c<
)"
}
getchar();
Whatistheoutput?
Thedatatypecharcanbeconsideredasintegerwithsinglebyte.Howtooutputacharvariableasaninteger?
Howtooutputanintegerasachar?
WhatisASCII?
(2)Inspectthisprogram:
intmain()
{
intnum;
num<
num=2;
(num<
1)<
intn=5;
if(num==2&
&
n==5)
good"
num=-2;
num<
=1;
if(num==-4&
anothergood"
Canyouimagintheoutput?
Isyourideacorrect?
Ifavariableisnotinitialized,whatisitsvalue?
Leftshiftoperationandstandardoutputoperationarebothrepresentedas<
so,howtodistinguishthem?
Leftshiftby1bitmeansmultiplyingby2,doesn'
tit?
Howaboutrightshiftoperation?
(3)Writeafunctionthatoutputsacharvariableasabinarysequence。
(4)Outputaminusvalueasabinarysequence.Makeclearwhatthetwo'
s-complementis,andcheckitactually.
(5)Writeafunctionthatoutputsafloatvariableasabinarysequence。
(Pointersareusedinthisfunction.Asktheteachingassistantfordetail.)
(6)Writeaprogramthatoutputsthegreatestcommondivisoroftwointegersandtheleastcommonmultipleoftwointegers.
Sourcecode:
outputafloatasabinarysequence
(2013-03-2020:
16:
20)
分类:
Cpp
Everyone!
Thefollowingisthesamplesourcecodeforoneproblemgiventheotherday:
howtooutputafloatvariableasabinarysequence.Pleasestudyitandcomparewithyourworks.
//outputasinglebyteasabinarysequence
voidBinaryOut(charc){
unsignedchartmp=0x80;
for(inti=0;
8;
i++){
if(c&
tmp)
1;
else
0;
tmp>
//outputseveralcontiguousbytesformagivenaddress,withthehighestbyteontheleftmost
voidshow(char*p,intlen){
for(inti=len-1;
i>
=0;
i--){
BinaryOut(*(p+i));
floatd=-28.0;
show((char*)&
d,sizeof(d));
//usingtype-casttofetchbytes
C++assignment(3)
(2013-03-2514:
46:
37)
Hi,
intx=5,y=8;
x=sizeof(y++);
x<
y<
Isitthesameasyouhaveexpected?
Doyouknowtheconcepts"
Compile-time"
and"
Run-time"
?
(2)Inspectthisprogram:
charch=-1;
BinaryOut(ch);
ch=127;
Theprogramcanoutputacharvariableasabinarysequence.Ifwechangethedatatypeoftmpintochar,doesthisprogramstillwork?
Whynot?
Howtooutputavariableofarbitrarytypeasabinarysequence?
(3)Inspectthisprogram:
#include<
string>
Whatisyourname?
strings;
s;
Hello"
s<
ThereisaclassnamedstringintheC++standardlibrary,whosefunctionalityisprovidingsomeusefuloperationswithtextstrings.Thisisonlyaverysimpleexample.YoucanconsultC++documentationfordetail.
(4)Inspectthisprogram:
//CStringExample
cstring>
chars[]="
hello"
chart[]={'
w'
'
o'
r'
l'
d'
!
'
\0'
};
//modifythestringsdirectly,replacehwithH
s[0]='
H'
//comparestrings
if(strcmp(t,"
world!
)==0)
{
strcpy(t,"
World!
);
//notecapitalW
char*u=strcat(s,t);
//willoutput"
HelloWorld!
std:
:
cout<
u<
Thisstringis"
strlen(u)
characterslong."
ThisisanexampleabouttraditionalC-stylestring.Pleasenotethattheheaderfileiscstring,andyoucanusestring.hinstead.Inthisheaderfile,somefamousstring-relatedfunctionsstartingwithstraredeclared,suchasstrlen(),strcat(),strcpy()andsoon.Youcantrymoreofthembyyourself。
(5)Inspectthisprogram:
//Usingvariableswithautomaticconversion
iostream.h>
intx,y;
x=6.23;
//xbecomes6
x="
\n"
charc='
//(see<
coid="
varinit-co"
linkends="
varinit"
/>
)
c="
x=c;
//xbecomestheinteger
//equivalentof‘w’whichis119
y=2.110;
//ybecomes2
doubled;
varintro-co"
varintro"
)
d=y;
//dbecomes2.0
d="
d<
constfloatpi=3.14159;
varconst-co"
varconst"
//pi=223.34;
//wouldbeanerror
Youcangetsomeideaoftheprincipleoftypecast(typeconvertion)inC++.
(6)Inputsomeparagraphsthroughkeyboard,andcalculatethelinesandwordscontainedinthem.
Hint:
usecin.get()ratherthancin>
toreadcharacters.
窗体顶端
(2013-04-0109:
44:
40)
usingstd:
cout;
cin;
intmain(){
intyear;
cout<
Inputyear:
cin>
year;
if(year%4==0&
year0!
=0
||year@0==0)
year<
isaleapyear"
elsecout<
year<
isnotaleapyear"
Isitabsolutelynecessarytowrite"
usingnamespacestd"
whenreferencesomeidentifierinstd(suchascin,cout,andsoon)?
Infact,ifyoujustneedtousecininyourprogram,youcanuse"
tointroduceitseperately.Youcantrythewaytointroducesomeotheridentifierinthenamespacestd.
floatnum1,num2;
charop;
cout<
Inputoperand1,perator,operand2:
cin>
num1>
op>
num2;
switch(op){
case'
+'
num1<
op<
num2<
="
num1+num2<
break;
-'
num1-num2<
*'
num1*num2<
/'
num1/num2<
default:
isaunknownoperator!
Thisisamini-calculatorprogram.Pleasemodifyit,andwhenweinput"
2^3"
itcangivetheresultof3rdpowerof2。
constintn=100;
//nisaconstantvariable
inti=1,sum=0;
//Initializationforloop
while(i<
=n){
sum+=i;
i++;
//modificationtocontrollingvariable
sum="
sum<
Thisprogramcangivetheresultof"
1+2+...+100"
.Pleasewriteafunction:
intsum(intn),togivetheresultof"
1+2+...+n"
.And,invokeitinmain()tocalculatethesummationfrom1to100.
(4)Insepcttheprogram:
inti,num,subscript;
intdigit[5];
Inputainteger:
num;
Theoriginalnumberis:
num<
subscript=0;
//subscriptofthearray
do{digit[subscript]=num;
num=num/10;
subscript++;
}while(num>
0);
for(i=0;
i
num=num*10+digit[i];
Thenumberafterreversionis:
return0;
Thisisaprogramtooutputaintegerreversely.Pleasemodifytheprogramto:
(1)Withoutusingarray,andcandealwithintegerswitharbitrarylength;
(2)Usingonlyoneloop.
(5)Inspecttheprogram:
constintm=5;
intA[m]={};
inti;
A[i]<
intB[m]={1,2};
B[i]<
Isitthesamewithwhatyou'
veexpeted?
Whatistheruletoinitiateanarryusing"
{}"
(6)Inspecttheprogram:
boolbvar;
inti=5,j=5;
bvar=i==j;
bvar<
bvar+i<
Infact,operator==takesprecedenceoveroperator=,andthevalueofaboolvariableisfrom{0,1}.
(7)Writeafunction:
intmax3(intx,inty,intz)toreturnthemaximumofthreeintegers,usingoperator?
.
C++assignment(5)
(2013-04-0814:
37:
36)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 习题