《C面向对象程序设计答案18章谭浩强1.pdf》由会员分享,可在线阅读,更多相关《C面向对象程序设计答案18章谭浩强1.pdf(25页珍藏版)》请在金锄头文库上搜索。
1、1 第一章 5 include using namespace std int main cout This is cout a C cout program endl return 0 6 include using namespace std int main int a b c a 10 b 23 c a b cout a b cout c cout endl return 0 7 include using namespace std int main int a b c int f int x int y int z cin a b c c f a b c cout c endl r
2、eturn 0 int f int x int y int z int m if x y m x else m y if z m m z return m 8 include using namespace std int main int a b c cin a b c a b cout a b a b endl return 0 9 include using namespace std int main int add int x int y int a b c cin a b c add a b cout a b c endl return 0 int add int x int y
3、int c c x y return c 10 include using namespace std int main void sort int x int y int z int x y z cin x y z sort x y z return 0 void sort int x int y int z int temp if x y temp x x y y temp 内 3 个语句的作用是将 x 和 y 的值互换 if z x cout z x y en dl else if z y cout x z y en dl else cout x y z en dl 11 include
4、 using namespace std int main int max int a int b int c 0 int a b c cin a b c cout max a b c max a b c endl cout max a b max a b a a b if c a a c return a 12 include 2 using namespace std int main void change int int int a b cin a b if a b change a b cout max a min b endl return 0 void change int in
5、t int r1 r2 temp temp r1 r1 r2 r2 temp 13 include using namespace std int main void sort int int a b c a1 b1 c1 cout a b c a1 a b1 b c1 c sort a1 b1 c1 cout a b c in sorted order is cout a1 b1 c1 j change i j if i k change i k if j k change j k void change int temp x x y y temp 14 include include us
6、ing namespace std int main string s1 week s2 end cout s1 s1 endl cout s2 s2 endl s1 s1 s2 cout The new string is s1 endl return 0 15 include include using namespace std int main string str int i n char temp cout str n str size for i 0 i n 2 i temp str i str i str n i 1 str n i 1 temp cout str endl r
7、eturn 0 16 include include using namespace std int main int i string str 5 BASIC C FORTRA N C PASCAL void sort string sort str cout the sorted strings endl for i 0 i 5 i cout str i cout endl return 0 void sort string s int i j string t for j 0 j 5 j for i 0 is i 1 t s i s i s i 1 s i 1 t 17 include
8、include using namespace std int main long c 5 10100 123567 1198783 165654 3456 int a 5 1 9 0 23 45 float b 5 2 4 7 6 5 5 6 6 2 3 void sort int void sort float void sort long sort a sort b sort c return 0 3 void sort int a int i j t for j 0 j 5 j for i 0 ia i 1 t a i a i a i 1 a i 1 t cout the sorted
9、 numbers endl for i 0 i 5 i cout a i cout endl endl void sort long a int i j long t for j 0 j 5 j for i 0 ia i 1 t a i a i a i 1 a i 1 t cout the sorted numbers endl for i 0 i 5 i cout a i cout endl endl void sort float a int i j float t for j 0 j 5 j for i 0 ia i 1 t a i a i a i 1 a i 1 t cout the
10、sorted numbers endl for i 0 i 5 i cout a i cout endl endl 18 include include using namespace std template void sort T a int i j min T t for i 0 i 5 i min i for j i 1 ja j min j t a i a i a min a min t cout the sorted numbers endl for i 0 i 5 i cout a i cout endl endl int main int a 5 1 9 0 23 45 flo
11、at b 5 2 4 7 6 5 5 6 6 2 3 long c 5 10100 123567 1198783 165654 3456 sort a sort b sort c return 0 第二章 1 include using namespace std class Time public void set time void show time private 成员改为公用的 int hour int minute int sec void Time set time 在 main 函数之前定义 cin hour cin minute cin sec void Time show
12、time 在 main 函数之前定义 cout hour minute sec endl int main Time t1 t1 set time t1 show time return 0 2 include using namespace std class Time public void set time void cin hour cin minute cin sec void show time void 4 cout hour minute sec endl private int hour int minute int sec Time t int main t set tim
13、e t show time return 0 3 include using namespace std class Time public void set time void void show time void private int hour int minute int sec void Time set time void cin hour cin minute cin sec void Time show time void cout hour minute sec endl Time t int main t set time t show time return 0 4 x
14、t2 4 1 cpp main cpp include using namespace std include xt2 4 h int main Student stud stud set value stud display return 0 xt2 4 2 cpp 即 student cpp include xt2 4 h 在此文件中进行函数的定 义 include using namespace std 不要漏写此行 void Student display cout num num endl cout name name en dl cout sex sex num cin name
15、cin sex 5 xt2 5 1 cpp file1 cpp include include xt2 5 h int main Array max arrmax arrmax set value arrmax max value arrmax show value return 0 xt2 5 2 cpp arraymax cpp include using namespace std include xt2 5 h void Array max set value int i for i 0 i array i void Array max max value int i max arra
16、y 0 for i 1 imax max array i void Array max show value cout max max endl 6 解法一 include using namespace std class Box public void get value float volume void display public float lengh float width float height void Box get value cout lengh cin width cin height float Box volume return lengh width height void Box display cout volume endl int main Box box1 box2 box3 box1 get value cout volmue of bax1 is box1 display box2 get value cout volmue of bax2 is box2 display box3 get value cout volmue of bax