C++程序设计习题程序合成.doc

上传人:飞****2 文档编号:60929336 上传时间:2022-11-19 格式:DOC 页数:22 大小:63.50KB
返回 下载 相关 举报
C++程序设计习题程序合成.doc_第1页
第1页 / 共22页
C++程序设计习题程序合成.doc_第2页
第2页 / 共22页
点击查看更多>>
资源描述

《C++程序设计习题程序合成.doc》由会员分享,可在线阅读,更多相关《C++程序设计习题程序合成.doc(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、C+程序设计-期末考试所考大题第1题 P863-15#include using namespace std;int main ()int p,r,n,m,temp; coutnm; if (nm) temp=n; n=m; m=temp; /把大数放在n中, 小数放在m中 p=n*m; /先将n和m的乘积保存在p中, 以便求最小公倍数时用 while (m!=0) /求n和m的最大公约数 r=n%m; n=m; m=r; coutHCF=nendl; coutLCD=p/nendl; / p是原来两个整数的乘积 return 0; 3-17#include using namespace s

2、td;int main ()int a,n,i=1,sn=0,tn=0; coutan; while (i=n) tn=tn+a; /赋值后的tn为i个a组成数的值 sn=sn+tn; /赋值后的sn为多项式前i项之和 a=a*10; +i; couta+aa+aaa+.=snendl; return 0; 3-18#include using namespace std;int main ()float s=0,t=1; int n; for (n=1;n=20;n+) t=t*n; / 求n! s=s+t; / 将各项累加 cout1!+2!+.+20!=sendl; return 0;

3、3-19#include using namespace std;int main ()int i,j,k,n; coutnarcissus numbers are:endl; for (n=100;n1000;n+) i=n/100; j=n/10-i*10; k=n%10; if (n = i*i*i + j*j*j + k*k*k) coutn ; coutendl;return 0; 3-20-1#include using namespace std; int main() int m,s,i; for (m=2;m1000;m+) s=0; for (i=1;im;i+) if (

4、m%i)=0) s=s+i; if(s=m) coutm is a完数endl; coutits factors are:; for (i=1;im;i+) if (m%i=0) couti ; coutendl; return 0; 3-20-2#include using namespace std;int main() int k11; int i,a,n,s; for (a=2;a=1000;a+) n=0; s=a; for (i=1;ia;i+) if (a%i)=0) n+; s=s-i; kn=i; / 将找到的因子赋给k1k10 if (s=0) couta is a 完数e

5、ndl; coutits factors are:; for (i=1;in;i+) coutki ; coutknendl; return 0; 3-21#include using namespace std;int main()int i,t,n=20; double a=2,b=1,s=0; for (i=1;i=n;i+) s=s+a/b; t=a; a=a+b; / 将前一项分子与分母之和作为下一项的分子 b=t; / 将前一项的分子作为下一项的分母 coutsum=sendl; return 0; 3-22#include using namespace std;int main

6、() int day,x1,x2; day=9; x2=1; while(day0) x1=(x2+1)*2; / 第1天的桃子数是第2天桃子数加1后的2倍 x2=x1; day-; couttotal=x1endl; return 0; 3-24#include using namespace std;int main() int i,k; for (i=0;i=3;i+) / 输出上面4行*号 for (k=0;k=2*i;k+) cout*; / 输出*号 coutendl; /输出完一行*号后换行 for (i=0;i=2;i+) / 输出下面3行*号 for (k=0;k=4-2*i

7、;k+) cout*; / 输出*号 coutendl; / 输出完一行*号后换行 return 0; 第2题 P2277-1#include using namespace std;struct int year; int month; int day; date; int main()int i,days; int day_tab13=0,31,28,31,30,31,30,31,31,30,31,30,31; coutdate.yeardate.monthdate.day; days=0; for (i=1;i=3) days+=1; coutdate.month/date.day is

8、 the days th day in date.year.endl; return 0;7-2#include using namespace std;struct y_m_d int year; int month; int day; ; int main() y_m_d date; int days(int,int,int); /* 对days函数的声明 */ int day_sum; coutdate.yeardate.monthdate.day; day_sum=days(date.year,date.month,date.day); coutdate.month/date.day

9、is the day_sum th day in date.yearendl; return 0; int days(int year,int month,int day) /* 定义days函数 */int day_sum,i; int day_tab13=0,31,28,31,30,31,30,31,31,30,31,30,31; day_sum=0; for (i=1;i=3) day_sum+=1; return(day_sum);7-3#include #include using namespace std;const int n=5;struct student char num

10、6; char name8; int score4;stun;int main()void print(student stu6); int i,j; for (i=0;in;i+) coutinput scores of student i+1:endl; coutstui.num; coutstui.name; for (j=0;j3;j+) coutscore j+1stui.scorej; coutendl; print(stu);return 0;void print(student stu6) int i,j; cout NO. name score1 score2 score3e

11、ndl; for (i=0;in;i+) coutstui.num setw(10)stui.name ; for (j=0;j3;j+) coutsetw(3)stui.scorej ; coutendl; 7-4#include #include using namespace std;const int n=5;struct student char num6; char name8; int score4;stun;int main()void input(student stu); void print(student stu); input(stu); print(stu); re

12、turn 0;void input(student stu)int i,j; for (i=0;in;i+) coutinput scores of student i+1:endl; coutstui.num; coutstui.name; for (j=0;j3;j+) coutscore j+1stui.scorej; void print(student stu) int i,j; cout NO. name score1 score2 score3endl; for (i=0;in;i+) coutstui.num setw(10)stui.name ; for (j=0;j3;j+

13、) coutsetw(3)stui.scorej ; coutendl; 7-5#include #include using namespace std;const int n=10;struct student char num6; char name8; int score4; float avr; stun;int main() int i,j,max,maxi,sum; float average; for (i=0;in;i+) coutinput scores of student i+1endl; coutstui.num; coutstui.name; for (j=0;j3

14、;j+) coutscore j+1stui.scorej; coutendl; average=0; max=0; maxi=0; for (i=0;in;i+) sum=0; for (j=0;jmax) max=sum; maxi=i; average/=n; cout NO. name score1 score2 score3 averageendl; for (i=0;in;i+) coutsetw(8)stui.num setw(10)stui.name ; for (j=0;j3;j+) coutsetw(3)stui.scorej ; coutstui.avrendl; cou

15、taverage=averageendl; coutThe highest score is :stumaxi.name, score total:maxendl; return 0; 第3题8-6-1 P262 #include using namespace std;class Boxpublic: void get_value(); float volume(); void display(); public: float lengh; float width; float height;void Box:get_value() coutlengh; cinwidth; cinheigh

16、t;float Box:volume() return(lengh*width*height);void Box:display() coutvolume()endl;int main()Box box1,box2,box3; box1.get_value(); coutvolmue of bax1 is ; box1.display(); box2.get_value(); coutvolmue of bax2 is ; box2.display(); box3.get_value(); coutvolmue of bax3 is ; box3.display(); return 0;8-6

17、-2#include using namespace std;class Boxpublic: void get_value(); void volume(); void display();public: float lengh; float width; float height; float vol; ;void Box:get_value() coutlengh; cinwidth; cinheight;void Box:volume() vol=lengh*width*height;void Box:display() coutvolendl;int main()Box box1,b

18、ox2,box3; box1.get_value(); box1.volume(); coutvolmue of bax1 is; box1.display(); box2.get_value(); box2.volume(); coutvolmue of bax2 is; box2.display(); box3.get_value(); box3.volume(); coutvolmue of bax3 is; box3.display(); return 0;例题8.4 P255 例题9.2 P266 P3119-4-1#include using namespace std;class

19、 Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() coutnum scoredisplay(); p-change(101,80.5); p-display(); return 0;9-4-2#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s)

20、 num=n;score=s; void display() constcoutnum scoredisplay(); stud.change(101,80.5); p-display(); return 0;9-5#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() coutnum scoredisplay(); p-change(101,80.5); p

21、-display(); return 0;9-9#include using namespace std;class Productpublic: Product(int n,int q,float p):num(n),quantity(q),price(p); void total(); static float average(); static void display();private: int num; int quantity; float price; static float discount; static float sum; static int n; ;void Pr

22、oduct:total() float rate=1.0; if(quantity10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; void Product:display() coutsumendl; coutaverage()endl; float Product:average() return(sum/n);float Product:discount=0.05;float Product:sum=0;int Product:n=0;int main()Product Prod3=Pr

23、oduct(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5); for(int i=0;i3;i+) Prodi.total(); Product:display(); return 0; 9-10#include using namespace std;class Date;class Time public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; ; Ti

24、me:Time(int h,int m,int s) hour=h; minute=m; sec=s; class Datepublic: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; ;Date:Date(int m,int d,int y) month=m; day=d; year=y; void display(const Date &d,const Time &t) coutd.month/d.day/d.yearendl

25、; coutt.hour:t.minute:t.secendl; int main() Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0;第4题例题10.1 P314例题10.8 P333P34310-1#include using namespace std;class Complexpublic: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void

26、display(); private: double real; double imag; ;double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()cout(real,imagi)endl;Complex operator + (Complex &c1,Complex &c2)return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()Complex c1(

27、3,4),c2(5,-10),c3; c3=c1+c2; coutc3=; c3.display(); return 0;10-2#include using namespace std;class Complexpublic: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Comple

28、x &c2); void display(); private: double real; double imag; Complex Complex:operator+(Complex &c2)Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; Complex Complex:operator-(Complex &c2)Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;Complex Complex:operator*(Complex &c2)Co

29、mplex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c;Complex Complex:operator/(Complex &c2)Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;void Complex:dis

30、play()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1+c2=; c3.display(); c3=c1-c2; coutc1-c2=; c3.display(); c3=c1*c2; coutc1*c2=;c3.display(); c3=c1/c2; coutc1/c2=; c3.display(); return 0;10-3#include /用VC+时改为 #include using namespace std; /用VC+时为取消此行class Complexpubli

31、c: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); void display(); private: double real; double imag;Complex Complex:operator+(Complex &c)return Complex(real+c.real,imag+c.imag); Com

32、plex Complex:operator+(int &i)return Complex(real+i,imag);void Complex:display()cout(real,imagi)endl;Complex operator+(int &i,Complex &c)return Complex(i+c.real,c.imag);int main()Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; coutc1+c2=; c3.display(); c3=i+c1; couti+c1=; c3.display(); c3=c1+i; coutc1+i=; c3.display(); return 0;10-4#include using namespace std;class Matrix /定义Matrix类public: Matrix(); /默认构造函数 friend Matrix operator+(Matrix &,Matrix &); /重载运算符“+” void input();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁