C程序設(shè)計(jì)基礎(chǔ)課后答案第五章_第1頁
C程序設(shè)計(jì)基礎(chǔ)課后答案第五章_第2頁
C程序設(shè)計(jì)基礎(chǔ)課后答案第五章_第3頁
C程序設(shè)計(jì)基礎(chǔ)課后答案第五章_第4頁
C程序設(shè)計(jì)基礎(chǔ)課后答案第五章_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、5.1 閱讀下列程序,寫出執(zhí)行結(jié)果1#include struct data int n ; float score ; ;void main() data a3 = 1001,87,1002,72,1003,90 , *p = a ; cout n endl ; cout n endl ; cout n+ endl ; cout (*p).n+ endl ; 2#include struct employee char name 20 ; char sex ; ; void fun( employee *p ) if( (*p).sex = m )cout (*p).name endl ;

2、void main() employee emp5 = Liming, m, Wangxiaoping, f, Luwei, m ;int i ; for( i=0; i3; i+ ) fun( emp+i ) ; 3 #include struct node char * s ;node * q ; ; void main() node a = Mary, a+1 , Jack, a+2 , Jim, a ;node *p = a ;cout s endl ;cout q-s endl ;cout q-q-s endl ;cout q-q-q-s endl ; 4#include class

3、 A public : int f1(); int f2(); void setx( int m ) x = m ; cout x endl; void sety( int n ) y = n ; cout y endl; int getx() return x ; int gety() return y ; private : int x, y ; ; int A:f1() return x + y ; int A:f2() return x - y ; void main() A a ; a.setx( 10 ) ; a.sety( 5 ) ; cout a.getx() t a.gety

4、() endl ; cout a.f1() t a.f2() endl ; 5 #include class T public :T( int x, int y ) a = x ; b = y ; cout 調(diào)用構(gòu)造函數(shù)1. endl ; cout a t b endl ; T( T &d ) cout 調(diào)用構(gòu)造函數(shù)2. endl ; cout d.a t d.b endl ; T() cout 調(diào)用析構(gòu)函數(shù).endl; int add( int x, int y = 10 ) return x + y ; private : int a, b ; ; void main() T d1( 4,

5、 8 ) ;T d2( d1 ) ; cout d2.add( 10 ) endl ; 6 #include class T public: T(int x) a=x; b+=x; static void display(T c) couta=c.atb=c.bendl; private: int a; static int b; ; int T:b=5; void main() T A(3),B(5); T:display(A); T:display(B); 7 #include #include class point public : point( float x, float y )

6、a = x; b = y;? cout 點(diǎn)( a , b ) ; friend double d( point &A , point &B ) return sqrt(A.a-B.a) * (A.a-B.a) + (A.b-B.b) * (A.b-B.b) ; private: double a, b ; void main() point p1( 2, 3 ) ; cout 到 ; point p2( 4, 5 ) ; cout 的距離是: d( p1,p2 ) endl ; 8 #include class A public :A() a = 5 ; void printa() cout

7、A:a = a endl ; private : int a ; friend class B ; ; class B public: void display1( A t ) t.a + ; cout display1:a = t.a endl ; ; void display2( A t ) t.a - ; cout display2:a = t.a endl ; ; ; void main() A obj1 ; B obj2 ; obj1.printa() ; obj2.display1( obj1 ) ; obj2.display2( obj1 ) ; obj1.printa() ;

8、5.2 思考題1結(jié)構(gòu)和類有什么區(qū)別?如果把程序中定義結(jié)構(gòu)的關(guān)鍵字struct直接改成class,會有什么問題?用教材上的一個(gè)例程試一試,想一想做什么修改能使程序正確運(yùn)行?2分析以下說明結(jié)構(gòu)的語句 struct node int data; node error; /錯(cuò)誤 node * ok; /正確 ;error和ok分別屬于什么數(shù)據(jù)類型?有什么存儲要求?error出錯(cuò)的原因是什么?3有說明 class A int a; double x;public: funMember(); A a1, a2, a3 ;編譯器為對象a1,a2,a3開辟了什么內(nèi)存空間?它們有各自的funMember函數(shù)的副

9、本嗎?C+通過什么機(jī)制調(diào)用類的成員函數(shù)?4C+提供了默認(rèn)版本的構(gòu)造函數(shù),為什么還需要用戶自定義構(gòu)造函數(shù)?編寫一個(gè)驗(yàn)證程序,說明自定義構(gòu)造函數(shù)的必要性。5試從定義方式、訪問方式、存儲性質(zhì)和作用域分析類的一般數(shù)據(jù)成員和靜態(tài)數(shù)據(jù)成員的區(qū)別,并編寫一個(gè)簡單程序驗(yàn)證之。6試從定義方式、調(diào)用方式分析靜態(tài)成員函數(shù)和友員函數(shù)的區(qū)別??疾旖滩牡睦?-22,若class Goods的指針域Goods * next;被聲明為私有(private)成員,程序會出現(xiàn)什么錯(cuò)誤?做什么最小修改能使程序正確運(yùn)行?請你試一試。1分析以下說明結(jié)構(gòu)的語句struct Node int data;Node error; /錯(cuò)誤Nod

10、e * ok; /正確;error和ok分別屬于什么數(shù)據(jù)類型?有什么存儲要求?error出錯(cuò)的原因是什么?【答案】error是Node結(jié)構(gòu)類型數(shù)據(jù)成員,錯(cuò)誤。原因是結(jié)構(gòu)定義的數(shù)據(jù)成員若為本身的結(jié)構(gòu)類型,是一種無窮遞歸。ok是指向Node類型的指針,定義正確,占4字節(jié)。2本章例5-5中用輔助數(shù)組對結(jié)構(gòu)數(shù)組做關(guān)鍵字排序,有定義person *index100;index數(shù)組存放結(jié)構(gòu)數(shù)組元素的地址。如果把index定義改為int index100;用于存放結(jié)構(gòu)數(shù)組元素的下標(biāo)??梢詫?shí)現(xiàn)對結(jié)構(gòu)數(shù)組的索引排序嗎?如何修改程序?請你試一試?!敬鸢浮靠梢?。關(guān)鍵是通過整型索引數(shù)組元素作為下標(biāo)訪問結(jié)構(gòu)數(shù)組。表示為

11、: allpii.id allpii.salary有關(guān)程序如下:#includeusing namespace std;struct person /說明結(jié)構(gòu)類型 char name10;unsigned int id; double salary; ;void Input( person, const int );void Sort( person, int,const int );void Output( const person, int,const int );int main() person allone100 ; /說明結(jié)構(gòu)數(shù)組int index100; /

12、說明索引數(shù)組int total ; for(int i=0; i100; i+) /索引數(shù)組元素值初始化為結(jié)構(gòu)數(shù)組元素下標(biāo)indexi=i ;couttotal;cout輸入職工信息:n;Input(allone,total);cout以工資做關(guān)鍵字排序n;Sort(allone,index, total);cout輸出排序后信息:n;Output(allone,index,total);void Input( person all, const int n ) int i ; for( i=0; in; i+ ) / 輸入數(shù)據(jù) ;cout alli.id;cout

13、 alli.salary ;void Sort(person all, int pi, const int n) int i,j; int t; /交換用中間變量for(i=1; in; i+) /以成員salary做關(guān)鍵字排序 for(j=0; jallpij+1.salary) /通過索引數(shù)組訪問結(jié)構(gòu)數(shù)組元素 t=pij; /交換索引數(shù)組元素值pij=pij+1;pij+1= t;void Output(const person all, int pi, const int n) for( int i=0; in; i+ ) / 輸出排序后數(shù)據(jù)tallpii.

14、idtallpii.salaryendl;3有以下結(jié)構(gòu)說明和遍歷單向鏈表的函數(shù)。函數(shù)內(nèi)有錯(cuò)誤嗎?是什么性質(zhì)的錯(cuò)誤?請上機(jī)驗(yàn)證你的分析。struct Node int data; Node * next; ;void ShowList( Node *head ) while( head ) cout date next4結(jié)構(gòu)和類有什么區(qū)別?如果把程序中定義結(jié)構(gòu)的關(guān)鍵字struct直接改成class,會有什么問題?用教材上的一個(gè)例程試一試,想一想做什么修改能使程序正確運(yùn)行?【答案】結(jié)構(gòu)是數(shù)據(jù)的封裝,類是數(shù)據(jù)和操作的封裝??梢园呀Y(jié)構(gòu)看成是類的特例。結(jié)構(gòu)和類都可以用關(guān)鍵字struct或class定義。

15、區(qū)別是,struct定義的結(jié)構(gòu)或類的全部成員都是公有的,用class定義的結(jié)構(gòu)或類不做聲明的成員是私有的。若把struct改成class,只需要把全部成員定義為public就可以了。5有說明class A int a;double x;public:funMember();A a1, a2, a3 ;編譯器為對象a1、a2和a3開辟了什么內(nèi)存空間?它們有各自的funMember函數(shù)的副本嗎?C+通過什么機(jī)制調(diào)用類的成員函數(shù)?【答案】開辟的存儲空間有a1.a, a1.x, a2.a, a2.x, a3.a, a3.x。各對象沒有funMember函數(shù)的副本,C+通過this指針調(diào)用成員函數(shù)。6C

16、+提供了默認(rèn)版本的構(gòu)造函數(shù),為什么還需要用戶自定義構(gòu)造函數(shù)?編寫一個(gè)驗(yàn)證程序,說明自定義構(gòu)造函數(shù)的必要性。【答案】類的默認(rèn)構(gòu)造函數(shù)可以建立基本類型數(shù)據(jù)成員的存儲空間?;谝韵聝蓚€(gè)原因,需要用戶定義構(gòu)造函數(shù):(1)對數(shù)據(jù)成員的值做指定初始化;(2)類的數(shù)據(jù)是由指針管理的堆。程序略。7試從定義方式、訪問方式、存儲性質(zhì)和作用域分析類的一般數(shù)據(jù)成員和靜態(tài)數(shù)據(jù)成員的區(qū)別,并編寫一個(gè)簡單程序驗(yàn)證它。定義方式 訪問方式 存儲性質(zhì) 作用域 一般數(shù)據(jù)成員 類中定義 對象.數(shù)據(jù)成員 局部數(shù)據(jù) 由訪問屬性public, protected, private決定 靜態(tài)數(shù)據(jù)成員 類中聲明,類外定義 對象.數(shù)據(jù)成員 類:

17、數(shù)據(jù)成員 全局?jǐn)?shù)據(jù) 程序略。 8試從定義方式、調(diào)用方式分析常成員函數(shù)、靜態(tài)成員函數(shù)和友員函數(shù)的區(qū)別。考察教材的例5-22,若class Goods的指針域 Goods * next;被聲明為私有(private)成員,程序會出現(xiàn)什么錯(cuò)誤?做什么最小修改能使程序正確運(yùn)行?請你試一試。 【答案】 定義方式 調(diào)用方式 常成員函數(shù) 函數(shù)原型以const做后綴 this指針被約束為指向常量的常指針 與一般成員函數(shù)調(diào)用形式相同 對數(shù)據(jù)成員只讀 靜態(tài)成員函數(shù) 以static做函數(shù)原型前綴 沒有this指針 通過類或?qū)ο笳{(diào)用 用于操作靜態(tài)數(shù)據(jù)成員 友員函數(shù) 以friend做函數(shù)原型前綴 沒有this指針 通過

18、參數(shù)訪問對象 可以訪問對象的不同屬性的成員 在例5-22中,若把next聲明為私有數(shù)據(jù)成員,只須把有關(guān)指針操作的函數(shù)定義為友員函數(shù)就可以了: friend void purchase( Goods * &f, Goods *& r, int w );friend void sale( Goods * & f , Goods * & r );9設(shè)有 class M int a ;class N M m; int b; void fun() /*/ ;int main() N n; N *p=&n; /*/ 描述在N:fun中如何訪問M類的數(shù)據(jù)成員a;在main函數(shù)中又如何訪問對象n的全部數(shù)據(jù)成員

19、? 【答案】 在N:fun中訪問M類的數(shù)據(jù)成員a的形式是: m.a在main函數(shù)中訪問M類的數(shù)據(jù)成員的形式是: n.b n.m.a5.3 編程題1使用結(jié)構(gòu)類型表示復(fù)數(shù)。設(shè)計(jì)程序輸入兩個(gè)復(fù)數(shù),可以選擇進(jìn)行復(fù)數(shù)的+、或運(yùn)算,并輸出結(jié)果。答案 5.3-1 #include #include struct complexfloat re,im;void main() complex a,b,c; char oper;cout a.re a.im;cout b.re b.im;cout oper;switch ( oper ) case +: c.re=a.re+b.re; c.im=a.im+b.im

20、; break;case -: c.re=a.re-b.re; c.im=a.im-b.im; break;case *: c.re=a.re*b.re-a.im*b.im; c.im=a.im*b.re+a.re*b.im; break;case /: c.re=(a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im); c.im=(a.im*b.re-a.re*b.im)/(b.re*b.re+b.im*b.im); break;default: cout input error! endl;return;cout c= c.re;cout setiosfla

21、gs( ios:showpos );cout c.im i endl;return;2把一個(gè)班的學(xué)生姓名和成績存放到一個(gè)結(jié)構(gòu)數(shù)組中,尋找和輸出最高分者。答案 5.3-2 #include void main() struct data char name12; float score; a = 李小平,90,何文章,66,劉大安,87,汪立新,90,羅建國, 78,陸豐收,81,楊勇,85,吳一兵,55,伍曉笑,68,張虹虹,90; float max = a0.score;int i,n = sizeof(a) / sizeof(data); for( i=1; i max ) max =

22、ai.score; for( i=0; in; i+ ) if( ai.score = max ) cout endl;3使用結(jié)構(gòu)表示XY平面直角坐標(biāo)系上的點(diǎn),編寫程序順序讀入一個(gè)四邊形的四個(gè)頂點(diǎn)坐標(biāo),判別由這四個(gè)頂點(diǎn)的連線構(gòu)成的圖形是否正方形、矩形或其它四邊形。要求定義求兩個(gè)點(diǎn)距離的函數(shù)使用結(jié)構(gòu)參數(shù)。答案 5.3-3 struct coordinates double x,y; ;#include #include double distance( coordinates p1,coordinates p2 ) return sqrt( pow( p1.x-p2.x,2 )+

23、pow( p1.y-p2.y,2 ) ); void main() int i; coordinates p5; for( i=1; i=4; i+ ) cout 輸入第 i pi.x pi.y; if(fabs( distance( p1,p2 ) - distance( p3,p4 )=1e-8& fabs( distance( p1,p4 ) - distance( p2,p3 )=1e-8& fabs( distance( p1,p3 ) - distance( p2,p4 )=1e-8)if( fabs( distance( p1,p2 ) - distance( p2,p3 )1e

24、-8 ) cout 四個(gè)頂點(diǎn)構(gòu)成的圖形為正方形! endl;else cout 四個(gè)頂點(diǎn)構(gòu)成的圖形為矩形! endl; else cout 四個(gè)頂點(diǎn)構(gòu)成的圖形為其它四邊形! endl;4建立一個(gè)結(jié)點(diǎn)包括職工的編號、年齡和性別的單向鏈表,分別定義函數(shù)完成以下功能:( 1)遍歷該鏈表輸出全部職工信息;( 2)分別統(tǒng)計(jì)出男女性職工的人數(shù);( 3)在鏈表尾部插入新職工結(jié)點(diǎn);( 4)刪除指定編號的職工結(jié)點(diǎn);( 5)刪除年齡在60歲以上的男性職工或55歲以上的女性職工結(jié)點(diǎn),保存在另一個(gè)鏈表中。主函數(shù)建立簡單菜單選擇,測試你的程序。 答案 5.3-4 #include struct employeeint

25、num;int age;char sex;employee *next;employee *head, *head1; /建立單向鏈表 employee *create()employee *head, *p, *pend;char ch;head = NULL;cout ch;if( ch = y ) p = new employee; cout p-num; cout p-age;cout p-sex; else goto L0; while( ch = y ) if( head = NULL ) head = p; else pend-next = p; pend = p;cout ch

26、; if( ch = y ) p = new employee;cout p-num;cout p-age;cout p-sex; pend-next = NULL; L0: ; return head; /顯示單向鏈表中全部職工信息 void show( employee *head ) employee *p = head; if( !head ) cout t空鏈表! endl; goto L1; cout t鏈表中的數(shù)據(jù)是: n; while( p ) cout t num , age , sex next; L1: ; /統(tǒng)計(jì)男女職工人數(shù) void count( employee *

27、head ) employee *p = head; int m, f; m = 0; f = 0; while( p ) if( p-sex = m ) m+; else f+; p = p-next; cout t男職工人數(shù): m endl; cout t女職工人數(shù): f endl; /在鏈表尾部插入新結(jié)點(diǎn) employee *insert() employee *pend = head, *p; /在空鏈表尾部插入新結(jié)點(diǎn) if( !head ) p = new employee; cout p-num; cout p-age; cout p-sex; head = p; p-next =

28、 NULL; return head; /在鏈表尾部插入新結(jié)點(diǎn) while( pend-next != NULL ) pend = pend-next; p = new employee; cout p-num; cout p-age; cout p-sex; pend-next = p; pend = p; pend-next = NULL; return head; /刪除指定編號的結(jié)點(diǎn) employee *del( int bh ) employee *p, *q; if ( !head ) cout t空鏈表! num = bh ) p = head; head = head-next

29、; delete p; cout t結(jié)點(diǎn)已被刪除! next != NULL ) if ( q-next-num = bh ) p = q-next; /待刪除結(jié)點(diǎn) q-next = p-next; delete p; cout t結(jié)點(diǎn)已被刪除! next; cout t找不到需刪除結(jié)點(diǎn)! endl; L2: ; return ( head ); /刪除指定年齡段的結(jié)點(diǎn),并把被刪除結(jié)點(diǎn)保存在另一鏈表中 employee *delcreate() employee *p, *pd, *p1, *q; int flag; /建立新鏈表 if ( head = NULL ) cout t空鏈表! a

30、ge = 55 & p-age num = p-num; pd-age = p-age; pd-sex = p-sex; if( head1 = NULL ) head1 = pd; else p1-next = pd; p1 = pd; pd = new employee; flag = 1; p = p-next; if ( flag = 0 ) cout t沒有需刪除的結(jié)點(diǎn)! next = NULL; /顯示新鏈表 cout t新鏈表中的數(shù)據(jù)是: n;p = head1;while( p ) cout t num , age , sex next; /刪除指定年齡的結(jié)點(diǎn) p = head

31、; q = p;while ( p != NULL ) if( p-age = 55 & p-age age = p-age )pd = head; /待刪除結(jié)點(diǎn) head = head-next;delete pd;p = head;continue;else if( p-next = NULL ) pd = p; /待刪除結(jié)點(diǎn) q-next = NULL; delete pd; goto L3; else pd = p; /待刪除結(jié)點(diǎn) q-next = p-next; delete pd; p = q-next; continue; q = p;p = p-next; L3: return

32、 ( head );void main() int choice, bh ; L: cout ntt請鍵入操作選擇n endl; cout t 1 - 建立單向鏈表 endl; cout t 2 - 顯示單向鏈表中全部職工信息 endl; cout t 3 - 統(tǒng)計(jì)男女職工人數(shù) endl; cout t 4 - 在職工尾部插入新結(jié)點(diǎn) endl; cout t 5 - 刪除指定編號的結(jié)點(diǎn) endl; cout t 6 - 刪除指定年齡的結(jié)點(diǎn),并把被刪除結(jié)點(diǎn)保存在另一鏈表中 endl; cout t 0 - 退出 endl ; cout choice ; switch ( choice ) cas

33、e 1 : head = create() ; goto L ; case 2 : show( head );? goto L ; case 3 : count( head ); goto L; case 4 : head = insert(); goto L; case 5 : cout bh; head = del( bh ); goto L; case 6 : head = delcreate(); goto L; case 0 : cout t退出程序的運(yùn)行!n endl ; break ; default :cout t輸入錯(cuò)誤,請重新輸入!n endl ; goto L; 5輸入一

34、行字符,按輸入字符的反序建立一個(gè)字符結(jié)點(diǎn)的單向鏈表,并輸出該鏈表中的字符。答案 5.3-5 為了形成反序鏈表,每讀入一個(gè)字符都把它插入在表頭。#include #include struct node char ch; node *next; ;void show( node *head );void main() node *head, *p; char c; head = NULL; while( (c = getchar() != n ) /輸入一行字符 p = new node; /建立新結(jié)點(diǎn) p-ch = c; p-next = head; /插入表頭 head=p; show(he

35、ad);void show( node *head ) /輸出鏈表 node *p = head; cout 鏈表中的字符是: n; while( p ) cout ch; p = p-next; cout endl;6定義一個(gè)Book(圖書)類,在該類定義中包括數(shù)據(jù)成員: bookname(書名)、price(價(jià)格)和number(存書數(shù)量);成員函數(shù): display()顯示圖書的情況;borrow()將存書數(shù)量減1,并顯示當(dāng)前存書數(shù)量;restore()將存書數(shù)量加1,并顯示當(dāng)前存書數(shù)量。在 main函數(shù)中,要求創(chuàng)建某一種圖書對象,并對該圖書進(jìn)行簡單的顯示、借閱和歸還管理。答案 5.3-

36、6 #include #include #include class Book public:void setBook(char*,float,int); void borrow(); void restore(); void display(); private: char bookname40; float price; int number;/在類外定義Book類的成員函數(shù) void Book:setBook(char *name, float pri, int num) strcpy(bookname, name);price=pri;number=num;void Book:borr

37、ow() if (number=0 ) cout 已沒存書,退出! endl; abort(); number = number - 1; cout 借一次,現(xiàn)存書量為: number endl;void Book:restore() number = number + 1;cout 還一次,現(xiàn)存書量為: number endl;void Book:display() cout 存書情況: endl bookname: bookname endl price: price endl number: number endl;void main()char flag, ch; Book compu

38、ter;computer.setBook( c+程序設(shè)計(jì)基礎(chǔ) , 32, 1000 );computer.display(); ch = y;while ( ch = y ) cout flag;switch ( flag ) case b: computer.borrow(); break; case r: computer.restore(); cout ch; computer.display();7定義一個(gè)Box(盒子)類,在該類定義中包括數(shù)據(jù)成員: length(長)、width(寬)和height(高);成員函數(shù): 構(gòu)造函數(shù) Box 設(shè)置盒子長、寬和高三個(gè)初始數(shù)據(jù);函數(shù)volume 計(jì)算并輸出盒子的體積。在 main函數(shù)中,要求創(chuàng)建Box對象,并求盒子的體積。答案 5.3-7 #include class BOX public:BOX( float l, float w, float h ) length = l; width = w; height = h; void volume() cout volume= length * width * height endl; private: float length, widt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論