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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、第一次作業(yè)的第二題我沒有使用位運算已經(jīng)做出來了,但是我在嘗試位運算時一直得不出結(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)中加入打印語句跟蹤: printf(%0 x %0 x=, i, ai);會發(fā)現(xiàn)從0 xc0開始,逆轉(zhuǎn)后都是0 xff,這肯定不對,原因我想還是我課上講過的,計算過程中自動轉(zhuǎn)化成帶符號位的int了。需要多次左右移位的,必須分步移位。看來即使每個數(shù)字前加 (unsigned char) 也沒用。不能用一個表達式: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);來實現(xiàn)。=2. 第一次作業(yè)第一題老師,按照您上課說的我改了一下程序,發(fā)現(xiàn)還是不對(就是文件以空格結(jié)束),fscanf沒有返回0,讀到空格(就是沒有內(nèi)容)返回了-1,這是因為vs2012和vs2008的不同造成的?我查

5、了一下書發(fā)現(xiàn)寫的是返回數(shù)據(jù)個數(shù),按理說不該是-1啊。 答:vs2012和vs2008是否不同不是很清楚,那就判斷fscanf()=0 終止吧又問: 我就是換成了=0不過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.不成功確實返回0或者-1(EOF)。=3. 關于第一次作業(yè)第二題:今天想起檢驗一下,可發(fā)現(xiàn)程序達不到預期目標.老師,我的程序是這樣的。#include#include#includemain()FIL

7、E *fp,*fq;unsigned char c;unsigned char flag=NULL;/NULL的二進制碼為00000000int i,sign=1;/sign的二進制碼為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)進行每個字符的操作:比如將第k個轉(zhuǎn)移至第8-k個(第k個記為k)(以第3個為例):(1)首先得到00300000(i=5);(2)然后得到00000003(右移5位);(3)最后得到00000300(左移7-5=2位);即完成倒置,其余類似,最后相加即可。*/fputc(flag,fq);fclose(fp);fclose(fq);我想問下,就是我的這個方法能行么。答:你的程序中,不要寫unsigned char flag=NULL;這會有編譯警告錯誤。NULL是 void *類型的,專門給指針賦空值

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

10、個對象沒有問題,析構(gòu)函數(shù)把str所指的動態(tài)內(nèi)存delete釋放了,但析構(gòu)第二個對象時,由于str所指的動態(tài)內(nèi)存已經(jīng)被析構(gòu)第一個對象時釋放掉了,導致本次delete會出現(xiàn)致命錯誤。 (2) 給兩個類設置同一個友元函數(shù),就是把 friend double TotalWeight(Boat &B, Car &C); 在每個類的public中列一次。 在class Boat 定義前加 class Car; 進行類的向前引用說明。=5. 關于作業(yè)中的小問題#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()中默認值不能變成0,0,0,0,我如果給一個6,他就變成6,0,0,0,了,但如果什么都不寫編譯說error C2228: “.Print”的左邊必須有類/結(jié)構(gòu)/聯(lián)合;我有點沒懂,請老師指點迷津答:出現(xiàn)編譯錯誤:test.cpp(31) : error C2668: “sqrt”: 對重載函數(shù)的調(diào)用不明確 是因為

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

15、ther;就沒有問題了。 =6. 程設第三次作業(yè)選做題老師您看下這次的選做題是不是這個意思,有一處我沒懂,每一次調(diào)用TotalWeight函數(shù)時都會調(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;答:運行結(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請按任意鍵繼續(xù). . . 由于TotalWeight函數(shù)的形參是Boat B, Car C,每次調(diào)用TotalWeight函數(shù)時都會調(diào)用復制構(gòu)造函數(shù)來復制構(gòu)造C和B,函數(shù)執(zhí)行完也就調(diào)用相應析構(gòu)函數(shù)析構(gòu)掉這兩個對象。因為你沒給Boat和Car類寫復制構(gòu)造函數(shù),編譯系統(tǒng)會自動各生成一個來完成此事,但不打印信息,你看不到。如果你在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;你就會發(fā)現(xiàn)每次調(diào)用TotalWeight函數(shù)時都會調(diào)用這兩個復制構(gòu)造函數(shù)來復制構(gòu)造C和B。構(gòu)造和析構(gòu)是一一對應的。 =7. 作業(yè)疑問我的程序?qū)懞昧?,但是出現(xiàn)了這樣的問題不知道是哪里出現(xiàn)了問題,希望您能點撥一下,可能是我對于類的掌握還不夠吧,謝謝老師!#includeusing namespace std;class CIRCLEpublic:int x; /成員數(shù)據(jù)圓心橫坐標int y; /成員數(shù)據(jù)圓心縱坐標CIRCLE(int x,int y);/設置構(gòu)造函數(shù)CIRCLE(CIRCLE &p);/復制構(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圓心的橫坐標為x 圓心

溫馨提示

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

評論

0/150

提交評論