这学期c++习题电子科大版.docx
- 文档编号:10619080
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:25
- 大小:21.10KB
这学期c++习题电子科大版.docx
《这学期c++习题电子科大版.docx》由会员分享,可在线阅读,更多相关《这学期c++习题电子科大版.docx(25页珍藏版)》请在冰豆网上搜索。
这学期c++习题电子科大版
这学期c++习题,全是自己写的,绝对正确!
适用于西安电子科大的教材
P35T
(1)完数
#include
#include
intmain()
{
inti,j;
for(i=2;i<=999;i++)
{
inttemp=0;
for(j=1;j
{ if(i%j==0)
temp+=j;
}
if(i==temp)
cout<
}
}
P35T
(2)九九乘法表
#include
#include
intmain()
{
inti,j,k,m;
cout<<"*"<<"\t";
for(m=1;m<=9;m++)
{cout< cout<<"\n"; for(m=0;m<=24;m++) {cout<<"___";} cout<<"\n"; for(i=1;i<=9;i++)//控制行 { cout< for(j=1;j<=9;j++)//控制列 {k=j*i; cout< cout<<"\n"; } P36T(3)判断成绩 #include #include #include boolIsNum(char*s); voidmain() { intN; chars[100]; do//判断是否为数字,是,IsNum(S)返回true { cout<<"Pleaseinputascore! "< cin>>s; }while(! IsNum(s)); N=atoi(s);//把字符串转为整型数 if(N>=100) cout<<'A'< else { switch(N/10) { case6: cout<<'D'< case7: cout<<'C'< case8: cout<<'B'< case9: cout<<'A'< default: cout<<'E'< } } } boolIsNum(char*s) { for(inti=0;i { if(s[i] returnfalse; } returntrue; } P64T (1) 递归求10以内加法 #include intadd(intk) { intnum; if(k==0) num=0; else num=k+add(k-1); returnnum; } voidmain() { inti; i=10; i=add(i); cout< } P64T (2)递归求Fibonacci级数 #include intjs(intn) { if(n==1||n==2) return1; else returnjs(n-1)+js(n-2); } intmain() { intn; cout<<"Pleaseinputthenum: "; cin>>n; int*a=newint[n]; for(inti=1;i<=n;i++ { a[i-1]=js(i); cout< } return0; } P64T(3)绝对值函数模板 #include template doubleabsolute(Xj) { if(j>=0) returnj; else return-1*j; } voidmain() { doublei; cout<<"Pleaseinputafigure! "; cin>>i; i=absolute(i); cout< } P64T(4)非类实现 Rectangle的面积和周长 #include #include voidcomputeTriangle(float&a,float&p,floatx,floaty,floatz) { floatm; a=x+y+z; m=0.5*(x+y+z); p=sqrt(m*(m-x)*(m-y)*(m-z));//海伦公式 } voidmain() { floati,j,k,s,c; c=0,s=0; cout<<"Pleaseenterthelengthofthethreesidesofthetriangle"< cin>>i>>j>>k; computeTriangle(c,s,i,j,k); cout< } P64T(7)小于1000平方和 #include voidmain() { inti,d; d=0; for(i=1;d<1000;i++) d+=(i*i); cout< } P120T (1)类实现rectangle #include classrectangle { private: floatx1,y1,x2,y2; public: rectangle(floatxx1,floatyy1,floatxx2,floatyy2); floatarea(); }; rectangle: : rectangle(floatxx1,floatyy1,floatxx2,floatyy2) {x1=xx1;y1=yy1;x2=xx2;y2=yy2;} floatrectangle: : area() { return(x1-x2)*(y1-y2); } voidmain() { floatx1,y1,x2,y2; floatresult; cout<<"Pleaseenterthecoordinatevaluesoftherectangleontheupperleftcorner! (forexample: 00)"< cin>>x1>>y1; cout<<"Pleaseenterthecoordinatevaluesoftherectangleonthelowerrightcorner! (forexample: 11)"< cin>>x2>>y2; rectanglePo(x1,y1,x2,y2); result=Po.area(); cout< } P120T (2)complex类的构造和拷贝构造函数 #include classcomplex { private: floatreal; floatimaginary; public: complex(floata=0,floatb=0) {real=a;imaginary=b;} complex(complex&temp); floatPrintReal() {returnreal;} floatPrintimaginary() {returnimaginary;} }; complex: : complex(complex&temp) { real=temp.real; imaginary=temp.imaginary; cout<<"拷贝构造函数被调用! "; } voidf(complextemp) { cout< } complexr(floata,floatb) { complexq(a,b); returnq; } voidmain() { floata,b; cout<<"Pleaseentertherealpartofthecomplex! "< cin>>a; cout<<"Pleaseentertheimaginarypartofthecomplex! "< cin>>b; complexq(a,b); complexm(q); cout< f(m); m=r(a,b); cout< } P120 类组合求复数加法 #include classcomplex { private: floatreal; floatimaginary; public: complex(floata,floatb) { real=a; imaginary=b; } floatget_real() {returnreal;} floatget_imaginary() {returnimaginary;} }; classplus { private: floatplus_real,plus_imaginary; complexa,b; public: plus(complexxa,complexxb): a(xa),b(xb) { plus_real=a.get_real()+b.get_real(); plus_imaginary=a.get_imaginary()+b.get_imaginary(); } floatget_r() { returnplus_real; } floatget_i() { returnplus_imaginary; } }; voidmain() { floatm,n,o,p; cout<<"Pleaseentertherealvalueofthecomplex"< cin>>m; cout<<"Pleaseentertheimaginaryvalueofthecomplex"< cin>>n; complexsa(m,n); cout<<"Pleaseentertherealvalueofthecomplex"< cin>>o; cout<<"Pleaseentertheimaginaryvalueofthecomplex"< cin>>p; complexsb(o,p); plussc(sa,sb); cout<<"Theresultis"< cout< } P120 类模板求最大最小值 #include template classcompare { private: Ta,b; public: compare(Tx,Ty) {a=x;b=y;} voidprint() { if(a>b) cout<<"Themaximumis "< elseif(a==b)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 学期 c+ 习题 电子科