老師問(wèn)題集錦_第1頁(yè)
老師問(wèn)題集錦_第2頁(yè)
老師問(wèn)題集錦_第3頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

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

文檔簡(jiǎn)介

1、第一次作業(yè)的第二題我沒有使用位運(yùn)算已經(jīng)做出來(lái)了,但是我在嘗試位運(yùn)算時(shí)一直得不出結(jié)果,您能不能看看怎么回事?#include#includemain()FILE *fp,*fq;unsigned char i,x8,a256;for(i=0;i+)/x0=i%2;x1=(i/2)%2;x2=(i/4)%2;x3=(i/8)%2;x4=(i/16)%2;/x5=(i/32)%2;x6=(i/64)%2;x7=(i/128)%2;/ai=x0*128+x1*64+x2*32+x3*16+x4*8+x5*4+x6*2+x7;ai=(i(unsigned char)1(unsigned char)1)|

2、(i(unsigned char)2(unsigned char)2)|(i(unsigned char)3(unsigned char)3)|(i(unsigned char)7(unsigned char)3)|(i(unsigned char)7(unsigned char)2) |(i(unsigned char)7(unsigned char)7);if(i=255)break;if(fp=fopen(fp.txt,rb)=NULL)printf(cannot open fp.datn);exit(0);if(fq=fopen(fq.txt,ab)=NULL)printf(canno

3、t open fq.datn);exit(0);while(!feof(fp)if(fread(&i,1,1,fp)=0)break;fwrite(&ai,1,1,fq);fclose(fq);fclose(fp);答:循環(huán)中加入打印語(yǔ)句跟蹤: printf(%0 x %0 x=, i, ai);會(huì)發(fā)現(xiàn)從0 xc0開始,逆轉(zhuǎn)后都是0 xff,這肯定不對(duì),原因我想還是我課上講過(guò)的,計(jì)算過(guò)程中自動(dòng)轉(zhuǎn)化成帶符號(hào)位的int了。需要多次左右移位的,必須分步移位??磥?lái)即使每個(gè)數(shù)字前加 (unsigned char) 也沒用。不能用一個(gè)表達(dá)式:ai=(i(unsigned char)1(unsigned c

4、har)1) |(i(unsigned char)2(unsigned char)2) |(i(unsigned char)3(unsigned char)3) |(i(unsigned char)7(unsigned char)3) |(i(unsigned char)7(unsigned char)2) |(i(unsigned char)7(unsigned char)7);來(lái)實(shí)現(xiàn)。=2. 第一次作業(yè)第一題老師,按照您上課說(shuō)的我改了一下程序,發(fā)現(xiàn)還是不對(duì)(就是文件以空格結(jié)束),fscanf沒有返回0,讀到空格(就是沒有內(nèi)容)返回了-1,這是因?yàn)関s2012和vs2008的不同造成的?我查

5、了一下書發(fā)現(xiàn)寫的是返回?cái)?shù)據(jù)個(gè)數(shù),按理說(shuō)不該是-1啊。 答:vs2012和vs2008是否不同不是很清楚,那就判斷fscanf()=0 終止吧又問(wèn): 我就是換成了=0不過(guò)EOF的值是不是-1?它能夠返回EOF嗎? 用F1鍵查看fscanf的在線幫助Return Value Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned.

6、A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf and fwscanf.不成功確實(shí)返回0或者-1(EOF)。=3. 關(guān)于第一次作業(yè)第二題:今天想起檢驗(yàn)一下,可發(fā)現(xiàn)程序達(dá)不到預(yù)期目標(biāo).老師,我的程序是這樣的。#include#include#includemain()FIL

7、E *fp,*fq;unsigned char c;unsigned char flag=NULL;/NULL的二進(jìn)制碼為00000000int i,sign=1;/sign的二進(jìn)制碼為00000001if( ( fp=fopen(a.txt,r+b) )=NULL )printf(cannot open the file!);exit(0);if( ( fq=fopen(b.txt,w+b) )=NULL )printf(cannot open the file!);exit(0);while(!feof(fp)flag=NULL;c=fgetc(fp); for(i=0;i=7;i+)fl

8、ag+=( ( c&(signi )(7-i);/*算法思想即利用sign(00000001)進(jìn)行每個(gè)字符的操作:比如將第k個(gè)轉(zhuǎn)移至第8-k個(gè)(第k個(gè)記為k)(以第3個(gè)為例):(1)首先得到00300000(i=5);(2)然后得到00000003(右移5位);(3)最后得到00000300(左移7-5=2位);即完成倒置,其余類似,最后相加即可。*/fputc(flag,fq);fclose(fp);fclose(fq);我想問(wèn)下,就是我的這個(gè)方法能行么。答:你的程序中,不要寫unsigned char flag=NULL;這會(huì)有編譯警告錯(cuò)誤。NULL是 void *類型的,專門給指針賦空值

9、用的,一般的量賦初值,直接給0。我運(yùn)行測(cè)試了你的程序,結(jié)果是對(duì)的,說(shuō)明你的逆轉(zhuǎn)算法是正確的。檢查方法:由a,txt生成b.txt后,再修改一下程序,讀入b.txt生成c.txt,看a.txt和c.txt是否相同。=4. 求教(1) 復(fù)制構(gòu)造函數(shù)是怎么構(gòu)造的啊? 為什么課件上原來(lái)那個(gè)沒有復(fù)制構(gòu)造函數(shù)而且是*str的會(huì)出錯(cuò)???在哪里公用一個(gè)地址了?(2) 怎么給兩個(gè)類設(shè)置一個(gè)友元?答:(1) 缺省復(fù)制構(gòu)造函數(shù)會(huì)把一個(gè)對(duì)象原樣復(fù)制到新的一個(gè)對(duì)象上,如果原來(lái)的對(duì)象里有指針*str,那么也把這個(gè)指針的值復(fù)制到新的對(duì)象中相應(yīng)的str中,這就導(dǎo)致兩個(gè)對(duì)象的str指針指向了同一塊動(dòng)態(tài)內(nèi)存。因此析構(gòu)時(shí),析構(gòu)第一

10、個(gè)對(duì)象沒有問(wèn)題,析構(gòu)函數(shù)把str所指的動(dòng)態(tài)內(nèi)存delete釋放了,但析構(gòu)第二個(gè)對(duì)象時(shí),由于str所指的動(dòng)態(tài)內(nèi)存已經(jīng)被析構(gòu)第一個(gè)對(duì)象時(shí)釋放掉了,導(dǎo)致本次delete會(huì)出現(xiàn)致命錯(cuò)誤。 (2) 給兩個(gè)類設(shè)置同一個(gè)友元函數(shù),就是把 friend double TotalWeight(Boat &B, Car &C); 在每個(gè)類的public中列一次。 在class Boat 定義前加 class Car; 進(jìn)行類的向前引用說(shuō)明。=5. 關(guān)于作業(yè)中的小問(wèn)題#include #include using namespace std; class Rectangle public: Rectangle(in

11、t x=0,int y=0,int a=0,int b=0); Rectangle(); void Print(); void Set(); void Area();void Diagonal();private: int x,y,a,b; ;inline Rectangle:Rectangle(int x,int y,int a,int b) this-x=x;this-y=y;this-a=a;this-b=b; cout構(gòu)造函數(shù)被調(diào)用! endl; Rectangle:Rectangle() cout析構(gòu)函數(shù)被調(diào)用! endl; void Rectangle:Print()cout(x,

12、y)endl(a,b)endl;void Rectangle:Set() x+;y=a+b;a-;b=x-y;coutchange (x,y) (a,b)endl;void Rectangle:Area()int s;s=abs(b-y)*abs(a-x);couts=sendl;void Rectangle:Diagonal()double d;d=sqrt(b-y)*(b-y)+(a-x)*(a-x);coutd=dendl;void main()Rectangle rectangle(2,5,3,6),another();/*another(6);*/another.Print();re

13、ctangle.Print();rectangle.Diagonal();rectangle.Area();rectangle.Set();rectangle.Print();rectangle.Diagonal();rectangle.Area();我不太明白為什么another()中默認(rèn)值不能變成0,0,0,0,我如果給一個(gè)6,他就變成6,0,0,0,了,但如果什么都不寫編譯說(shuō)error C2228: “.Print”的左邊必須有類/結(jié)構(gòu)/聯(lián)合;我有點(diǎn)沒懂,請(qǐng)老師指點(diǎn)迷津答:出現(xiàn)編譯錯(cuò)誤:test.cpp(31) : error C2668: “sqrt”: 對(duì)重載函數(shù)的調(diào)用不明確 是因?yàn)?/p>

14、語(yǔ)句:d=sqrt(b-y)*(b-y)+(a-x)*(a-x); 中,a,b,x,y都是int型,因此sqrt()中的表達(dá)式也是int型,造成編譯器不知道調(diào)用哪個(gè)重載的sqrt,應(yīng)該改為: d=sqrt(double)(b-y)*(b-y)+(a-x)*(a-x); 出現(xiàn)編譯錯(cuò)誤:test.cpp(35) : error C2228: “.Print”的左邊必須有類/結(jié)構(gòu)/聯(lián)合 是因?yàn)榍懊娴恼Z(yǔ)句:Rectangle rectangle(2,5,3,6),another();定義another對(duì)象如果不賦初值,就不要帶(),寫為:Rectangle rectangle(2,5,3,6),ano

15、ther;就沒有問(wèn)題了。 =6. 程設(shè)第三次作業(yè)選做題老師您看下這次的選做題是不是這個(gè)意思,有一處我沒懂,每一次調(diào)用TotalWeight函數(shù)時(shí)都會(huì)調(diào)用構(gòu)造函數(shù)么?#includeusing namespace std;class Car;class Boatpublic: Boat(int w=0); Boat(); friend void TotalWeight(Boat B, Car C); void Set(int w); void Print();private: int weight;class Carpublic: Car(int w=0); Car(); friend void

16、 TotalWeight(Boat B, Car C); void Set(int w); void Print();private: int weight;Boat:Boat(int w):weight(w) coutboat constructor called weightendl;Boat:Boat() coutboat destructor called weightendl; void Boat:Set(int w) weight=w; Print();void Boat:Print() coutboat weight=weightendl;void TotalWeight(Boa

17、t B, Car C) int add; add=B.weight+C.weight; coutadded weight=addendl;Car:Car(int w):weight(w) coutcar constructor called weightendl;Car:Car() coutcar destructor called weightendl; void Car:Set(int w) weight=w; Print();void Car:Print() coutcar weight=weightendl;int main() Boat B(4); Car C(5); B.Print

18、(); C.Print(); TotalWeight(B,C); B.Set(7); C.Set(9); TotalWeight(B,C); return 0;答:運(yùn)行結(jié)果:boat constructor called 4car constructor called 5boat weight=4car weight=5added weight=9boat destructor called 4car destructor called 5boat weight=7car weight=9added weight=16boat destructor called 7car destructor

19、 called 9car destructor called 9boat destructor called 7請(qǐng)按任意鍵繼續(xù). . . 由于TotalWeight函數(shù)的形參是Boat B, Car C,每次調(diào)用TotalWeight函數(shù)時(shí)都會(huì)調(diào)用復(fù)制構(gòu)造函數(shù)來(lái)復(fù)制構(gòu)造C和B,函數(shù)執(zhí)行完也就調(diào)用相應(yīng)析構(gòu)函數(shù)析構(gòu)掉這兩個(gè)對(duì)象。因?yàn)槟銢]給Boat和Car類寫復(fù)制構(gòu)造函數(shù),編譯系統(tǒng)會(huì)自動(dòng)各生成一個(gè)來(lái)完成此事,但不打印信息,你看不到。如果你在Boat和Car類中加入:Boat:Boat(Boat &p):weight(p.weight) coutboat copy constructor called

20、 weightendl;Car:Car(Car &p):weight(p.weight) coutcar copy constructor called weightendl;你就會(huì)發(fā)現(xiàn)每次調(diào)用TotalWeight函數(shù)時(shí)都會(huì)調(diào)用這兩個(gè)復(fù)制構(gòu)造函數(shù)來(lái)復(fù)制構(gòu)造C和B。構(gòu)造和析構(gòu)是一一對(duì)應(yīng)的。 =7. 作業(yè)疑問(wèn)我的程序?qū)懞昧?,但是出現(xiàn)了這樣的問(wèn)題不知道是哪里出現(xiàn)了問(wèn)題,希望您能點(diǎn)撥一下,可能是我對(duì)于類的掌握還不夠吧,謝謝老師!#includeusing namespace std;class CIRCLEpublic:int x; /成員數(shù)據(jù)圓心橫坐標(biāo)int y; /成員數(shù)據(jù)圓心縱坐標(biāo)CIRCLE(int x,int y);/設(shè)置構(gòu)造函數(shù)CIRCLE(CIRCLE &p);/復(fù)制構(gòu)造函數(shù) CIRCLE()cout析構(gòu)函數(shù)被調(diào)用!endl;/析構(gòu)函數(shù)(打印信息表示其被調(diào)用)void Set(int r)/新值函數(shù)R=r;void Print()/打印成員值函數(shù)cout圓心的橫坐標(biāo)為x 圓心

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論