收藏 分享(赏)

高质量C编程指南.pdf

上传人:苏醒文档集 文档编号:13151364 上传时间:2022-07-01 格式:PDF 页数:101 大小:326.13KB
下载 相关 举报
高质量C编程指南.pdf_第1页
第1页 / 共101页
高质量C编程指南.pdf_第2页
第2页 / 共101页
高质量C编程指南.pdf_第3页
第3页 / 共101页
亲,该文档总共101页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、 C+/C 1.0 2001 7 24 C+/C v 1.0 2001 Page 2 of 101 / V 0.9 2001-7-1 2001-7-18 V 1.0 2001-7-18 2001-7-24 V 0.9 C+/C v 1.0 2001 Page 3 of 101 .6 1 .8 1.1 .8 1.2 .8 1.3 .8 1.4 .8 1.5 .8 2 .8 2.1 .8 2.2 .8 2.3 .8 2.4 .8 2.5 .8 2.6 .8 2.7 .8 2.8 .8 3 .8 3.1 .8 3.2 WINDOWS .8 3.3 UNIX .8 4 .8 4.1 .8 4.2 .8

2、 4.3 IF .8 4.4 .8 4.5 FOR .8 4.6 SWITCH .8 4.7 GOTO .8 5 .8 5.1 .8 5.2 CONST #DEFINE .8 5.3 .8 5.4 .8 6 .8 C+/C v 1.0 2001 Page 4 of 101 6.1 .8 6.2 .8 6.3 .8 6.4 .8 6.5 .8 6.6 .8 7 .8 7.1 .8 7.2 .8 7.3 .8 7.4 .8 7.5 FREE DELETE .8 7.6 .8 7.7 .8 7.8 MALLOC/FREE NEW/DELETE .8 7.9 .8 7.10 MALLOC/FREE .

3、8 7.11 NEW/DELETE .8 7.12 .8 8 C+ .8 8.1 .8 8.2 .8 8.3 .8 8.4 .8 8.5 .8 8.6 .8 9 .8 9.1 .8 9.2 .8 9.3 .8 9.4 STRING .8 9.5 .8 9.6 STRING .8 9.7 .8 9.8 .8 9.9 .8 10 .8 C+/C v 1.0 2001 Page 5 of 101 10.1 .8 10.2 .8 11 .8 11.1 CONST .8 11.2 .8 11.3 .8 .8 A C+/C .8 B C+/C .8 C C+/C .8 C+/C v 1.0 2001 Pa

4、ge 6 of 101 C+/C N-1 Internet (1) (2) (3) (4) (5) (6) (7) (8) (9) 30 (10) 9:00 5:00 9:00 C+/C CAD 3D C+/C v 1.0 2001 Page 7 of 101 1995 2000 10 C+ 3D 3D strcpy 1 2 3 IT IT C+/C v 1.0 2001 Page 8 of 101 1 2 10 if 14 C+/C IT 1 2 1 2 3 C+/C v 1.0 2001 Page 9 of 101 B C+/C 1 2 3 85 C+/C 4 C+/C Bug CMMI

5、CMMI C+/C v 1.0 2001 Page 10 of 101 C+/C 1 2 3 2002 7 CMMI 3 1000 2001 7 C+/C v 1.0 2001 Page 11 of 101 1 C+/C declaration implementation definition C+/C .h C .c C+ .cpp .cc .cxx 1.1 1-1 1 2 3 / 4 /* * Copyright (c) 2001, * All rights reserved. * * filename.h * * * * 1.1 * * 2001 7 20 * * 1.0 * * 20

6、01 5 10 */ 1-1 C+/C v 1.0 2001 Page 12 of 101 1.2 1 1-1 2 3 graphics.h 1-2 1-2-1 ifndef/define/endif 1-2-2 #include 1-2-3 #include “filename.h” 1-2-1 C+ 1-2-2 extern int value / 1-1 #ifndef GRAPHICS_H / graphics.h #define GRAPHICS_H #include / #include “myheader.h” / void Function1( ); / class Box /

7、 ; #endif 1-2 C+/C C+/C v 1.0 2001 Page 13 of 101 1.3 1 1-1 2 3 graphics.cpp 1-3 / 1-1 #include “graphics.h” / / void Function1( ) / void Box:Draw( ) 1-3 C+/C 1.4 Basic Fortran C+/C 1 2 C+/C v 1.0 2001 Page 14 of 101 1.5 include source C+/C v 1.0 2001 Page 15 of 101 2 2.1 2-1-12-1 a 2-1-2 2-1 b / vo

8、id Function1( ) / void Function2( ) / void Function3( ) / while (condition) statement1; / if (condition) statement2; else statement3; / statement4; 2-1(a) 2-1(b) C+/C v 1.0 2001 Page 16 of 101 2.2 2-2-1 2-2-2 if for while do 2-2 a 2-2 b int width; / int height; / int depth; / int width, height, dept

9、h; / x = a + b; y = c + d; z = e + f; X a + b; y = c + d; z = e + f; if (width height) dosomething(); if (width height) dosomething(); for (initialization; condition; update) dosomething(); / other(); for (initialization; condition; update) dosomething(); other(); 2-2(a) 2-2(b) 2-2-1 int width = 10;

10、 / width int height = 10; / height int depth = 10; / depth C+/C v 1.0 2001 Page 17 of 101 2.3 2-3-1 const virtual inline case if for while 2-3-2 2-3-3 ; 2-3-4 Function(x, y, z) ; for (initialization; condition; update) 2-3-5 = += = = + * % & | 2-3-1 for if for (i=0; i10; i+) if (a=b) & (c= 2000) / i

11、f(year=2000) / if (a=b) & (c=b&c=d) / for (i=0; i10; i+) / for(i=0;i10;i+) / for (i = 0; I 10; i +) / x = a b ? a : b; / x=aFunction(); / b - Function(); 2-3 C+/C v 1.0 2001 Page 18 of 101 2.4 2-4-1 2-4-2 2-4 a 2-4 b void Function(int x) / program code void Function(int x) / program code if (conditi

12、on) / program code else / program code if (condition) / program code else / program code for (initialization; condition; update) / program code for (initialization; condition; update) / program code While (condition) / program code while (condition) / program code 2-4(a) 2-4(b) C+/C v 1.0 2001 Page

13、19 of 101 2.5 2-5-1 70 80 2-5-2 if (very_longer_variable1 = very_longer_variable12) & (very_longer_variable3 = very_longer_variable14) & (very_longer_variable5 = very_longer_variable16) dosomething(); virtual CMatrix CMultiplyMatrix (CMatrix leftMatrix, CMatrix rightMatrix); for (very_longer_initial

14、ization; very_longer_condition; very_longer_update) dosomething(); 2-5 2.6 * * int* x; x int int* x, y; y x y 2-6-1 * char *name; int *x, y; / y C+/C v 1.0 2001 Page 20 of 101 2.7 C /*/ C+ /*/ / 1 2 3 2-6 2-7-1 2-7-2 i+; / i 1 2-7-3 2-7-4 2-7-5 2-7-6 2-7-8 2-6 /* * * * * */ void Function(float x, fl

15、oat y, float z) if () while () / end of while / end of if C+/C v 1.0 2001 Page 21 of 101 2.8 public protected private 1 private public 8-3 a 2 public private 8.3 b C+ Biarne Stroustrup class A private: int i, j; float x, y; public: void Func1(void); void Func2(void); class A public: void Func1(void)

16、; void Func2(void); private: int i, j; float x, y; 8.3(a) 8.3(b) C+/C v 1.0 2001 Page 22 of 101 3 Microsoft ch p ppch int i, j, k; float x, y, z; int iI, iJ, ik; / i int float fX, fY, fZ; / f float 3.1 3.2 3-1-1 CurrentValue NowValue 3-1-2 min-length & max-information ANSI C 6 C+/C ! maxval maxValue

17、UntilOverflow i,j,k,m,n,x,y,z 3-1-3 Windows AddChild Unix add_child C+/C v 1.0 2001 Page 23 of 101 3-1-4 int x, X; / x X void foo(int x); / foo FOO void FOO(float x); 3-1-5 3-1-6 float value; float oldValue; float newValue; 3-1-7 DrawBox(); / box-Draw(); / 3-1-8 int minValue; int maxValue; int SetVa

18、lue(); int GetValue(); 3-1-1 Value1,Value2 3.2 Windows Windows C+/C v 1.0 2001 Page 24 of 101 3-2-1 class Node; / / class LeafNode; / void Draw(void); / void SetValue(int value); / 3-2-2 BOOL flag; int drawMode; 3-2-3 const int MAX = 100; const int MAX_LENGTH = 100; 3-2-4 s_ static void Init() stati

19、c int s_initValue; / 3-2-5 g_ global int g_howManyPeople; / int g_howMuchMoney; / 3-2-6 m_ member void Object:SetValue(int width, int height) m_width = width; m_height = height; C+/C v 1.0 2001 Page 25 of 101 3-2-7 OpenGL gl GL 3.3 Unix C+/C v 1.0 2001 Page 26 of 101 4 if for while goto switch C+/C

20、4.1 C+/C 4-1 + - * ( ) - . ! + - sizeof + - * & * / % + - = = != & | & | ?: = += -= *= /= %= &= = |= = 4-1 4-1-1 4-1 word = (high 8) | low C+/C v 1.0 2001 Page 27 of 101 if (a | b) & (a & c) 4.2 a = b = c = 0 1 2 4-2-1 i = a = b & c d & c + f = g + h ; / 4-2-2 d = (a = b + c) + r ; a d a = b + c; d

21、= a + r; 4-2-3 if (a b c) / a b c if (ab) & (bc) if ( (ab)c ) 4.3 if if C+/C if 4.3.1 4-3-1 TRUE FALSE 1 0 FALSE TRUE TRUE Visual C+ TRUE 1 Visual Basic TRUE -1 flag if if (flag) / flag C+/C v 1.0 2001 Page 28 of 101 if (!flag) / flag if (flag = TRUE) if (flag = 1 ) if (flag = FALSE) if (flag = 0) 4

22、.3.2 4-3-2 = = 0 value if if (value = 0) if (value != 0) if (value) / value if (!value) 4.3.3 4-3-3 = = float double = = = =-EPSINON) & (x=EPSINON) EPSINON 4.3.4 4-3-4 = = NULL NULL NULL 0 p if if (p = NULL) / p NULL p if (p != NULL) if (p = 0) / p if (p != 0) if (p) / p if (!p) 4.3.5 if C+/C v 1.0

23、2001 Page 29 of 101 if (NULL = p) if (p = NULL) if (p = NULL) p NULL if (p = NULL) if (NULL = p) NULL if/else/return if (condition) return x; return y; if (condition) return x; else return y; return (condition ? x : y); 4.4 C+/C for while do 4-4-1 CPU 4-4(b)4-4(a) for (row=0; row100; row+) for ( col

24、=0; col5; col+ ) sum = sum + arowcol; for (col=0; col5; col+ ) for (row=0; row100; row+) sum = sum + arowcol; 4-4(a) 4-4(b) 4-4-2 C+/C v 1.0 2001 Page 30 of 101 4-4(c) 4-4(d) N-1 N 4-4(d) N 4-4(c) for (i=0; iN; i+) if (condition) DoSomething(); else DoOtherthing(); if (condition) for (i=0; iN; i+) D

25、oSomething(); else for (i=0; iN; i+) DoOtherthing(); 4-4(c) 4-4(d) 4.5 for 4-5-1 for for 4-5-1 for 4-5(a) x 0 = x N N N 4-5(b) x 0 = x = N-1 N-1 N 4-5(a) for (int x=0; xN; x+) for (int x=0; x=N-1; x+) 4-5(a) 4-5(b) 4.6 switch if switch C+/C v 1.0 2001 Page 31 of 101 switch if if switch switch switch

26、 (variable) case value1 : break; case value2 : break; default : break; 4-6-1 case break 4-6-2 default default default : break; default 4.7 goto goto goto goto goto state; String s1, s2; / goto int sum = 0; / goto state: goto C+/C goto goto goto break ; goto error; C+/C v 1.0 2001 Page 32 of 101 erro

27、r: goto C+/C v 1.0 2001 Page 33 of 101 5 C #define C+ #define const const 5.1 1 2 3 5-1-1 #define MAX 100 /* C */ const int MAX = 100; / C+ const const float PI = 3.14159; / C+ const 5.2 const #define C+ const #define 1 const 2 const 5-2-1 C+ const const 5.3 5-3-1 C+/C v 1.0 2001 Page 34 of 101 5-3-

28、2 const float RADIUS = 100; const float DIAMETER = RADIUS * 2; 5.4 #define const const const const const SIZE class A const int SIZE = 100; / const int arraySIZE; / SIZE ; const class A A(int size); / const int SIZE ; ; A:A(int size) : SIZE(size) / A a(100); / a SIZE 100 A b(200); / b SIZE 200 const

29、 class A enum SIZE1 = 100, SIZE2 = 200; / C+/C v 1.0 2001 Page 35 of 101 int array1SIZE1; int array2SIZE2; ; PI=3.14159 C+/C v 1.0 2001 Page 36 of 101 6 C+/C C pass by value pass by pointer C+ pass by reference 6.6 6.1 6-1-1 void void SetValue(int width, int height); / void SetValue(int, int); / flo

30、at GetValue(void); / float GetValue(); / 6-1-2 StringCopy str1 str2 void StringCopy(char *str1, char *str2); str1 str2 strSource strDestination strSource strDestination void StringCopy(char *strSource, char *strDestination); char str20; StringCopy(str, Hello World ); / 6-1-3 const C+/C v 1.0 2001 Pa

31、ge 37 of 101 void StringCopy(char *strDestination const char *strSource); 6-1-4 const & 6-1-1 5 6-1-2 C printf int printf(const chat *format, argument ); 6.2 6-2-1 C void C+ C+C C+/ C void 6-2-2 C getchar char c; c = getchar(); if (c = EOF) getchar c char getchar char int int getchar(void); c char -

32、128 127 EOF char if getchar 6-2-3 return C getchar int C+/C v 1.0 2001 Page 38 of 101 getchar getchar EOF EOF 1 getchar int return getchar BOOL GetChar(char *c); gechar GetChar putchar(getchar(); getchar 6-2-1 strcpy char *strcpy(char *strDest const char *strSrc); strcpy strSrc strDest strDest char

33、str20; int length = strlen( strcpy(str, Hello World ) ); 6-2-2 class String / String & operate=(const String &other); / friend friend String operate+( const String &s1, const String &s2); private: char *m_data; String operate = String & String:operate=(const String &other) if (this = &other) return

34、*this; delete m_data; C+/C v 1.0 2001 Page 39 of 101 m_data = new charstrlen(other.data)+1; strcpy(m_data, other.data); return *this; / *this String return *this String a,b,c; a = b; / *this a = b = c; / *this String operate + String operate+(const String &s1, const String &s2) String temp; delete t

35、emp.data; / temp.data 0 temp.data = new charstrlen(s1.data) + strlen(s2.data) +1; strcpy(temp.data, s1.data); strcat(temp.data, s2.data); return temp; String temp temp c = a + b; a + b c 6.3 6-3-1 assert 6.5 C+/C v 1.0 2001 Page 40 of 101 6-3-2 return return return return 1 return char * Func(void)

36、char str = “hello world”; / str return str; / 2 3 return return String(s1 + s2); temp String temp(s1 + s2); return temp; temp temp temp return int(x + y); / int temp = x + y; return temp; int,float,double 6.4 6-4-1 6-4-2 50 6-4-3 C+/C v 1.0 2001 Page 41 of 101 C/C+ static static 6-4-4 6-4-5 6.5 Debu

37、g Release Debug Release assert Debug 6-5 assert assert void *memcpy(void *pvTo, const void *pvFrom, size_t size) assert(pvTo != NULL) & (pvFrom != NULL); / byte *pbTo = (byte *) pvTo; / pvTo byte *pbFrom = (byte *) pvFrom; / pvFrom while(size - 0 ) *pbTo + = *pbFrom + ; return pvTo; 6-5 assert Debug

38、 Release assert assert assert assert assert assert Maguire, p8-p30 C+/C v 1.0 2001 Page 42 of 101 6-5-1 6-5-2 6-5-1 6-5-2 6.6 C+ n m reference m referent int m; int &n = m; n m n m n m m n m 1 2 NULL NULL 3 k i k = j k j k 6 k i i 6 int i = 5; int j = 6; int &k = i; k = j; / k i 6; C+ Func1 x n x n,

39、 n 0 void Func1(int x) x = x + 10; C+/C v 1.0 2001 Page 43 of 101 int n = 0; Func1(n); cout n = n endl; / n = 0 Func2 x n n n 10 void Func2(int *x) (* x) = (* x) + 10; int n = 0; Func2(&n); cout n = n endl; / n = 10 Func3 x n x n x n n 10 void Func3(int &x) x = x + 10; int n = 0; Func3(n); cout n =

40、n endl; / n = 10 C+/C v 1.0 2001 Page 44 of 101 7 Bill Gates 640K ought to be enough for everybody Bill Gates 1981 7.1 1 static 2 3 malloc new free delete 7.2 NULL p assert(p!=NULL) malloc new if(p=NULL) if(p!=NULL) C+/C v 1.0 2001 Page 45 of 101 1 1 for malloc free new/delete 1 2 return 3 free dele

41、te NULL 7-2-1 malloc new NULL NULL 7-2-2 7-2-3 1 1 7-2-4 7-2-5 free delete NULL 7.3 C+/C C+/C v 1.0 2001 Page 46 of 101 7.3.1 7-3-1 a 6 hello0 a a0= X p world world0 p0= X char a = “hello”; a0 = X; cout a endl; char *p = “world”; / p p0 = X; / cout p endl; 7-3-1 7.3.2 7-3-2 a b b = a strcpy b a if(b

42、=a) strcmp p = a a p a p a malloc p strlen(a)+1 strcpy if(p=a) strcmp / char a = hello; char b10; strcpy(b, a); / b = a; if(strcmp(b, a) = 0) / if (b = a) / int len = strlen(a); char *p = (char *)malloc(sizeof(char)*(len+1); strcpy(p,a); / p = a; if(strcmp(p, a) = 0) / if (p = a) 7-3-2 C+/C v 1.0 20

43、01 Page 47 of 101 7.3.3 sizeof 7-3-3 a sizeof(a) 12 0 p a sizeof(p) 4 sizeof(p) sizeof(char*) p C+/C 7-3-3 b a sizeof(a) sizeof(char *) char a = hello world; char *p = a; cout sizeof(a) endl; / 12 cout sizeof(p) endl; / 4 7-3-3 a void Func(char a100) cout sizeof(a) endl; / 4 100 7-3-3 b 7.4 7-4-1 Te

44、st GetMemory(str, 200) str str NULL void GetMemory(char *p, int num) p = (char *)malloc(sizeof(char) * num); void Test(void) char *str = NULL; GetMemory(str, 100); / str NULL strcpy(str, hello); / 7-4-1 C+/C v 1.0 2001 Page 48 of 101 GetMemory p _p _p = p _p p _p _p p GetMemory GetMemory free 7-4-2

45、void GetMemory2(char *p, int num) *p = (char *)malloc(sizeof(char) * num); void Test2(void) char *str = NULL; GetMemory2(&str, 100); / &str str strcpy(str, hello); cout str endl; free(str); 7-4-2 7-4-3 char *GetMemory3(int num) char *p = (char *)malloc(sizeof(char) * num); return p; void Test3(void)

46、 char *str = NULL; str = GetMemory3(100); strcpy(str, hello); cout str endl; free(str); 7-4-3 C+/C v 1.0 2001 Page 49 of 101 return return 7-4-4 char *GetString(void) char p = hello world; return p; / void Test4(void) char *str = NULL; str = GetString(); / str cout str endl; 7-4-4 return Test4 str =

47、 GetString str NULL str “hello world” 7-4-4 7-4-5 char *GetString2(void) char *p = hello world; return p; void Test5(void) char *str = NULL; str = GetString2(); cout str endl; 7-4-5 return Test5 GetString2 GetString2 hello world GetString2 C+/C v 1.0 2001 Page 50 of 101 7.5 free delete free delete d

48、elete 7-5 p free NULL p p NULL p p p if (p != NULL) if p NULL char *p = (char *) malloc(100); strcpy(p, “hello”); free(p); / p p if(p != NULL) / strcpy(p, “world”); / 7-5 p 7.6 7-6 p void Func(void) char *p = (char *) malloc(100); / 7-6 1 2 NULL C+/C v 1.0 2001 Page 51 of 101 NULL 7.7 NULL NULL if i

49、f 1 NULL NULL char *p = NULL; char *str = (char *) malloc(100); 2 p free delete NULL p 7.5 3 class A public: void Func(void) cout “Func of class A” Func(); / p Test p-Func() a p a p C+/C v 1.0 2001 Page 52 of 101 7.8 malloc/free new/delete malloc free C+/C new/delete C+ maloc/free malloc/free malloc

50、/free C+ new delete new/delete malloc/free new/delete 7-8 class Obj public : Obj(void) cout “Initialization” endl; Obj(void) cout “Destroy” endl; void Initialize(void) cout “Initialization” endl; void Destroy(void) cout “Destroy” Initialize(); / / a-Destroy(); / free(a); / void UseNewDelete(void) Ob

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 网络科技 > 开发文档

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报