1、这学期c+习题电子科大版这学期c+习题,全是自己写的,绝对正确!适用于西安电子科大的教材P35 T(1)完数#include#includeint main() int i,j; for(i=2;i=999;i+) inttemp=0; for(j=1;ji;j+) if(i%j=0) temp+=j; if(i=temp) coutiendl; P35 T(2)九九乘法表#include#includeint main() int i,j,k,m; cout*t; for(m=1;m=9;m+) coutmt; coutn; for(m=0;m=24;m+) cout_; coutn; fo
2、r(i=1;i=9;i+)/控制行 coutit; for(j=1;j=9;j+)/控制列 k=j*i;coutkt; coutn; P36 T(3)判断成绩#include#include#includebool IsNum(char *s);void main() int N; char s100; do/判断是否为数字,是,IsNum(S)返回true coutPleaseinput a score!s; while(!IsNum(s); N=atoi(s);/把字符串转为整型数 if(N=100) coutAendl; else switch(N/10) case6:coutDendl
3、;break; case7:coutCendl;break; case8:coutBendl;break; case9:coutAendl;break; default:coutEendl; bool IsNum(char *s) for(inti=0;istrlen(s);i+) if(siint(9) returnfalse; return true;P64 T(1)递归求10以内加法#includeint add(int k) int num; if(k=0) num=0; else num=k+add(k-1); return num;void main() int i; i=10;
4、i=add(i); couti;P64 T(2)递归求Fibonacci级数#includeint js(int n) if(n=1 |n=2) return 1; else returnjs(n-1)+js(n-2);int main() int n; coutn; int *a= newintn; for(inti=1;i=n;i+ ai-1=js(i); coutai-1 ; return 0;P64 T(3)绝对值函数模板#includetemplate/利用函数模板求绝对值double absolute(X j) if (j=0) return j; else return -1*j
5、;void main() double i; couti; i=absolute(i); couti;P64 T(4)非类实现Rectangle的面积和周长#include#includevoid computeTriangle(float &a,float &p,floatx,float y,float z) float m; a=x+y+z; m =0.5*(x+y+z); p =sqrt(m*(m-x)*(m-y)*(m-z);/海伦公式void main() floati,j,k,s,c; c=0,s=0; coutPleaseenter the length of the three
6、 sides of the triangle ijk; computeTriangle(c,s,i,j,k); coutcsendl;P64 T(7)小于1000平方和#includevoid main() int i,d; d=0; for(i=1;d1000;i+) d+=(i*i); couti-2;P120 T(1)类实现rectangle#includeclass rectangle private: floatx1,y1,x2,y2; public: rectangle(floatxx1 ,float yy1,float xx2,float yy2); floatarea();re
7、ctangle:rectangle(float xx1 ,float yy1,float xx2,floatyy2)x1=xx1;y1=yy1;x2=xx2;y2=yy2;float rectangle:area()return (x1-x2)*(y1-y2);void main() floatx1,y1,x2,y2; float result; coutPleaseenter the coordinate values of the rectangle on the upper left corner!(forexample:0 0) x1y1; coutPleaseenter the co
8、ordinate values of the rectangle on the lower right corner!(forexample:1 1) x2y2; rectanglePo(x1,y1,x2,y2); result =Po.area(); coutresult;P120 T(2) complex类的构造和拷贝构造函数#includeclass complex private: floatreal; floatimaginary; public: complex(floata=0,float b=0) real=a;imaginary=b; complex(complex&temp
9、); floatPrintReal() returnreal; floatPrintimaginary() returnimaginary;complex:complex(complex &temp) real=temp.real; imaginary=temp.imaginary; cout拷贝构造函数被调用!;void f(complex temp) couttemp.PrintReal()temp.Printimaginary()endl;complex r(float a, float b) complexq(a,b); return q;void main() float a,b;
10、coutPleaseenter the real part of the complex!a; coutPleaseenter the imaginary part of the complex!b; complexq(a,b); complex m(q); coutm.PrintReal()m.Printimaginary()endl; f(m); m=r(a,b); coutm.PrintReal()m.Printimaginary()endl;P120类组合求复数加法#includeclass complex private: floatreal; floatimaginary; pub
11、lic: complex(floata,float b) real=a; imaginary=b; floatget_real() returnreal; floatget_imaginary() returnimaginary;class plus private: floatplus_real,plus_imaginary; complexa,b; public: plus(complexxa,complex xb ):a(xa),b(xb) plus_real=a.get_real()+b.get_real(); plus_imaginary=a.get_imaginary()+b.ge
12、t_imaginary(); floatget_r() returnplus_real; floatget_i() returnplus_imaginary; ;void main() float m,n,o,p; coutPleaseenter the real value of the complexm; coutPleaseenter the imaginary value of the complexn; complexsa(m,n); coutPleaseenter the real value of the complexo; coutPleaseenter the imagina
13、ry value of the complexp; complexsb(o,p); plussc(sa,sb); coutTheresult is endl; coutsc.get_r()+sc.get_i()eendl;P120类模板求最大最小值#includetemplateclass compare private: Ta,b; public: compare(Tx,T y) a=x;b=y; voidprint() if(ab) coutThemaximum is aand the minimum is bendl; elseif(a=b) couta=bendl; else cout
14、Themaximum is band the minimum is aendl; ;void main() int x,y; float m,n; coutPleaseenter two Integers !xy; compare p(x, y); p.print(); coutPleaseenter two decimals !mn; compare q( m, n); q.print();P154 T(1)静态数据成员cat#includeclass Cat private: staticint HowManyCats; public: Cat()HowManyCats+; Cat(Cat
15、 &c)HowManyCats+; staticvoid GetHowMany() coutHowManyCatsendl;int Cat:HowManyCats=0;void main() Cat a; a.GetHowMany(); Cat b(a); b.GetHowMany(); Cat c; c.GetHowMany();P154 T(2)友元实现学生成绩#include#includeclass student private: charname20; floatEnglish_score; floatComputerScience_score; floatsum_score; p
16、ublic: student(char c20,float E_score,floatCS_score) strcpy(name,c); English_score= E_score; ComputerScience_score= CS_score; floatsum() sum_score=English_score+ComputerScience_score; return sum_score; friendvoid DescendingOrder(student p1,student p2);void DescendingOrder(student p1,student p2) if(p
17、1.sum()p2.sum() coutp1.namep1.sum()endlp2.name p2.sum()endl; else coutp2.namep2.sum()endlp1.namep1.sum()endl;void main() char c120,c220; floatE_score1, CS_score1, E_score2, CS_score2; coutc1; coutPleaseenter the students English score:E_score1; coutPleaseenter the students Computer Sciencescore!CS_s
18、core1; studentm(c1,E_score1,CS_score1); coutPleaseenter another students name!c2; coutPleaseenter the students English score!E_score2; coutPleaseenter the students Computer Sciencescore!CS_score2; studentn(c2,E_score2, CS_score2); DescendingOrder(m,n);P193 T(6)指针数组比较字符串#include#includevoid sort_country(char *c,int n) int i,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1