大学c  习题答案习题0304答案_第1页
大学c  习题答案习题0304答案_第2页
大学c  习题答案习题0304答案_第3页
大学c  习题答案习题0304答案_第4页
大学c  习题答案习题0304答案_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

习题313 75 8 或者 3 7 5 8 (注: 为空格、Tab键或者回车键)2 #include iostream.hvoid main()int a, b, c, d;cin.ignore(1);cin a hex b ;cin dec c ;cin.ignore(3);cin d; couta=a b=b c=c d=dendl;3 #include iostream.hvoid main()int count=0;char c;cin.get(c);if (c= ) count+; / 为空格cin.get(c);if (c= ) count+;cin.get(c);if (c= ) count+;cin.get(c);if (c= ) count+;cin.get(c);if (c= ) count+; cout空格共有:count个endl;4x is 100 y=3.140000e+000 s5 #include iostream.hvoid main()int i=8, j=12;double x=3.14, y=90;couti=octitj=hexjendl;cout.setf(ios:scientific, ios:floatfield);coutx=xt;cout.unsetf(ios:scientific); couty=yendl;6. #include iostream.h#include math.hvoid main()double a,b,c,s,area;cout请输入三角形的三边长:abc;s = (a+b+c)/2;area = sqrt(s*(s-a)*(s-b)*(s-c);cout三角形的面积为:areaendl;习题41 #include iostream.hvoid main()double a,b,c,max;cout请输入三个数:abc;if(ab) max=a;else max=b;if(maxc) max=c;cout最大数为:max=1000n+1nsum+nsum结束YN23 #include iostream.hvoid main()int i,count=0;for(i=1;i=1000;i+) if(i%9=0 & i%11=0)coutit;count+;if(count%5=0) coutendl;4If-else语句答案: #include iostream.hvoid main()int score;coutscore;if(score100 | score0)cout输入的成绩有错!endl;elsecout=90) coutA=80) coutB=70) coutC=60) coutDendl;else coutEendl;Switch语句答案:#include iostream.hvoid main()int score;coutscore;if(score100 | score0)cout输入的成绩有错!endl;elsecoutn五分制成绩为:;switch(score/10) case 10:case 9: coutAendl; break;case 8: coutBendl; break;case 7: coutCendl; break;case 6: coutDendl; break;default: coutEendl;5If-else语句答案:#include iostream.hvoid main()int base1,base2,base3,base4,base5,base6,base7,base8;float money,tax;base1=0.05*500;base2=base1+0.1*1500;base3=base2+0.15*3000;base4=base3+0.2*15000;base5=base4+0.25*20000;base6=base5+0.3*20000;base7=base6+0.35*20000;base8=base7+0.4*20000;coutmoney;if(money0)cout输入的金额不能是负数!endl;else money=money-800;if(money=0) tax=0;else if(money=500) tax=money*0.05;else if(money=2000) tax=base1+(money-500)*0.1; else if(money=5000) tax=base2+(money-2000)*0.15; else if(money=20000) tax=base3+(money-5000)*0.2; else if(money=40000) tax=base4+(money-20000)*0.25;else if(money=60000) tax=base5+(money-40000)*0.3; else if(money=80000) tax=base6+(money-60000)*0.35;else if(money=100000) tax=base7+(money-80000)*0.4; else tax=base8+(money-100000)*0.45;coutn应缴税款为:taxendl;6 #include iostream.hvoid main()int i,a,b,c,d;for(i=1000;i10000;i+)a=i/1000; /千位b=i%1000/100; /百位c=i%100/10; /十位d=i%10; /个位if(a*a*a*a+b*b*b*b+c*c*c*c+d*d*d*d=i)coutit;coutendl;7 #include iostream.h#include math.hvoid main()int i,j,m;for(i=2;i1000;i+)m=sqrt(i);for(j=2;j=m+1)coutit;cout0;j-,i-=2) for(m=0;mbeginCol+4-j;m+)cout ;for(m=0;mi;m+)cout*;coutendl;for(i=3,j=2;j=4;j+,i+=2) for(m=0;mbeginCol+4-j;m+)cout ;for(m=0;mi;m+)cout*;coutendl;coutendl; 9 #include iostream.hvoid main( ) int i, x, y, z;for(x=1;x=9;x+) for(y=0;y=9;y+) for(z=0;z=9;z+) if(x!=y & y!=z & z!=x & (x*100+y*10+z)%11=0) cout x*100+y*10+z t;coutendl;10#include iostream.hvoid main( ) int i; float m,n,sum=0; for(i=0,m=1,n=2; i30; i+) sum += n/m; n=n+m; m=n-m; coutsumendl;习题151答:在C+语言中不可以取消函数重载这一技术。首先,函数重载技术相对于函数模板技术来说比较简单,程序运行的效率会较高。其次,如15.2.2小节所述,为了实现复杂应用,函数模板也需要进行重载。取消了函数重载技术,就无法实现函数模板的重载了。这样,函数模板的应用就要受到极大的限制。2 #include iostream.h#include math.htemplate /定义函数模板的模板说明部分Type power(Type var1, Type var2) /定义函数模板的函数说明部分 return pow(var1,var2);void main( ) int m1,m2; double n1,n2; cout请输入需要计算的底数和幂数:n1n2;m1=n1; m2=n2; coutn1的n2次方是:power(n1,n2)endl; coutm1的m2次方是:power(m1,m2)endl;3 #include iostream.htemplate /定义函数模板的模板说明部分Type sum(Type* p, int count) /定义函数模板的函数说明部分 int i;Type s=0;for(i=0;icount;i+) s=s+pi;return(s);void main( ) int a=1,2,3,4,5; double b=1.3,2.5,9.2,4.3,2.1; cout整数数组a的元素之和为:sum(a,5)endl; cout浮点数数组b的元素之和为:sum(b,5)endl;4 #include iostream.htemplate class CValueprivate:Type data5; /数据成员public:CValue(); /构造函数Type Max(void); /成员函数Type Min(void);template CValue : CValue()cout请输入数组的5个元素的值:endl;for(int i=0;idatai;template Type CValue : Max(void)Type m=data0;for(int i=1;i5;i+)if(mdatai) m=datai; return m;template Type CValue : Min(void)Type m=data0;for(int i=0;idatai) m=datai;return m;void main( ) cout整数对象a,;CValue a; cout浮点数对象b,; CValue b; cout整数元素对象a的元素最大值为:a.Max()endl; cout整数元素对象a的元素最小值为:a.Min()endl; cout浮点数元素对象b的元素最大值为:b.Max()endl; cout浮点数元素对象b的元素最小值为:b.Min()endl;5 #include iostream.htemplate class CValueprivate:Type data5; /数据成员public:CValue(); /构造函数friend Type Max(CValue v); /友元函数friend Type Min(CValue v);template CValue : CValue()cout请输入数组的5个元素的值:endl;for(int i=0;idatai;template Type Max(CValue v)Type m=v.data0;for(int i=1;i5;i+)if(mv.datai) m=v.datai; return m;template Type Min(CValue v)Type m=v.data0;for(int i=0;iv.datai) m=v.datai;return m;void main( ) cout整数对象a,;CValue a; cout浮点数对象b,; CValue b; cout整数元素对象a的元素最大值为:Max(a)endl; cout整数元素对象a的元素最小值为:Min(a)endl; cout浮点数元素对象b的元素最大值为:Max(b)endl; cout浮点数元素对象b的元素最小值为:Min(b)endl;6 #include iostream.htemplate class CArrayprivate:Type* data; /数据成员int size;public:CArray(const int n); /构造函数CArray(); /析构函数 void Input(); /成员函数void Output();Type Max(); Type Min();Type Sum();Type Average();template inline CArray : CArray(const int n)size = n;data = new Typesize;for(int i=0; isize; i+)datai = 0;template inline CArray : CArray()delete data;template void CArray : Input()cout请输入数组的size个元素的值:endl;for(int i=0;idatai;template void CArray : Output()cout数组的元素值为:;for(int i=0;isize;i+)coutdatai;coutendl;template Type CArray : Max()Type m=data0;for(int i=1;isize;i+)if(mdatai) m=datai; return m;template Type CArray : Min()Type m=data0;for(int i=0;idatai) m=datai;return m;template Type CArray : Sum()Type sum=0;for(int i=0;isize;i+)sum += datai;return sum;template Type CArray : Average()Type avg=0;for(int i=0;isize;i+)avg += datai; avg /= size;return avg;void main( ) CArray a(5); CArray b(5);cout整数对象a,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论