復習課件及試題要點_第1頁
復習課件及試題要點_第2頁
復習課件及試題要點_第3頁
復習課件及試題要點_第4頁
復習課件及試題要點_第5頁
免費預覽已結束,剩余16頁可下載查看

下載本文檔

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

文檔簡介

1、一、工具與程序框架一、工具與程序框架1.使用 VC+6.0 開發的步驟:建立控制臺工程建立 C+源程序文件 (3)輸入以下框架:#include #include #include #include#includeusing namespa main()td;/ 你的代碼寫在此.return 0;編譯運行2.C+基本要素(1)標識符以字母、下劃線、$符開始的字符串,可以表示:變量、類型、函數、類等的名字注釋/ 一行注釋/*多行注釋 1多行注釋 2.*/程序基本流程: 輸入:cinxy;計算:z=x+y;輸出:coutz=z(2)(3)using namespatd; main()double

2、a, b, c; coutab;c = a+b;two numbers: ;cout a + b = c二、基本數據類型、變量、運算符、表達式基本數據類型short 16 位整數32 位整數float 32 位浮點數double 64 位浮點數char 8 位字符bool變量定義 x; x=3;double d=3.41;char boolboolc=a; b=true;b=false;轉義字符n 換行, r 回車,t,0 字符串結尾符算術運算符+、-、*、/、%、+、-關系運算符、=、=、=、 11.典型例子制表,反斜線, 單引號,雙引號,例 2-4/ Ex #include溫度轉換:輸入一

3、個華氏溫度,計算并輸出對應的攝氏溫度值。le 2-4:溫度轉換using namespatd; main()double c, f;coutf;c=5.0/9.0 * (f-32);cout對應于華氏溫度f的攝氏溫度為cxy;計算:z=x+y;輸出:coutz=z0)y=1;elsey=-1;if-else if-else if(x0)y=1;else if(y0)y=-1;elsey=0;多分支結構switch-case-default switch(x)case 0:/. break;case 1:/. break;default:/. break;while 循環結構while(條件)/

4、 代碼do-while 循環結構do/ 代碼while(條件); 7.for 循環結構for(初值;條件;步長)/ 代碼while(條件);break、continue 結構異常處理trycest;if(test!=0)throw test;/拋出異常 elsethrow it is a zero;catch(i)coutExcept occurred: iendl;catch(const char *s)coutExcept occurred: sendl;try.catch(.)coutExcept occurred: endl; try.catch(Exception e)coutExc

5、ept occurred: endl; 10.典型例子例 3-1編程實現分段函數/ Exle 3-1:分段函數 #include using namespatd; main( )double x, y;coutx;if(x0)y=x+1;coutx=x,y=x+1=yendl;/ 0 x 1else if(x1)y=1;coutx=x,y=yendl;else/ 1 xy=;coutx=x,y=yendl;return 0;例 3-3計算自然對數的底數e,當通項10 的-7 時停止計算。/ Exle 3-3:計算常數 e 的值#include using namespamain()td;dou

6、ble e = 1.0;double u = 1.0; n = 1;while(u = 1.0E-7)u = u/n; e = e+u; n = n+1;cout e = e ( n = n ) ai; coutaij;coutai;cina; coutai; couta;典型例子例 4-1給一維數組輸入 7 個整數,找出該數組中的最大數。/ Exle 4-1:求數組中的最大元素 #include using namespamain()a7;td;coutPlease input an array with seven elements: endl;/ 輸入每個數組元素的值for(i=0; i

7、ai;big = a0;/ 遍歷整個數組for(j=0; jbig)big = aj;coutmax=bigendl; return 0;例 4-6編寫一個用于對整型數組進行排序的程序,排序方法使用簡單的交換排序法。/ Exle 4-6:交換排序 #include using namespamain()constlist=td;COUNT=16;503, 87, 512, 61, 908, 170, 897, 275,653, 426, 154, 509, 612, 677, 765, 703;for(i=0; ii; j-)if(listj-1listj)tmp = listj-1; lis

8、tj-1 = listj;listj = tmp;cout The result is : endl;/ 輸出排序后的數組for(k=0; k16; k+) cout listk ;coutendl;return 0;例 4-9/ Ex字符串連接。le 4-9:連接兩個字符串#include #include using namespamain()td;char destination81 = abcdefghijklmnopqrstuvwxyz;char source = ABCDEFGHIJKLMNOPQRSTUVWXYZ;i = strlen(destination); j = 0;wh

9、ile(sourcej!=0)destinationi+ = sourcej+; destinationi = 0;coutThe result is: destination0)str1+; str2+; n-;return toupper(*str1)- toupper(*str2);例 6-8使用指針編寫一個用于對整型序列進行排序的函數,排序方法使用簡單選擇排序法。/ Exle 6-8:簡單選擇排序#include using namespatd;/ 函數 selectsort ():簡單選擇排序void selectsort (*list,count)for(i=0; icount-1

10、; i+)k=i;for(j=i+1; j count; j+)if(*(list+j) *(list+k) k=j; if(k!=i)tmp = *(list+i);*(list+i) = *(list+k);*(list+k)= tmp;main()array 6=2, 7, 2, 2, 3, 1;selectsort (array, 6);cout The result is : endl;for(i=0;i6;i+)cout array i ;coutendl;return 0;六、函數、數組與函數、與函數、指針與函數、函數指針六、函數、數組與函數、與函數、指針與函數、函數指針1.函數

11、定義格式:add1(a,b)return a+b;add2(&return a+b;a,&b)add3(*a,*b)return *a+*b;add4(count,a)c=0;for(i=0;icount;i+)c=c+a;return c;調用格式:a=3; b=5;c=add1(a,b);c=add2(a,b); c=add3(&a,&b);iny (*ptr)(,)=add1; c=ptr(a,b);d=1,2,3,4;c=add4(4,d); 2.典型例子例 5-5 編寫一個用于對整型數組進行排序的函數,排序方法使用例 4-6 的簡單交換排序法。le 5-5: 交換排序/ Ex#inc

12、lude using namespatd;/函數 bubble_up(): 冒泡法排序void bubble_up(list,count)for(i=0; ii; j=j-1) if(listj-1listj)tmp = listj-1; listj-1 = listj;listj = tmp;/測試冒泡法排序的主程序 main()i;array16=503, 87, 512, 61, 908, 170, 897, 275,653, 426, 154, 509, 612, 677, 765, 703;cout 原數組是: endl;for(i=0; i16; i+)cout array i ;

13、 coutendl;bubble_up(array, 5);/ 函數調用cout 對數組前 5 項進行排序后的結果是: endl; for(i=0; i16; i+)cout array i ; coutendl;bubble_up(array, 16);cout 對整個數組排序后的結果是: endl; for(i=0; i16; i+)cout array i ; coutendl;return 0;例 7-1采用遞歸算法求 n!/ Exle 7-1:用遞歸方法求 n!/ 函數 fac():求階乘的遞歸函數fac(n)if(n0)return ?1;else if(n=0) return 1

14、;/ 不能求負數的階乘/ 0 的階乘為 1elsereturn n*fac(n-1); / n!為(n-1)!乘以 n例 7-11 編寫一個用于在字符串中查找某字符的函數。/ Exle 7-11:函數 strchr(): 在字符串中查找指定字符char *strchr(char *string,c)while(*string!=c & *string!=0)string+; if(*string=c)return string;/ 查找成功/string=0(字符串結束符), 字符串中沒有celsereturn NULL;七、結構體、枚舉體、七、結構體、枚舉體、1.結構體定義格式:struct

15、 Dateyear;使用格式:month;day;Date date;date.year=2012; 2.枚舉體定義格式:enum ColorRED,GREEN,BLUE;使用格式:Color color; color=RED;3.定義格式:unionition使用格式:grand;char title20;ition;.grand=3;strcpy(4.典型例子.title,Engineer);例 8-7詞頻統計:輸入一系列英文單詞,單詞之間用空格隔開,用“xyz”表示結束輸入,統計輸入過哪些單詞以及各單詞出現的次數,統計時區分大小寫字母,最后按單詞的字典順序輸出單詞和出現次數的對照表。le

16、 8-7: 詞頻統計/ Ex#include #include using namespa/ 詞條類型struct WordListtd;char word50;freq;/ 詞典排序函數void Sort(WordList list,count)for(i=0; ii; j=j-1)if(strcmp(listj-1.word,listj.word)0)WordList tmp; tmp=listj-1;listj-1=listj; listj=tmp;/ 主函數:進行詞頻統計main()WordList list5000;i, num=0; char temp50;cout請輸入一系列英語

17、單詞,以 xyz 表示輸入結束temp;while(strcmp(temp, xyz)!=0)掃描當前詞典for(i=0; i=num)strcpy(listi.word, temp); listi.freq =1;num+;cintemp;/ 若詞典中存在該詞條,詞頻加 1/ 若詞典中無該詞條,添加該詞/ 繼續輸入單詞Sort(list, num);/ 輸出詞典cout詞頻統計結果如下:endl; for(i=0; inum; i+)coutlisti.wordtlisti.freqendl;return 0;/ 對詞典進行排序八、類、繼承與對象八、類、繼承與對象1. 類:定義格式:clas

18、s A:基類權限修飾符:成員變量定義權限修飾符:構造函數定義virtual 虛函數定義virtual 純虛函數定義=0;一般成員函數定義析構函數定義;2. 對象:使用格式:A a;a.成員函數調用3.典型例子 例 10-4為類增加構造函數和析構函數。/ Exle 10-4:為類#include #include 增加構造函數和析構函數using namespaclasstd;charName20;Age;charSex; public:()/構造函數strcpy(Name, Age = 0; Sex = m;);void void/析構函數()coutNow destroying the in

19、stance ofRegister(char *name,age, char sex); ShowMe();endl;void: Register(char *name,strcpy(Name, name); Age = age;Sex = (sex = m?m:f);age, char sex)void: ShowMe()cout Name t Age t Sex endl;main()2; /對象調用構造函數1,1: t;cout 1.ShowMe();1.Register(Zhang3, 19, m);cout 1: t;1.ShowMe();cout 2: t;2.ShowMe();/

20、對象之間的賦值2 =cout 1;2: t;2.ShowMe();return 0;例 11-1演示公有繼承中派生類對基類成員的。le 11-1 公有繼承中派生類對基類成員的/Ex#include#includeusing namespaclasstd;char Name20;char Sex;Age;public:void Register(char *name,age, char sex)strcpy(Name, name);Age = age;Sex = (sex = m?m:f);void ShowMe() cout Name t Age t Sex endl;class Studen

21、t : public/公有繼承Number;char Clapublic:ame10;void RegisterStu(char *claame,number, char *name,age, char sex)strcpy(Claame, claame);Number = number;Register(name, age, sex);/派生類成員函數直接使用基類的公有成員void ShowStu()cout Number t ClaShowMe();ame t;/直接使用基類的公有成員;main()Student stu;stu.RegisterStu(計算機 51,85071011, s

22、tu.ShowStu();,18,m);/派生類對象直接使用基類的公有成員stu.ShowMe();return 0;例 11-5從 Po類繼承的 Circle 類/ Po.h 文件 Po類的#ifndef PO#define PO class Po_H_H/點的 x 和 y 坐標public:x, y;/ 構造函數/ 設置坐標/ 取 x 坐標/ 取 y 坐標/輸出點的坐標Po(= 0,= 0 );void SetPo(,);GetX() return x; GetY() return y; void Pr();#endif/ Po.cpp 文件 Po類的成員函數定義#include usin

23、g namespa#include po.htd;Po:Po(a,b ) SetPo( a, b ); void Po:SetPo(a,b )x = a;y = b;void Po:Pr() cout x , y ; / Circle.h 文件 Circle 類的#ifndef CIRCLE_H #define CIRCLE_H #include using namespa#include po.htd;class Circle : public Podouble radius;public:Circle(x = 0,y = 0 ,double r = 0.0);void SetRadius(

24、 double );/設置半徑/取半徑/計算面積/輸出圓心坐標和半徑double GetRadius();double Area(); void Pr();#endif/ Circle.cpp 文件 Circle 類的成員函數定義#include using namespa#include circle.htd;Circle:Circle(a,b,double r): Po(a,b) SetRadius( r ); void Circle:SetRadius( double r ) radius = ( r = 0 ? r : 0 );double Circle:GetRadius() ret

25、urn radius; double Circle:Area() return 3.14159 * radius * radius; void Circle:Pr()cout Center = ;Po:Pr();cout ; Radius = radius endl;/ Exle11-5.cpp 文件: Circle Demo #include using namespa#include po.h #include circle.hmain()td;Pop(30,50);Circle c(120,80,10.0);cout Pop.Pr();p: ;cout nCircle c: ;c.Pr(

26、);cout The centre of circle c: ; c.Po:Pr();cout nThe area of circle c: c.Area() endl;return 0;九、文件與輸入輸出九、文件與輸入輸出1.文本文件讀#include #include using namespatd;ifstream in(a.txt);if(!in)/退出char a20; in a;coutaendl; in.close();2.文本文件循環讀#include using namespatd;ifstream in(q.txt);if(!in)/退出while(in)in if(!in

27、)/退出循環in.close();3.文本文件寫 #include using namespatd;ofstream out(a.txt);if(!out)/ 退出out Ok! endl;olose();4.文本文件循環寫#include using namespatd;ofstream out(學生成績.txt); if(!out)/退出while(.).out olose();5.格式化輸入#include using namespatd;ch=cin.get(); 輸入一個或多個字節cin.get(char &ch);cin.get(char *buf,size,char delim=

28、n );cin.getline(char *buf,6.格式化輸出#include size,char delim=n )using namespatd;cout hex十六進制 cout dec十進制 cout oct八進制cout.setf(ios:scientific);科學記數法 cout.setf(ios:fixed);固定小數位記數法 cout.setf(ios:left); 左對齊cout.setf(ios: right);右對齊(4);精度cout.precicout.width(10);寬度cout.fill(*);填充符十、模板、十、模板、 1.模板函數定義格式template T tmax(T a, T b)/求兩個數據最大值的函數模板return ab?a:b;使用格式m1=5, m2=3;double d1=12.5, d2=6.4; char c1=a, c2=b;coutm1和m2中的最大值是: tmax(m1, m2) endl; coutd1和d2中的最大值是: tmax(d1, d2) endl; coutc1和c

溫馨提示

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

評論

0/150

提交評論