《C++面向对象程序设计(谭浩强_编著)习题程序.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计(谭浩强_编著)习题程序.docx(123页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、XT1-5.CPP(第一章的第五题,下面类似)#include using namespace std;int main()int sum;for(int i=0;i6;i=+3)sum=i;for(int j=i;j6;j+)sum+=j;)coutsum=sum;return 0;)XT1-6.CPP#include using namespace std;int main()(int a,b,c;a=10;b=23;c=a+b;couta+b=;coutc;coutendl;return 0;XT1-7.CPP#include using namespace std; int main(
2、)(int a,b,c;int f(int x,int y,int z); cinabc;c=f(a,b,c);coutcendl;return 0;int f(int x,int y,int z)(int m;if (xy) m=x;else m=y;if (zm) m=z;return (m);XT1-8.CPP#include using namespace std;int main()(int a,b,c;cinab;c=a+b;cout a+b=a+ben dl;return 0;)XT1-9.CPP#include using namespace std;int main()int
3、 add(int x,int y);int a,b,c;cinab;c=add(a,b);cou t a+b= cendl;return 0;)int add(int x,int y)int z;z=x+y;return(z);)XT1-10.CPP#include using namespace std;int main()void sort(int x,int y,int z);int x,y,z;cinxyz;sort(x,y,z);return 0;void sort(int x, int y, int z)int temp;if (xy) temp=x;x=y;y=temp; 内 3
4、 个语句的作用是 将x和y的值互换)if (zx) cou tz, x, yen dl;else if (zy) coutx,z,yendl;else coutx,y,zendl;)XT1-11.CPP#include using namespace std;int main()int max(int a,int b,int c=0);int a,b,c;cinabc;coutmax(a,b,c)=max(a,b,c)endl;coutmax(a,b)=max(a,b)endl;return 0;)int max(int a,int b,int c)if(ba) a=b;if(ca) a=c;
5、return a;XT1-12.CPP#include using namespace std;int main()(void change(int &,int &);int a,b;cinab;if(ab) change(a,b);coutmax=a min=bendl;return 0;)void change(int &rl,int &r2)(int temp;temp=rl;rl=r2;r2=temp;coutrl ddd r2endl;XT1-13.CPP #include using namespace std;int main()void sort(int &,int &,int
6、 &);int a,b,c,al,bl,cl;coutPlease enter 3 integers:;cinabc;al=a;bl=b;cl=c;sort(al,bl,cl);couta b c in sorted order iscoutal bl clendl;return 0;)void sort(int &i,int &j,int &k) void change(int &,int &);if (ij) change(i,j);if (ik) change(i,k);if (jk) change。,k);void change(int &x,int &y) int temp;temp
7、=x;x=y;y=temp;)XT1-14.CPP#include #include using namespace std;int main() string s 1=week,s2=end;couts 1 =s lendl;couts2=s2endl;sl=sl+s2;coutThe new string is:slendl;return 0;)XT1-15.CPP #include #include using namespace std;int main() string str;int i,n;char temp;coutplease input a string:;cinstr;n
8、=str.size();for(i=0;in/2;i+)temp=stri;stri=strn-i-l;strn-i-l=temp; coutstrendl;return 0;)XT1-16.CPP#include #include using namespace std;int main() int i;string str5=BASIC,C,FORTRAN,C+,PASCAL;void sort(string );sort(str);coutthe sorted strings :endl;fbr(i=0;i5;i+)coutstricoutendl;return 0;void sort(
9、string s)int i,j;string t;for (j=0;j5;j+)for(i=0;isi+l)t=si ;si=si+1 ;si+1 =t;)XT1-17-1.CPP#include #include using namespace std;int main()double c5= 10100,-123567, 11,-165654, 3456;int a5= 1,9,0,23,-45;float b5=2.4f, 7.6f, 5.5f, 6.6f,-2.3f ;void sort(int );void sort(float );void sort(double );sort(
10、a);sort(c);sort(b);coutsizeof(long int) sizeof( int); return 0;)void sort(int a)int i,j,t;for (j=0;j5;j+)for(i=0;iai+l)t=ai;ai=ai+ l;ai+l=t; coutthe sorted numbers :endl; for(i=0;i5;i+)coutai;coutendlendl;void sort(double a)int i,j;double t;for (j=O;j5;j+)for(i=0;iai+l)t=ai ;ai=ai+1 ;ai+1 =t;coutthe
11、 sorted numbers :endl;for(i=0;i5;i+)coutai coutendlendl;)void sort(float a)int i,j;float t;for (j=O;j5;j+)for(i=0;iai+l)t=ai;ai=ai+ l;ai+l=t;coutthe sorted numbers :endl;for(i=0;i5;i+)coutai coutendlendl;XT1-17-2.CPP#include #include using namespace std;int main() int a5= 1,9,0,23,-45;float b5=2.4,
12、7.6, 5.5, 6.6, -2.3 ;long int c5= 10100,-123567, 1198783,-165654, 3456);void sort(int );void sort(float );void sort(long );sort(a);sort(b);sort(c);return 0;void sort(int a)int i,j,t;for (j=O;j5;j+)for(i=0;iai+l)t=ai ;ai=ai+1 ;ai+1 =t;coutthe sorted numbers :endl;for(i=0;i5;i+)coutai coutendlendl;)vo
13、id sort(float a)int i,j;float t;for (j=O;j5;j+)for(i=0;iai+l)t=ai ;ai=ai+1 ;ai+1 =t;coutthe sorted numbers :endl;for(i=0;i5;i+)coutaicou ten dlendl;void sort(long a)int i,j,min;long t;for(i=0;i5;i+)min=i;for (j=i+l;jaj) min=j;t=ai; ai=amin;amin=t; coutthe sorted numbers :endl; for(i=0;i5;i+)coutai c
14、outendlendl;XT1-18.CPP#include #include using namespace std;template void sort(T a)int i,j,min;Tt;for(i=0;i5;i+)min=i;for (j=i+l;jaj) min=j;t=ai; ai=amin; amin=t;)coutthe sorted numbers :endl;for(i=0;i5;i+)coutaicoutendlendl;)int main() int a5= 1,9,20,23,-45;float b5=2.4f, 7.6f, 5.5f, 6.6f, -2.3f ;d
15、ouble c5= 101.00,-123.567, 1198.783,-165.654, 34.56;sort(a);sort(b);sort(c);return 0;XT2-1.CPP#include using namespace std; class Timepublic:int hour;int minute;int sec;;Time t;void set_time(void)(cint.hour;cint.minute;成员改为公用的在main函数之前定义cint.sec;void show_time(void)在main函数之前定义coutt.hour:t.minute:t.s
16、ecendl;int main()set_time();show_time();return 0;XT2-2.CPP#include using namespace std;class Timepublic:void set_time(void)cinhour;cinminute;cinsec;)void show_time(void)couthour: minute: secendl;private: int hour;int minute;int sec;I;Time t;int main()t.set_time();t.show_time();return 0;IXT2-3.CPP#in
17、clude using namespace std;class Timepublic:void set_time(void);void show_time(void);private:int hour;int minute;int sec;);void Time:set_time(void)cinhour;cinminute;cinsec;void Time:show_time(void)couthour: minute: secendl;Time t;int main() t.set_time();t.show_time();return 0;)XT2-4-1.CPP/xt2-4-1 .cp
18、p(main.cpp) #include using namespace std;#include xt2-4.hint main()Student stud;stud.set_value();stud.displayO;return 0;XT2-4-2.CPPxt2-4-2.cpp(即 student.cpp)#include xt2-4.h在此文件中进行函数的定义#include using namespace std;不要漏写此行void Student:display() coutnum:numendl;coutname:nameendl;coutsex:sexendl;)void S
19、tudent: set_value() cinnum;cinname;cinsex;XT2-4.H/xt2-4.h(student.h) class Student public:void display();void set_value();private:int num;char name20;char sex ;);XT2-5-1.CPP/xt2-5-l.cpp(file 1 .cpp)#include include xt2-5.hint main()Array_max arrmax; arrmax.set_value(); arrmax.max_value();arrmax.show
20、_value();return 0;)XT2-5-2.CPP/xt2-5-2.cpp(arraymax.cpp)#include using namespace std;#include xt2-5.hvoid Array_max: set_value() int i;for (i=0;i10;i+) cinarrayi;Ivoid Array_max: :max_value()int i;max=array0|;for (i=l;imax) max=arrayi;void Array_max:show_value()coutmax=maxendl;)XT2-5.H/xt2-5.h(array
21、max.h)class Array_maxpublic:void set_value();void max_value();void show_value();private:int array10J;int max;);XT2-6-1.CPP#include using namespace std;class Boxpublic:void get_value();float volume();float lengh;float width;float height;);void Box:get_value() coutplease input lengh, width,height:;cin
22、lengh;cinwidth;cinheight;)float Box:volume() return(lengh*width*height);void Box:display() coutvolume()endl;)int main()Box boxl,box2,box3;boxl.get_value();coutvolmue of boxl is ;boxl .displayO;box2.get_value();coutvolmue of box2 isbox2.display();box3.get_value();coutvolmue of box3 isbox3.display();r
23、eturn 0;)XT2-6-2.CPP#inelude using namespace std;class Boxpublic:void get_value();void volume();void displayO;public:float lengh;float width;float height;float vol;void Box:get_value() coutplease input lengh, width,height:;cinlengh;cinwidth;cinheight;void Box:volume() vol=lengh*width*height;void Box
24、:display() coutvolendl;int main()Box boxl,box2,box3;boxl.get_value();boxl.volume();coutvolmue of box 1 isboxl.displayO;box2. get_value();box2.volume();coutvolmue of box2 isbox2.display();box3.get_value();box3.volume();coutvolmue of box3 isbox3.display();return 0;)XT3-2.CPP#inelude using namespace st
25、d;class Datepublic:Date(int,int,int);Date(int,int);Date(int);Date();void displayO;private:int month;int day;int year;);Date二Date(int m,int d,int y):month(m),day(d),year(y) ( Date:Date(int m,int d):month(m),day(d) year=2005;Date:Date(int m):month(m)day=l;year=2005;IDate:Date()month=l;day=1;year=2005;
26、)void Date:display()coutmonth/day/yearendl;int main()Date dl(10,13,2005);Date d2( 12,30);Date d3(10);Date d4;dl .displayO;d2.display();d3.display();d4.display();return 0;XT3-3.CPP#include using namespace std;class Datepublic:Date(int=l,int= l,int=2005);void displayO;private:int month;int day;int yea
27、r;I;Date:Date(int m,int d,int y):month(m),day(d),year(y) ( void Date:display()coutmonth/day/yearendl;int main()(Date dl(10,13,2005);Date d2(12,30);Date d3(10);Date d4;dl .displayO;d2.display();d3.display();d4.display();return 0;XT3-4.CPP#include using namespace std;class Studentpublic:Student(int n,
28、float s):num(n),score(s) void displayO;private:int num;float score;);void Student:display()coutnum scoreendl;int main()Student stud5=Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(l 04,100.0),Student( 105,95.5);Student *p=stud;for(int i=0;idisplay();return 0;IXT3-5.CPP#include using n
29、amespace std;class Studentpublic:Student(int n,float s):num(n),score(s)int num;float score;);void main()Student stud5=Student(101,78.5),Student(102,85.5),Student(103,98.5),Student( 104,100.0),Student(l 05,95.5);void max(Student* );Student *p=&stud0;max(p);void max(Student *arr)float max_score=arr0.s
30、core;int k=0;for(int i=l;imax_score) max_score=arri.score;k=i; coutarrk.num max_scoreendl;)XT3-6.CPP#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO coutnum scoreendl;private:int num;int main()Student stud(1
31、01,78.5);stud.displayO;stud.change(l 01,80.5);stud.displayO;return 0;XT3-7-1.CPP#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO coutnum scoreendl;可改为:void displayO const coutnum scoreendl;private:int num;in
32、t main()const Student stud(101,78.5);stud.displayO;/stud.change( 101,80.5);stud.displayO;return 0;XT3-7-2.CPP#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) const num=n;score=s;void displayO const coutnum scoreendl; private:mutable in
33、t num;mutable float score;);const Student stud(101,78.5);stud.displayO;stud.change( 101,80.5);stud.displayO;return 0;XT3-7-3.CPP#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO coutnum scoreendl; private:int
34、 num;int main()Student stud(101,78.5);Student *p=&stud;p-display();p-change( 101,80.5);p-display();return 0;xt3-7-4-a.cpp#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO coutnum scoreendl; private:mutable in
35、t num;mutable float score;);Student stud(101,78.5);const Student *p=&stud;p-display();p-change( 101,80.5);p-display();return 0;xt3-7-4-b.cpp#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO constcoutnum score
36、endl; private:int num;int main()Student stud(101,78.5);const Student *p=&stud;p-display();stud.change(l 01,80.5);p-display();return 0;XT3-7-5.CPP#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void displayO coutnum score
37、endl; private:int num;float score;);int main()Student stud(101,78.5);Student * const p=&stud;p-display();p-change( 101,80.5);p-display();return 0;XT3-8.CPP#include using namespace std; class Studentpublic:Student(int n,float s):num(n),score(s) void change(int n,float s) (num=n;score=s; void displayO
38、 coutnum scoreendl;private:int num;float score;);int main()Student stud(101,78.5);void fun(Student&);fun(stud);return 0;)void fun(Student &stu)stu.displayO;stu.change( 101,80.5);stu.displayO;XT3-9.CPP#include using namespace std;class Productpublic:Product(int n,int q,float p):num(n),quantity(q),pri
39、ce(p);void total();static float average();static void displayO;private:int num;int quantity;float price;static float discount;static float sum;static int n;I;void Product: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()(