




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、 C+大作業(yè)報告 班級 姓名 總學(xué)號 一題目:21內(nèi)容:用三種循環(huán)語句完成求100以內(nèi)的質(zhì)數(shù)設(shè)計思路:1既不是質(zhì)數(shù)也不是合數(shù),所以直接從2考慮。找出來這些數(shù)字就是要保證這個數(shù)只能讓1和其本身整除,所以讓這個數(shù)先除以2,然后慢慢整除其小于除以2后的數(shù),然后輸出這些數(shù)。 程序代碼:while 循環(huán)#includeusing namespace std;int main()int i=2;int j,n,m;while (i101)m=1;n=i/2;j=2;while (j=n)if(i%j=0)m=0;break;j+;if(m)coutiendl;i+;return 0;Do while 循環(huán)
2、#includeusing namespace std;void main()int i=2;int j,n,m;dom=1;n=i/2;j=2;doif(i%j=0)m=0;break;j+;while(j=n)if(m)coutiendl;i+while(i101);return 0;For 循環(huán)#includeusing namespace std;void main()int i,j,n,m;for (i=2;i101;i+)m=1;n=i/2;for (j=2;j=n;j+)if (i%j=0)m=0;break;if (m)coutiendl;return 0;運行結(jié)果:結(jié)論: 不
3、管for還是while還是do while,他們的循環(huán)體都是一樣的,所以只要編出來一個就等于全編出來了,而且程序要設(shè)計盡量簡單。題目:22內(nèi)容:輸入一個有符號的十進制數(shù),轉(zhuǎn)換成機內(nèi)二進制數(shù)輸出(要求用位操作運算)。設(shè)計思路:利用位運算將二進制的每一位取出存入數(shù)組,然后按要求輸出。程序代碼:#include using namespace std;void main()char a;int t8;int i;couta;for(i=0;i1;for(i=7;i=0;i-)coutti;coutendl;system(pause);結(jié)果: 結(jié)論:只有掌握位運算規(guī)則,才能編出來程序二內(nèi)容: 書上P1
4、44,4-10 設(shè)計一個用于人事管理的“人員”類 .由于考慮到通用性,這里只抽象出所有人員都具有的屬性:編號,性別,出生日期,身份證號.(“出生日期”聲明為一個“日期”類內(nèi)嵌子對象。用成員函數(shù)實現(xiàn)對人員信息的錄入和顯示。要求包括:構(gòu)造函數(shù)、復(fù)制構(gòu)造函數(shù)、內(nèi)聯(lián)成員函數(shù)、帶默認(rèn)形參值的成員函數(shù)、類的組合。)設(shè)計思路:通過構(gòu)造函數(shù),實現(xiàn)人員的錄入和輸出。程序代碼:#includeusing namespace std;class dateprivate: int year; int month; int day;public: date(int a=0,int b=0,int c=0)year=a;
5、month=b;day=c; inline void setyear(int y)year=y; void setmonth(int m) month=m; void setday(int d) day=d; void showdate() coutyear month day endl; ;class peopleprivate: char number100; char id100; char sex2; date birthday; public: people(); people(people&p); people(); void setnumber(char* a) strcpy(n
6、umber,a); void setid(char*); void setsex(char* c) strcpy(sex,c); void setbirthday(date d) birthday=d; char *getnumber() return number; char *getsex() return sex; char *getid() return id; date getbirthday() return birthday; ;date d;char m;people:people():birthday(d)void people:setid (char*ids) strcpy
7、(id,ids);int main() date birthday; cout錄入信息endl; people p1; /people*p4=&p1,&p2,&p3,&p4; cout輸入員工的出生日期endl; couta; birthday.setyear (a); coutb; birthday.setmonth (b); coutc; birthday.setday (c); cout輸入編號numberstr; p1.setnumber (numberstr); cout輸入身份證號idstr; p1.setid (idstr); cout輸入性別sexstr; p1.setsex
8、(sexstr); cout輸出信息endl; cout員工的出生日期; birthday.showdate (); cout編號為 p1.getnumber() 身份證號為 p1.getid() 性別為 p1.getsex() ; return 0;運行結(jié)果:結(jié)論: 要充分理解函數(shù)的概念,只有在理解的情況下才能編出程序。但是不能實現(xiàn)多個成員的錄入和輸出。內(nèi)容: 書上P144,4-11定義并實現(xiàn)一個矩形類,有長、寬兩個屬性,由成員函數(shù)計算矩形的面積。設(shè)計思路:通過設(shè)計類,實現(xiàn)矩形的計算函數(shù)程序代碼:#includeusing namespace std;class Rectanglepubli
9、c:Rectangle(); float area(); void show();private:float a;float b;Rectangle:Rectangle() do coutplease input two numbers :ab;while(a= 0 | b= 0); float Rectangle:area()return a*b;void Rectangle:show()couta=a,b=b,area=area()endl;int main()Rectangle c;c.show();return 0;運行結(jié)果:結(jié)論: 要理解類的含義,理解每個定義的作用!三內(nèi)容:書上P1
10、86 5-7定義一個Cat類,擁有靜態(tài)數(shù)據(jù)成員numOfCats,記錄cat的個體數(shù)目,靜態(tài)成員函數(shù)getNumOfCats(),讀取numOfCats。設(shè)計程序測試這個類,體會靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的用法!設(shè)計思路:定義一個cat類,通過構(gòu)造函數(shù),并且聲明靜態(tài)數(shù)據(jù)成員。代碼:#include #include using namespace std;class Catpublic: Cat()+numOfCats; Cat(const Cat& cat)+numOfCats; virtual Cat()-numOfCats; static int getNumOfCats()return
11、 numOfCats;private: static int numOfCats;int Cat:numOfCats=0;int main() Cat a; Cat b; coutnumOfCats:Cat:getNumOfCats()endl; Cat c(a); Cat* p=new Cat(); coutnumOfCats:Cat:getNumOfCats()endl; delete p; coutnumOfCats:Cat:getNumOfCats()endl; return 0;運行結(jié)果:結(jié)論:這部分與類密切聯(lián)系,所以要掌握類,并且理解靜態(tài)數(shù)據(jù)成員的使用.內(nèi)容:書上P186 5-14
12、定義Boat類和Car兩個類,二者都有weight屬性,定義二者的友元函數(shù)getTotalWeight(),計算二者重量之和!設(shè)計思路:定義兩個類,使其為友元函數(shù),在其基礎(chǔ)上進行所需的運算。代碼:#includeusing namespace std;class Car;class Boatprivate: int Boatweight;public: Boat() Boatweight=450; friend int totalWeight(Boat &,Car &);class Carprivate: int Carweight;public: Car( ) Carweight=450;
13、friend int totalWeight(Boat &,Car &);int totalWeight(Boat &x,Car &y) return x.Boatweight+y.Carweight;int main() Boat a; Car b; cout這兩者的總重量為totalWeight(a,b)endl; return 0;運行結(jié)果: 結(jié)論:在理解類的情況下,可以用友元函數(shù)。在編寫程序時,可以有效的減少程序的冗長。四內(nèi)容:書上P248 已知有一個數(shù)組名叫oneArray,用一條語句求出其元素的個數(shù)。 設(shè)計思路:利用sizeof函數(shù)代碼:#include using namespa
14、ce std;int main() int array = 8, 4, 3, 4, 5,7,9,10; int i = sizeof(array)/sizeof(int); cout i endl; return 0;結(jié)果:結(jié)論:掌握基本函數(shù)的含義和用法內(nèi)容:書上P249 6-20實現(xiàn)一個名為SimpleCircle的簡單圓類。其數(shù)據(jù)成員int*itsRadius為一個指向其半徑的指針,存放其半徑值。設(shè)計數(shù)據(jù)成員的各種操作,給出這個類的完整實現(xiàn)并測試這個類。設(shè)計思路:利用類與友元函數(shù)代碼:#include using namespace std;class SimpleCirclepublic
15、:SimpleCircle();SimpleCircle(int);SimpleCircle(const SimpleCircle &);SimpleCircle() void SetRadius(int);int GetRadius()const; private:int *itsRadius; SimpleCircle:SimpleCircle()itsRadius = new int(5); SimpleCircle:SimpleCircle(int radius)itsRadius = new int(radius); SimpleCircle:SimpleCircle(const S
16、impleCircle & rhs)int val = rhs.GetRadius();itsRadius = new int(val); int SimpleCircle:GetRadius() constreturn *itsRadius;int main()SimpleCircle CircleOne, CircleTwo(9);cout CircleOne: CircleOne.GetRadius() endl;cout CircleTwo: CircleTwo.GetRadius() endl;return 0;結(jié)果:結(jié)論:要充分理解類的含義與用法,理解友元的用法 五內(nèi)容:書上P25
17、0 6-22編寫函數(shù)void reverse(string &s),用遞歸算法是字符串倒置。算法設(shè)計:利用遞歸算法設(shè)計倒敘代碼:#include#includeusing namespace std;void reverse (char *s)char t;static int i=0;t = *(s+strlen(s)-i-1);*(s+strlen(s)-i-1) = *(s+i);*(s+i) = t;i+;if(strlen(s)/2=i);else reverse (s);void main()char s100;cout請輸入字符串:s;reverse(s);cout倒序后的字符串
18、:endl;coutsendl;結(jié)果: 結(jié)論:要熟練掌握遞歸方法內(nèi)容:書上P250 6-27定義一個Employee類,其中包括姓名、地址、城市和郵編等屬性,包括setname(),display(),等函數(shù)。Display()使用cout語句顯示姓名、地址、城市和郵編等屬性,函數(shù)setname()改變對象的姓名屬性,實現(xiàn)并測試這個類。算法設(shè)計:利用類來設(shè)計這個程序代碼:#include using namespace std;class Employee private: char *name,*address,*city,*postCode;public: Employee(char *_
19、name,char *_address,char *_city,char *_postCode) name = _name; address = _address; city = _city; postCode = _postCode; void setName(char *_name) name = _name; void display() coutname : nameendl; coutaddress : addressendl; coutcity : cityendl; coutpostcode : postCodedisplay(); e-setName(李四); e-displa
20、y(); 結(jié)果:結(jié)論:熟練掌握類的方法,用類來表示需要表示的東西。 六題目:定義一個基類sharp,在此基礎(chǔ)上派生出rectangle和circle,兩者都有g(shù)etarea()函數(shù)計算面積。程序代碼:#include #include #define pi 3.14using namespace std;class shape public: virtual float area()=0;class circle:public shape public: circle(float r1) r=r1; private: float r; float area() return (float)pi
21、*r*r; ;class rectangle:public shape public: rectangle(float w1,float h1) width=w1;height=h1; private: float width,height; float area() return width*height; ;class square : public rectangle public: square(float len):rectangle(len,len); square(); float area(float len) return len * len; ;int main() sha
22、pe* s2; int m,a,b; cout輸入圓半徑:m; s0=new circle(m); coutarea()endl; cout輸入長和寬:ab; s1=new rectangle(a,b); coutarea()endl; s 2 = new square( a ); cout area() endl; return 0;:運行結(jié)果:結(jié)論:在派生類的基礎(chǔ)上在派生,相當(dāng)于把派生看作是基類!題目:定義一個Document類,有數(shù)據(jù)成員name,從document派生出book類,增加數(shù)據(jù)pagecount。程序代碼:#include#includeusing namespace st
23、d;class Document public:Document(string Name) name=Name;void display() /coutname=nameendl;private:string name; ;class Book:public Document public:Book(string nam,int page):Document(nam) pageCount=page; void show() /顯示Book類數(shù)據(jù)的函數(shù)coutpageCount=pageCountendl;private:int pageCount; /該類有數(shù)據(jù)成員pageCount;int
24、main() Book a(李四,50); a.display(); /顯示數(shù)據(jù)name a.show(); /顯示數(shù)據(jù)pageCountreturn 0;運行結(jié)果:結(jié)論:掌握類的含義,在類的基礎(chǔ)上派生,直接輸出的!可以轉(zhuǎn)換為輸入輸出!七題目:7-10一、內(nèi)容:定義一個object類,有數(shù)據(jù)成員weight及相應(yīng)的操作函數(shù),由此派生出box類,增加數(shù)據(jù)成員height和width及相應(yīng)的操作函數(shù),聲明一個box類對象,觀察構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序。二、設(shè)計思路:利用類的派生思想三、程序代碼:#includeusing namespace std;class Objectpublic:Obj
25、ect()coutcall the Objects constructorendl;void set_weight(int neww=0,int newn=0)weight=neww;num=newn;void total_weight()coutthe total weight is weight*numendl;int getw()return weight;Object()coutcall the Objects desconstructorendl;private:int weight,num;class Box:public Objectpublic:Box() coutcall t
26、he Boxs constructorendl;void set_digital(int h=0,int w=0,int l=0)height=h;width=w;length=l;void showBox()coutBoxs height:heightendl;coutBoxs width:widthendl; coutBoxs length:lengthendl;coutboxs weightgetw()endl;Box()coutcall the Boxs desconstructorendl;private:int height,width,length;void main()Box
27、a;a.set_weight(4,5);a.total_weight();a.set_digital(1,2,3);a.showBox();四、運行結(jié)果:題目:711一、內(nèi)容:定義一個基類baseclass,從它派生出類derivedclass。Baseclass有成員函數(shù)fn1,fn2,derivedclass也有成員函數(shù)fn1,fn2。在主函數(shù)中聲明一個derivedclass的對象,分別用derivedclass的對象以及baseclass和derivedclass的指針來調(diào)用fn1,fn2,觀察運行結(jié)果。 程序代碼:#includeusing namespace std;class B
28、aseClasspublic:void fun1()cout1endl;void fun2()cout2endl;class DerivedClass:public BaseClasspublic:void fun1()cout3endl;void fun2()cout4endl;void main()DerivedClass a;cout通過DerivedClass的對象調(diào)用函數(shù)endl;a.BaseClass:fun1(); a.BaseClass:fun2(); a.fun1(); a.fun2(); BaseClass &b=a; cout通過BaseClass的指針調(diào)用函數(shù)endl;
29、 b.fun1(); b.fun2(); DerivedClass &t=a; cout通過DerivedClass的指針調(diào)用函數(shù)endl; t.BaseClass:fun1(); t.BaseClass:fun2(); t.fun1(); t.fun2();運行結(jié)果: 八一、內(nèi)容:使用I/O流以文本方式建立一個文件test1.txt,寫入字符“已成功寫入文件!”用其他字處理程序(例如windows的記事本程序Nodepad)打開,看看是否正確寫入。使用I/O流以文本方式打開11-3題建立的文本text1.txt讀出其內(nèi)容并顯示出來,看看是否正確。二、設(shè)計思路:利用輸出流對象ofstream和
30、輸入流對象ifstream實現(xiàn)對文件的操作。代碼:#include#include #include using namespace std;void main() ofstream ofile(test1.txt); string s=已成功寫入文件;couts s.length(); ofile.write(char*)&s,s.length(); ofile.close(); ifstream tfile(test1.txt);coutndispaly filen;string s2; if(tfile) tfile.read(char*)&s2,s.length(); cout s2e
31、ndl; else cout ERROR: Cannot open file payroll. endl; tfile.close();結(jié)果: 題目11-6一、內(nèi)容:定義一個dog類,包涵體重和年齡兩個成員函數(shù),聲明一個實例dog1,體重為5,年齡為10,使用I/O流把dog1的狀態(tài)寫入磁盤文件,再聲明另一個dog2,通過讀文件把dog1的狀態(tài)賦給dog2。使用二進制方式操作文件,看看結(jié)果有何不同;再看看磁盤文件的ASCII碼有何不同。二、設(shè)計思路:三、程序代碼:#include#include using namespace std;class dogpublic:dog(int weigh
32、t, long days):itsWeight(weight),itsNumberDaysAlive(days)dog()int GetWeight()const return itsWeight; void SetWeight(int weight) itsWeight = weight; long GetDaysAlive()const return itsNumberDaysAlive; void SetDaysAlive(long days) itsNumberDaysAlive = days; private:int itsWeight;long itsNumberDaysAlive
33、;int main() / returns 1 on errorchar fileName80;cout fileName;ofstream fout(fileName);/ ofstream fout(fileName,ios:binary);if (!fout)cout Unable to open fileName for writing.n;return(1);dog Dog1(5,10);fout.write(char*) &Dog1,sizeof Dog1);fout.close();ifstream fin(fileName);/ ifstream fin(fileName,io
34、s:binary);if (!fin)cout Unable to open fileName for reading.n;return(1);dog Dog2(2,2);cout Dog2 weight: Dog2.GetWeight() endl;cout Dog2 days: Dog2.GetDaysAlive() endl;fin.read(char*) &Dog2, sizeof Dog2);cout Dog2 weight: Dog2.GetWeight() endl;cout Dog2 days: Dog2.GetDaysAlive() endl;fin.close();retu
35、rn 0;四、運行結(jié)果:九內(nèi)容: 分別用函數(shù)重載,函數(shù)的缺省參數(shù)及模板函數(shù)完成兩個或三個(n個)數(shù)相加。設(shè)計思路:定義不同的函數(shù)讓兩個數(shù)相加,然后讓其根據(jù)數(shù)據(jù)類型相加。 設(shè)計相加的函數(shù),然后用主函數(shù)調(diào)用函數(shù),運行結(jié)果。程序代碼:函數(shù)重載:#includeusing namespace std;int sum(int m,int n)return m+n;double sum(double m,double n)return m+n;int main()int m,n;coutmn;coutTheir sum:sum(m,n)endl;double x,y;coutxy;coutTheir su
36、m:sum(x,y)endl;return 0;缺省參數(shù):#includeusing namespace std;int Sum(int a,int b=5,int c=2) return a+b+c;int main()const x=8,y=1,z=9;coutSum(x,y,z)endl;coutSum(x,y)endl;coutSum(x)endl;return 0;函數(shù)模板:#includeusing namespace std;int Sum(int a,int b/*=4*/,int c/*=8*/)return a+b+c;templateT sum(T x,T y)retur
37、n x+y;int main()int a=4,b=9;double c=3.4,d=2.6;coutTheir sum:sum(a,b)endl;coutTheir sum:sum(c,d)endl;return 0;運行結(jié)果:結(jié)果一:結(jié)果二:結(jié)果三:結(jié)論:個人覺得使用函數(shù)重載比較好,因為其易明白, 容易操作。 十內(nèi)容:用類完成一個時鐘,要求做到使用構(gòu)造函數(shù) 使用析構(gòu)函數(shù) 十二時制,二十四時制 顯示時間 輸入走過的時間,在輸出走過之后顯示的時間設(shè)計思路:設(shè)置時鐘,構(gòu)造函數(shù),通過數(shù)字大小判定是否是AM還是PM,看分,秒是否大于60,注意加一。代碼:#includeusing namespace
38、 std;class Clockpublic:Clock(int newH,int newM,int newS);Clock(Clock &a);void setTime(int newH,int newM,int newS);void showTime();void run(int newH,int newM,int newS,int k);private:int hour,minute,second;Clock:Clock(int newH,int newM,int newS)hour=newH;minute=newM;second=newS;void Clock:setTime(int
39、newH,int newM,int newS)hour=newH;minute=newM;second=newS;inline void Clock:showTime()if(hour=12)&(hour=23)coutPMhour-12:minute:second=0)|(hour12)coutAMhour:minute:second60) second=newS-60;newM=newM+1;else second=newS;if(newM60) minute=newM-60;newH=newH+1;else minute=newM;int main() int h,m,s,k;coutp
40、lease enter time:endl;couth m shms;coutkk;Clock a(0,0,0);a.showTime(); a.setTime(h,m,s);a.showTime();a.run(h,m,s,k);a.showTime();return 0;運行結(jié)果:結(jié)論:時鐘函數(shù)需要自己掌握構(gòu)造,調(diào)用函數(shù)等,要認(rèn)真,不放棄,就會編出自己的程序十一內(nèi)容: 求解一元二次方程設(shè)計思路:通過類來設(shè)計程序。 程序代碼: #include#includeusing namespace std;class funtionpublic: void make();void display()
41、; void answer();private: float a,b,c;float x1,x2; float r,i,f;void funtion:make () cout輸入 a b c 的值a; cinb; cinc;void funtion:display ()f=b*b-4*a*c; if(f0) x1=-b/(2*a)+sqrt(f)/(2*a); x2=-b/(2*a)-sqrt(f)/(2*a); else if(f=0) x1=-b/(2*a); x2=-b/(2*a); else r=-b/(2*a); i=sqrt(-f)/(2*a); void funtion:answer() if(f0) coutx1=x1 x2=x2endl; else if(f=0
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 七年級英語下冊 Unit 6 I'm watching TV Section B第4課時(2a-2c)教學(xué)設(shè)計 (新版)人教新目標(biāo)版
- 10 在牛肚子里旅行(教學(xué)設(shè)計)2024-2025學(xué)年統(tǒng)編版三年級語文上冊
- 2024-2025學(xué)年高中歷史 第二單元 凡爾賽-華盛頓體系下的短暫和平 第6課 國際聯(lián)盟教學(xué)教學(xué)設(shè)計 岳麓版選修3
- Unit 1 Home-Reading(教學(xué)設(shè)計)2024-2025學(xué)年譯林版(2024)英語七年級下冊
- 裝飾施工現(xiàn)場安全用電
- 2024秋九年級語文上冊 第1單元 3我愛這土地教學(xué)設(shè)計 新人教版
- 管理學(xué)矩陣結(jié)構(gòu)
- 《煎荷包蛋》(教案)-2023-2024學(xué)年四年級下冊勞動人教版
- 一年級道德與法治下冊 第四單元 我們在一起 14 請幫我一下吧教學(xué)設(shè)計2 新人教版
- 2024-2025學(xué)年高中生物 第二章 基因和染色體的關(guān)系 第1節(jié) 減數(shù)分裂和受精作用 一 減數(shù)分裂教學(xué)設(shè)計3 新人教版必修2
- (2024年更新)國家慢性疾病編碼新目錄
- 治療室物品分類擺放
- 一次性使用醫(yī)療用品管理制度
- 獸醫(yī)屠宰衛(wèi)生人員考試題庫及答案(415題)
- 商務(wù)預(yù)算員培訓(xùn)課件
- 物業(yè)合同增加人員補充協(xié)議書(2篇)
- 房屋中介公司員工管理規(guī)章制度
- 餐飲服務(wù)電子教案 學(xué)習(xí)任務(wù)3 餐巾折花技能(4)-餐巾折花綜合實訓(xùn)
- 先天性唇腭裂與顱面裂的診斷及治療(口腔頜面外科學(xué)課件)
- 醉里乾坤大壺中日月長-初中語文九年級第六單元名著導(dǎo)讀《水滸傳》整本書閱讀精讀研討課 公開課一等獎創(chuàng)新教學(xué)設(shè)計
- TCECA-G 0310-2024 離網(wǎng)制氫靈活消納與柔性化工系統(tǒng)開發(fā)規(guī)范
評論
0/150
提交評論