Click here to load reader
Upload
jalia
View
79
Download
3
Embed Size (px)
DESCRIPTION
C++ 语言程序设计教程 第 5 章 构造数据类型. 第 5 章 构造数据类型. C++ 语言程序设计教程 第 5 章 构造数据类型. 第 5 章 构造数据类型. 学习目标. 1. 掌握枚举类型的使用 ; 2. 深入理解数组的概念 , 掌握数组应用的一般方法; 3. 深入理解指针的概念,掌握指针的使用; 4. 注意指针与数组的区别,会使用多重指针以及指针与数组的多种混合体,会分配动态数组; 5. 理解字符串的概念,会使用字符串; 6. 理解引用的概念,掌握引用型函数参数的用法; - PowerPoint PPT Presentation
Citation preview
5
C++
5
5
1. ;2. , 3. 4. 5. 6. 7.
C++
5
5.1 1.
:
012 enum {1, 2,, n}
C++
5
5.1
1 enum weekday { SUN, MON, TUE, WED, THU, FRI, SAT } 7weekdaySUN0MON1TUE2SAT6
2 enum city{ Beijing,Shanghai,Tianjin=5,Chongqing} Beijing0Shanghai1Tianjin51()Chongqing6
C++
5
5.1 :
enum city city1, city2; city city1, city2; city1city2
enum city{ Beijing,Shanghai,Tianjin=5,Chongqing} city1, city2 enum { Beijing,Shanghai,Tianjin=5,Chongqing} city1, city2
C++
5
5.1
: weekday d1,d2,d3,d4; d1=SUN; d2=6; // d3=Shanghai; // d26
d1=d1+2; //int d1=(weekday)(d1+2);
C++
5
5.1 if(city1==3) if(city2>=Beijing) if(Shanghai==1); if(city1>SUN);
cin>>d1; //
C++
4: Shanghai=Beijing()
5.1 5-1
C++
5
123456789101112/******************************************************************* p5_1.cpp ** ********************************************************************/#includeusing namespace std;enum city{ Beijing,Shanghai,Tianjin=6,Chongqing};void main(){ int n; cout
5.1 Input a city number (-1 to exit)1Shanghai8Invalid city number!-1
C++
5
1314151617181920212223 while(n>=Beijing){ switch(n) { case Beijing: cout
5.2
C++
5
5.2.1
void unsigned int [ ] []
C++
5:
5.2.1 void 2 int a[5]; //5a weekday b[10]; //10bweekday
int a1[10], a2[20]; //
int x, a[20]; //
C++
5
5.2.1 1a()5-1 int a[5]; //5a
C++
5
5*sizeof(int)
a
a+1
a+2
a+3
a+4
103410A0
103410A4
103410A8
103410AC
103410B0
a[0]
a[1]
a[2]
a[3]
a[4]
5.2.1
{1,2,, n}, { }iin()00 weekday b[10]={MON,WED,FRI}; char str[ ] = {'a', 'b', 'c', 'd', 'e' }; //str50 [] ={1, 2,, n};
C++
5
5.2.1
naC++00123n-1a[0]a[1]a[n-1][0n-1] a[1+2]=100; // a4100 [];
C++
5
5.2.1
5-2
a[0]a[1]~a[n-1]a[0]a[0] a[0]~a[n-1] a[1]a[2]~a[n-1]a[1]a[1] a[1]~a[n-1] i=2i=n-1, a[i]a[i+1]~a[n-1] a[i]
C++
5
C++
5
123456789101112131415161718/******************************* p5_2.cpp ** -- *******************************/#includeusing namespace std;void main(){ const int MaxN=5; int n,a[MaxN],i,j; for (n=0;n>a[n]; // if (a[n]
80 90 95 70 -195 90 80 70
C++
5
19202122232425262728293031 // for (i=0;i
5.2.1
4. a[5]a+5 ainta+nn+1a[n]a+n*sizeof(int) a+n
C++
5+;
5.2.1
C++
5:
() int a[5],c[5],i; a=c;//! for(i=0;i
5.2.2
1122a[m][n]n
int M[2][3]; MM typedef int M1[3]; // M1 M1 M[2]; // M1M [2][1]
C++
5typedef [1]; [2]
5.2.2
12
a[m][n]
[][]
C++
5sizeof() m*sizeof(a[0]); m*n*sizeof()
5.2.2 :,; int M[2][3]3int2M[0], M[1] M[0]M[0][0]M[0][1]M[0][2] M[1]M[1][0]M[1][1]M[1][2] 2 M: M[0], M[1] M
C++
5
103B2000 M[0][0] M, M[0]
103B2004 M[0][1] M[0]+1
103B2008 M[0][2] M[0]+2
103B200C M[1][0] M[1]
103B2010 M[1][1] M[1]+1
103B2014 M[1][2] M[1]+2
5.2.2
(1) M[m][n]
int M[3][4]={{1,2,3,4},{3,4,5,6},{5,6,7,8}}; //M int a[2][3]={{1},{0,0,1}}; // int b[][3]={{1,2,3},}; // int d[][3]={{1,3,5},{5,7,9}}; // [2][1]=
C++
5M={M[0],M[1],,M[m-1]}M[i]={M[i][0], M[i][1],,M[i][n-1]}i0m-1;
5.2.2 2 () int M[3][4]={1,2,3,4,3,4,5,6,5,6,7,8}; //M int a[2][3]={1,0,0,0,1,1}; //, int b[ ][3]={1,0,0,0,0,0}; //,
int b[ ][3]={1,0,0,0}2
C++
5 /
5.2.2 3
0a[i][j]i+1j+1a[m][n], 1n, 2ma[i][j]
a, a[0]: a, a[0][0] a[i]+j: i+1j+1a[i][j]
C++
5 [] []a[i][j]=1n*i+j+1;a[i][j]=a+(1n*i+j)*sizeof()
C++
5
1234567891011121314151617181920/*********************************************************************** p5_3.cpp ** ************************************************************************/#includeusing namespace std;void main(){ const int MaxN=100,CourseN=5; int n,score[MaxN][CourseN+1]={0}; float aver[CourseN+1]={0}; for (n=0;nscore[n][j]; if (score[n][0]
70 71 72 73 7482 83 84 85 8692 93 94 95 96-1 0 0 0 070 71 72 73 74 36082 83 84 85 86 42092 93 94 95 96 470-------------------------------------------------------------------------81.3333 82.3333 83.3333 84.3333 85.3333 416.667
C++
5
2122232425262728293031323334363637 for (int j=0;j
5.2.3 1 int b[2][3][4]; // typedef int B1[3][4]; B1 b[2];
b[2][3][4] b[0][0]b[0][0][0]b[0][0][1]b[0][0][2]b[0][0][3] b[0] b[0][1]b[0][1][0]b[0][1][1]b[0][1][2]b[0][1][3]b b[0][2]b[0][2][0]b[0][2][1]b[0][2][2]b[0][2][3] b[1][0]b[1][0][0]b[1][0][1]b[1][0][2]b[1][0][3] b[1] b[1][1]b[1][1][0]b[1][1][1]b[1][1][2]b[1][1][3] b[1][2]b[1][2][0]b[1][2][1]b[1][2][2]b[1][2][3]
C++
5
5.2.3 2
b[2][3][4] b[1]; //b[1][0][0] b[2] // b[0]+1; //b[0][1]b[0][1][0] b[1][2]; //b[1][2][0] b[1][2]+4; //b[1][2][4]bb[1][2][4]
C++
5
C++
5
123456789101112131415161718/******************************************* 5_4.cpp ** ********************************************/#includeusing namespace std;void main(){ const int K=3,M=4,N=5; int k=2,m=3,n=4; short int b[K][M][N]; cout
Add of b: &b=0012FEF0 b=0012FEF0 &b[0][0][0]=0012FEF0Add of b[k]: &b[k]=0012FF40 b[k]=0012FF40 b+k =0012FF40 &b[0]+k=0012FF40 &b[k][0][0]=0012FF40Add of b[k][m]: &b[k][m]=0012FF5E b[k][m]=0012FF5E b[k]+m =0012FF5E &b[0][0]+k*M+m=0012FF5E &b[k][m][0]=0012FF5E &b[0][0]+(k*sizeof(b[0]))/sizeof(b[0][0])+m=0012FF5EAdd of b[k][m][n]: &b[k][m][n]=0012FF66 b[k][m]+n=0012FF66 &b[0][0][0]+k*M*N+m*N+n=0012FF66 &b[0][0][0]+(sizeof(b[0])*k+sizeof(b[0][0])*m)/sizeof(b[0][0][0])+n=0012FF66
C++
5
19202122232425262728293031cout
5.2.4
(1) () (2)
C++
5
C++
5
123456789101112131415161718192021/*************************************************** 5_5.cpp ** , ** ***************************************************/#includeusing namespace std;const col=5;enum dir {Asc,Des};void sort(int a[][col], int n, int Cn, dir D) //{ int t[col]; // for (int i=0;i
20060101182861682006010217586161200602042869017620060205290831732006020328080160
C++
5
222324252627282930313233343536373839404142 void main(){ const CourseN=5; int n,score[][CourseN]={{20060101, 1, 82, 86, 0}, {20060203, 2, 80, 80, 0}, {20060204, 2, 86, 90, 0}, {20060205, 2, 90, 83, 0}, {20060102, 1, 75, 86, 0}}; n=sizeof(score)/sizeof(score[0]); for (int i=0;i
5.2.5 C++
1. chars1[ ]={'C','h','i','n','a'}; chars2[ ][4]={{'H','o','w'},{'a','r','e'},{'y','o','u'}};
2. chars3[ ]="China"; chars4[ ][4]={"how", "are", "you"};
C++
5
3. C++ C++
C++
55.2.5
strlen()(\0) Cstring
strset(, C)C, \0strlwr()strupr()strcmp(s1,s2)ASCIIs1s21s1s2-1s1s20s1s2
5.2.5
C++
5
strcpy(s1,s2) s2s1s1 , s2 Cstring strcat(s1,s2)(\0) Ctype toupper()C, \0tolower()Cstdlibatoi()atol()ASCIIs1s21s1s2-1s1s20s1s2 atof()s2s1s1 , s2
C++
5
12345678910111213141516171819/********************************** p5_6.cpp ** ***********************************/#includeusing namespace std;const NameLen=20;void order(char name[][NameLen],int n)//{ char temp[NameLen]; for(int i=0;i
1 AnZijun2 ChenHailing3 CuiPeng4 GongJing5 HuangPin6 LianXiaolei7 LiuNa8 LiuPingInput a name:LiuPingPosition:8
C++
5
2021222324252627282930313233343536373839404142int find(char name[][NameLen],int n,char aname[NameLen]) { for(int i=0;i0) //0return 0; return 0; //}void main(){ charNameTab[][NameLen]={"GongJing","LiuNa","HuangPin","AnZijun", "LianXiaolei","ChenHailing","CuiPeng","LiuPing"}; char aname[NameLen]; int n=sizeof(NameTab)/NameLen; order(NameTab,n); for(int i=0;i
5.3 C++C++ C++C++
C++
5
5.3 1.
C++
C++
5
5.3 2. void *
int*ip; //intip float*fp; //floatfp typedef int A[10]; A *ap; // Aap sizeof(ip)=sizeof(fp)=sizeof(ap)=4; *
C++
5
5.3 3 1 int *p=100; //100int int *p=(char *)100; //100, 2 char *p=NULL; int *ip=(int *)p+100; //charint3& int a[10] //int int *i_pointer=a //int 4voidvoid void void v; //void void *vp; //void *=
C++
5
5.3 3 sizeof int *p, *q, a=5; p=&a; p++; //p4 *p++; //pa5p4 (*p)++; //papa1 *++p; //p4p ++*p; //pa1 *q++=*p++; //:*q=*p, q++, p++
4
C++
5
5.3 5
5void voidvoidvoidvoid void*vp; //voidvp inti=6, *ip; vp=&i; // vpi cout
5.3.2 \0
char str[5]="abcd"; char *p_str="abcd"; (1) str[5]abcdstr abcd\0p_strabcd p_str"abcd"'a' (2) p_strstr
C++
5
5.3.3 1 *
(1) T a[N] (T)a[i] *(a+i) a+ia[i](2) T b[K][M][N], a[i][j][k] *(*(*(b+i)+j)+k); *(*(b+i)+j)+kb[i][j][k]&b[i][j][k]; *(*(b+i)+j)b[i][j][0]b[i][j]; *(b+i)b[i][0][0]b[i]
C++
5
5.3.3 2 *[ ][ ]**p[N]*( p[N])p [N]*
int*p_a[5];5int :
C++
5
5.3.3 3 [ ]*(* )
int(* a_p) [5]; typedef int I_A[5]; I_A *a_p;
C++
5
int*p_a[5] int(* a_p) [5];
p_a[0] a_p
p_a[1]
p_a[2]
p_a[3]
p_a[4]
int
int
int [5]
5.3.4 () int **pp; typedef int * P; P *p; int a[2][3]={1,2,3,4,5,6}; // int *p_a[3]; // p_a[0]=a[0]; // p_a[1]=a[1]; int **pp; pp=p_a;
C++
5
5.3.4 p[i][j]; //a[i][j] *(*(p+i)+j); //a[i][j] *(p[i]+j); //a[i][j]
:
C++
5
5.3.5 C++C++newdelete
1. new new
new() NULL =new ();
C++
5
5.3.5 1new int*ip; ip=newint(5); //ip15int int *ip=new int(5);
C++
5: ip; int5; ip
5.3.5 2new new----new newNULLnew
int*pa; pa=newint[5]; //ip5int
C++
5=new [];
5.3.5 3new new
new1newTT()
: int *pb pb = new int[3][4][5]; //newint[4][5]int // int (*pb)[4][5]; pb = new int[3][4][5]; //pb //
C++
5=new T [1][ 2][]
5.3.4 2delete newdelete
[]delete[] delete ip; delete [] pa; delete [] pb; delete delete [ ]
C++
5
C++
5
123456789101112131415161718192021/********************************** p5_7.cpp ** ***********************************/#includeusing namespace std;void main(){ int m,n; int **dm; coutm>>n; dm=new int * [m]; //mm for(int i=0;i
C++
5 input matrix size m,n2 31 2 34 5 61 2 34 5 6 :newdelete deletenewdelete newdelete newdelete newdelete
22232425262728293031 for (i=0;i
5.3.6 1
5-8 void
C++
5
C++
5
1234567891011121314151617181920/*********************************************** p5_8.cpp * * ************************************************/#include using namespace std;void swap_i(int *num1,int *num2) //{ int t; t=*num1; *num1=*num2; *num2=t;}void swap(void *num1,void *num2,int size) // { char *first=(char *)num1,*second=(char *)num2; for(int k=0;k
C++
5 before swap:a=3 b=6after swap: a=6 b=3before swap:x=2.3 y=4.5after swap: x=4.5 y=2.3before swap:c1=John c2=Antonyafter swap: c1=Antony c2=John
21222324252627282930313233343536373839 first[k]=second[k]; second[k]=temp; }} void main(){ int a=3,b=6; double x=2.3,y=4.5; char c1[8]="John",c2[8]="Antony"; cout
5.3.6 2 void
** * ();
C++
5
5.3.6
C++
5
12345678910111213141516/********************************* p5_9.cpp ** *********************************/#include using namespace std; char *ladd(char *s1, char *s2) { int n1,n2,n; char *res,c=0; n1=strlen(s1); //n1=s1 n2=strlen(s2); //n2=s2 n = n1>n2 ? n1 : n2; //s1,s2 res=new char [n+2]; // for(int i=n+1;i>=0;i--) //s1res res[i] = i>n-n1 ? s1[i-n-1+n1] : '0';
5.3.6
C++
5 123456789012349999912345678901234+99999=012345679001233
17181920212223242526272829303132 for(i=n;i>=0;i--) { char tchar; tchar = i>n-n2 ? res[i]-'0'+s2[i-n+n2-1]-'0'+c : res[i]-'0'+c; // c = tchar>9 ? 1 : 0; // res[i] = c>0 ? tchar-10+'0' : tchar+'0'; // } return res;}void main() { char num1[100],num2[100],*num; cin>>num1>>num2; num=ladd(num1,num2); cout
5.3.6 3
C++
5
5.3.6
:* *
C++
5
5.3.6
int add(int a,int b); // int (*fptr)(int a,int b); // fptr=add; // fptr=&add;
C++
5=
() (* ) ()
5.3.6 sum: add(1,2); //sum (*fptr)(1,2); //sum fptr(1,2); //sum
5-10()
C++
5:add()(*fptr)(1,2)
C++
5
1234567891011121314151617181920/******************************** p5_10.cpp ** *********************************/#include using namespace std; int add(int a,int b) {return a+b;}int sub(int a,int b) {return a-b;}int mul(int a,int b) {return a*b;}int divi(int a,int b) {if (b==0) return 0x7fffffff;else return a/b;}int (*menu[ ])(int a,int b)={add,sub,mul,divi};
C++
5 Select operator: 1: add 2: sub 3: multiply
212223242526272829303132 void main() {int num1,num2,choice;cout
5.3.7 1. const const char * const p="ABCDE"; // p=NULL; // p="1234"; // p=(char *)q; // const *p='1'; // p[1]='1'; // * const =
C++
5
5.3.7 2. const
const char * p="ABCDE"; // *p='1'; // char *q; q=p; // const * = const * =
C++
5
5.3.7 const p=NULL; // p=q; // const const int a[3]={1,2,3}; // a[0]=0; // int *p=a; //
C++
5: const int a[]={1,2,3}; // const char a[]={'1','2','3'}; // const int a[10]={1,2,3}; //
5.3.7 3.
constconst char *q; const char * const p="ABCDE"; // q=p; // p=q; // const * const = const * const =
C++
5
5.4
:;&, ;; intx; int&refx=x;//refxx & = ;
C++
5
5.4.2 1.
*
C++
5
C++
5before swap: x=3 y=5after swap: x=5 y=3
1234567891011121314151617181920 /**************************************** p5_11.cpp * * *****************************************/#include using namespace std;void swap(int &refx,int &refy){ int temp; temp=refx; refx=refy; refy=temp;}void main(){ int x=3,y=5; cout
5.4.2 2.
C++
5
12345678910111213/************************************ p5_12.cpp * * *************************************/#include using namespace std;int max1(int a[ ],int n) //a[ ]{ int t=0; for(int i=0;ia[t]) t=i; return a[t]+0;}
5.4.2
C++
5
1415161718192021222324252627 int& max2(int a[],int n) //a[]{ int t=0; for(int i=0;ia[t]) t=i; return a[t];}int& sum(int a[ ],int n) //a[]{ int s=0; for(int i=0;i
5.4.2 intint &
C++
5m1=10m2=10m3=10m4=-858993460-35
282930313233343536373839404142 void main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; int m1=max1(a,10); int m2=max2(a,10); int &m3=max2(a,10); int &m4=sum(a,10); cout
5.4.3 const
int i (100); const int & r = i; r=200iii i=200; r200const & =
C++
5
5.4.3
void fun(const int& x, int& y) { x=y; // x y=x; }
C++
5: void fun(const int& x, int& y), fun(100,200), fun(100,a)(a)
5.5 C++
C++
5:C++CC++C++class
5.5.1 :
:struct struct { 1 1 2 2 n n}
C++
5
5.5 enum gender {man, ferman}; struct student { long no,birthday; // char name[22]; // gender sex; // float score; // }
5-137
C++
5:C++ score=95; // cout
5.5.2 1.
union { 1 1 2 2 n n}
C++
5
5.5.2 union UData { char Ch; short Sint; long Lint; unsigned Uint; float f; double d; char str[10] }
sizeof(UData)=sizeof(str)=10; sizeof(UData)=sizeof(double)*2=16
C++
5
5.5.2 UData u;strcpy(u.str,123456789"); //u.str
C++
5: 5-14
0013FF70 1=0x31 char Sint:
2=0x32 short
3=0x33 int, long, float
4=0x34 Uint,Lint,f:
0013FF74 5=0x35 double
6=0x36
7=0x37
8=0x38
0013FF78 9=0x39
\0= 0x00
32 31
34 33 31
32 31 31
32
0 sizeof(s) =strlen(s)+1; 4
C++
5
C++
5