第六章 复合数据类型

Click here to load reader

Upload: eagan

Post on 15-Jan-2016

153 views

Category:

Documents


0 download

DESCRIPTION

程序设计是计算机学科的. 核心和灵魂. 第六章 复合数据类型. 程序设计基础. 2000H. 0x34. x. 0x12. 6.1 变量与赋值. 变量在内存中占用的存储空间的首地址称为变量的地址. 例: int x=0x1234; 则 x 的地址为 2000H. 根据变量的使用属性,变量分:. 值变量: 在其存储空间中存放的是该变量的值,按变量名存取变量值; 地址变量: 在其存储空间中存放的是地址,使用变量名时操作的是变量的地址;. 指针类型的变量是最常用的地址变量. 6.2 指针类型. 指针 : 一个变量的地址称为该变量的指针. - PowerPoint PPT Presentation

TRANSCRIPT

  • int x=0x1234; x 2000H6.1

  • 6.2:int a=1;&a int *bbb*b

  • 12345678 void9.10.11.12.

  • 1.

    *1,*n;,: int * p, * q; /* pq*/ float x ,y; pf=&x; /* pfpfValuexy*/ pfValue=&y;

  • * int *pInt1, pInt2; pInt2** pInt1 pInt1* pInt1 pInt1 pInt2

  • 2.

  • int a=0, *p;p=&a;*p=*p+2; & &aa * *ppint a,*p; 2000Happ=&a; /* pa */2000H*p=2;26-1

  • int age = 30; int* age_ptr; age_ptr = & age ;*age_ptr = 50 ;50age_ptr &age*age_ptr age *age_ptr = 50 age = 50;age++; (*age_ptr)++;

  • 1) int *p; *p=2; /* Error! */int a,*p; p=&a; /* pa */*p=2;2)& * int a,*p; p=&a;&*p p *&a a

  • 3)&

    p = &45//p = &i + &j //p = &i + 4; //

    *

    int i;int *p = &i;*p = 45//*i = 34//

  • 6-2 #include void main( ){int value1 =10,value2=20,temp;int *ptr;ptr=&value1;temp=*ptr; //*ptrptr //value110temp*ptr=value2; //value220ptr //value120value2=temp; cout
  • 6-3 #include void main( ){int a=10,b=10,*ptr1,*ptr2;ptr1=&a;ptr2=&b;cout
  • 3.p = &i;p = &j;2df42df8)............9915612342df01238123c2df42df8............int * p;int i = 156;int j = 99;

  • 6-4

    #include void main( ){int a,*ptr1=&a,*ptr2=&a;cout

  • 4unsigned long intsizeof(int *) == sizeof(char *) ==sizeof(float *) == sizeof(char **)

  • 5.6-51#include 2void main( )3{4int a=10,*ptr1=a,*ptr2,*ptr3;5*ptr2=*ptr1;6ptr3=&ptr2;7a=ptr2;8}

  • 76-6#includevoid main(){ int *p1,*p2,i1,i2; cin>>i1>>i2; p1=&i1;p2=&i2; cout
  • p1p2

  • 6-7 #includevoid main(){ int *p1,*p2,*p,i1,i2; cin>>i1>>i2; p1=&i1;p2=&i2; cout
  • 6-8

    #includevoid main(){ int *p1,*p2,i1,i2,i; cin>>i1>>i2; p1=&i1;p2=&i2; cout

  • p1p2

  • 6-9

    6-7#includevoid main(){ int *p1,*p2,*p,i1,i2; cin>>i1>>i2; p1=&i1;p2=&i2; cout

  • 7.

    **int a,*q=&a;int **p=&q;ppp

  • 6-10

    #include #include void main( ){char **p;char a='x',*q=&a;p=&q;cout

  • 6-11 #includevoid main(){ int a; int *p=&a,**pp=&p; //pappp a=1; cout
  • 8. voidvoid ()1void void2voidvoid *p1; int *p2; p1 = p2; void *p1; int *p2; p2 = p1;

  • 6-12 #include void main( ){void *p;int a=10, *ptr=&a;*p=a; //pptr=p; //voidp=ptr; cout
  • 9.const int *ptr=&x;*ptr=4;ptr=&y; int * const ptr=&x;*ptr=4;ptr=&y;const int * const ptr=&x;*ptr=4;ptr=&y;,,, (1)const(2)const*,((3)const

  • 10.C++C++

  • 111p1++++p1p1----p1/*a()*/2p1+np1-n/*a()n*/3p2-p1/*ab*/int a, b, *p1, *p2;p1=&a;p2=&b;a2000Hb2400Hp1p22000H2400H2004H2004H100H

  • () ptr+n (ptrn)ptr+n*d(d)n ptr1-ptr2=(ptr1- ptr2) /

  • p1 == p2p2 > p1p2 < p1

  • 1)char a, *p;p=&a;2)int *p, *q;p=q;3)char a[10], *pa;pa=a;4)int *p, *q, n;p=q+n;p=q-n;p+=n;p-=n;

  • int max ( int x , int y ){ int z ; z=x>y? x : y ; return (z) ;}void main (void){ int a ,b ,c ; cin>>a>>b ; c=max( a ,b ); cout
  • int main(){int x =2, y = 3;int temp;temp = x;x = y;y = temp;cout
  • 11.void swap(int *p,int *q){int temp = *p;*p = *q;*q = temp;cout
  • : void swap(int *p,int *q){int temp = *p;*p = *q;*q = temp;}swap(2, 3);

  • 12.1) & = ;

  • int num=50;int& ref=num;ref+=20;cout
  • void Swap(int& p, int& q){ int temp=p; p=q; q=temp; cout
  • 6.310,void main(){ int w1, w2, w3, w4, w5, w6, w7, w8, w9, w10; int t; cin>>w1>>w2>>&w3>>w4>>w5>>w6>>w7>>w8>>w9>>w10;

  • t=(w1+w2+w3+w4+w5+w6+w7+w8+w9+w10)/10; if( w1 < t ) cout
  • C++main(){ int w[10]; /* 1 */ int t = 0, i; for( i=0; i>w[i] ; for( i=0; i
  • 1.2.3.4.5.6.7.8.9.10.11.12.13.14.

  • 1..

  • C++0 w[0]w w[9]w

  • 2. 1[], , n[].,,

  • : int a[5]; a[0]a[1]a[2]a[3]a[4]a()5,4BYTEa:,int a; float a[5];[ ][]0 int n=5; int a[n];

  • ,0int a[5];5a[0],a[1], a[2], a[3], a[4]int a(10); int n=5;int a[n]char name[0]; float weight[10.3];float array[-100];

  • 3.{ } int a[5]={2, 4, 6, 8, 10};{}

  • int a[ ]={ 2 , 4 , 6 , 8 , 10 }; int a[ ]; 0 int a[5]={ 2 , 4 }; a[0]=2, a[1]=4, a[2]=0, a[3]=0, a[4]=0 int a[5]={1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }; (3)0 int a[10]={0};

  • int a[10]; int i; for(i=0;i>a[i];4.

  • 5.,,,.12 [ ] :C++,.

  • [] a[3] int a[10] ; float i=3 ; a[i]=10 ;

  • 0 int a[2]; cin>>a[1]>>a[2];1 int a[5]={ 2 , 4 , 6 , 8 , 10 } , b[5] ; b[5]=a[5] ;2 int a[5] ; a[5]={ 2 , 4 , 6 , 8 , 10 } ;aaaa[0]

  • 6.11010#include using namespace std;int main(){ float a[10]; int i; for(i=0 ; i>a[i]; for(i=9 ; i>=0 ; i--) cout
  • (Fibonacci) 11235820main(){ int f[20]={1,1} int i; for(i=2 ; i
  • 7. 1999 90 40 80 30 2000 100 50 90 40 2001 95 45 100 50

  • 8. [1][2] int a[3][4];int a[3,4];

  • :.int a[3][4];/*3*4(34). */a:a[0] a[1] a[2]4 a[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3] a

  • int a[0][3]; 0 int i=3 , j=4 ; int a[i][j] ;

  • 9. int a[2][3]={{1 , 2 , 3 } , { 4 , 5 , 6 }}; int a[2][3]={1 , 2 , 3 , 4 , 5 , 6 };

  • 0 int a[3][4]={{1},{5},{9}};int a[3][4]={{1},{5,6}};int a[3][4]={{1},{},{0,0,11}};int a[3][4]={1,0,6,7,8,9};

  • ()int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};int a[][4]={{0,0,3},{},{0,10}};

  • 10. int a[2*5][3*4], i=15; a[3*3][0], a[1][i-5]

  • b[1][2]=a[2][3]/2int a[3][4]a[3][4]=3aa aa[0][0]

  • 11.1#include #include using namespace std;int main(){ int a99[10][10], i, j ; for(i=1; i
  • a=1 2 34 5 6b=1 4 2 53 6main(){ int a[2][3]={{1,2,3},{4,5,6}} int b[3][2], i,j; cout
  • 12. [1][n]int a[2][3] [2];a[0][0][0]a[0][0][1]a[0][1][0]a[0][1][1]a[0][2][0]a[0][2][1]na[1][0][0]a[1][0][1]a[1][1][0]a[1][1][1]a[1][2][0]a[1][2][1]3.

  • 13.1)2) aa[0] [][][] int i, a[5], *pa=a; a[i]pa[i]*(a+i)*(pa+i)

  • 1)int a[5], *pa=a;pa[0],*(a)pa[1],*(a+1)pa[2],*(a+2)pa[3],*(a+3)pa[4],*(a+4)

  • 1)int a[10],*p;2)p = a;3)ip+i,a+i&p[i] &a[i] a[i]*(p + i) a[i] *(p + i) *(a+i) p a *p a[0] *ap[0]*(p + i) a[i] *(a+ i)p[i]p+i a + i &a[i]&p[i]a[2]a[1]a[0]............a[3]ap::a[i] p[i]:*(p+i):*(a+i)

  • --a++; //a[2]a[1]a[0]............a[3]app += 2;int a[10];int *p = a;cout
  • #includevoid main(){int a[5]={1,2,3,4,5},i;for (i=0;i
  • #includevoid main(){ int a[5]={1,2,3,4,5},i;int *p=a;for (i=0;i

  • 2)aint a[3][4];a[0] aa[1]a[2]---------a[0][0] a[0][1] a[0][2] a[0][3]---------a[1][0] a[1][1] a[1][2] a[1][3]---------a[2][0] a[2][1] a[2][2] a[2][3]

  • int a[3][4] a 0 a , 1 a + 1, 2 a + 2; a &a[0][0]*a a[0]*(a+0)a+1a+i, a[i],*(a+i),&a[i][0] a[i]+j, &a[i][j], *(a+i)+ja+1&a[1][0] a[1]*(a+1)a+2&a[2][0] a[2]*(a+2)

  • :

    1)a[i][j]*(a[i]+j) *(a+i)+j*(&a[0][0]+col*i+j)

    2)int *p,a[3][4];p=a[0];a[i][j] *(p+4*i+j)

  • #includevoid main(){ int a[3][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int *p; for (p=a[0];p
  • 14. 1)2)1)

    *[]int *p[4]

  • 34, 1~12main(){ int i,j,k=1,a[3][4], *b[3]; for(i=0;i
  • #include #include void main( ){char *proname[ ]={"Fortran","c","c++"};cout

  • #includevoid main(){int x[2][3]={{1,2,3},{4,5,6}};int i,j;int *px[2]; //px[0]=x[0];px[1]=x[1]; //for(i=0;i
  • 2) (*)[]int (*p)[4]4int a[3][4], (*p)[4];p=a;a[i][j] *(*(p+i)+j)int a[3][3],*p; p=a[0]; a[i][j] *(p+3*i+j)

  • 6.4 \0china 65

  • char c[10]; c1.

  • 1)char charray[const exp1][const expn],;char a[10],b[2][12];char str1[6]={ h, e, l, l, o};char str2[ ]={hello };

    \0hello\0

  • int c[10] 4 char c[10]1int c[10]4 \0 int c[8]={H,e,l,l,o}; c[0]=H, c[1]= e, c[2]= l, c[3]= l, c[4]= ,o c[5]=\0, c[6]=\0, c[7]=\0

    C[0]C[1]C[2]C[3]C[4]C[5]C[6]C[7]C[8]C[9]Iamhappy

    C[0]C[1]C[2]C[3]C[4]C[5]C[6]C[7]C[8]C[9]Iamhappy

  • 2) char str[]={Hello} char str[]=Hello char str[]={H, e, l, l, o}; H e l l o H e l l o \0\0char ch[2]={A, B}\0\0char str[]={H, e, l, l, o, \0};

  • 2.1char *str =I love China! ;str1\0 2

  • 1

    2 2

  • char *a=I love China!; char *a; a=I love China!; char str[14]={I love China!}; char str[14]str[]=I love China!;

  • #include #include void main( ){char p[3][6]={"hello","good","abcde"};for (int i=0;i
  • 6.5 -> 1class exe{ int x; public: void set_a(int a) { x=a; } void show_a() { cout show_a();}2void main(){ exe ob[2], *p; ob[0].set_a(10); ob[1].seta(20); p=ob; p->show_a(); p++; p->shoe_a();}

  • this

  • thisthisthis,this.,this.,this.

  • main(){Person p,q;p.setname ("C++");q.setname ("VC");

    }C++VCthis void setname(string n){ this->name=n; }100200100200this string getname(void){ return this->name; }cout

  • this ,;this,.

    this ,

  • newdelete

  • newdelete-------- new:: newnew3:1 int*ip; ip=newint; 2 int*ip; ip=newint(68); 3 int*ip; ip=newint[5]; delete ip;delete[] ip;delete ip;

  • delete:new:delete delete[] 1newdelete 2delete 3deletenew 4new

  • int i=10;char *lpBuf; lpBuf = new char[i]; lpBuficharlpBuf[0],lpBuf[1] ... delete[] lpBuf; NULLNULL*void

  • 6.66.6.11. 2.,,.,3. (*)( ) int (*fp)(); /* fpint*/

  • 4.-----17:a b main ( ) { int max(int,int); int (*p)(int,int); int a,b,c; p=max; Cin>>a>>b;c=(*p) (a,b);Cout
  • 5.

    18:process a b processa b a b ++--

  • min(int x,int y) /**/{int z;if(x
  • 6.6.21.,2. *([]){} int *pr(x,y); pr().()*,pr()..*,()int int (*p)(int i,int j); int *p(int i,int j);

  • float *find(float(*p)[4],int); main() { static float score[][4]={{60,70,80,90},{56,89,34,45},{34,23,56,45}}; float *p; int i,m; coutm; cout
  • 6.7 .06001 Li Fun M 18 87.5 Beijing Num name sex age score addr,.,++()

  • struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }struct { .},

  • struct studentstruct student

    3 numstruct studentnum

  • 4

    struct date{ int year; int month; int day;}; struct student{ int num; char name[20] char sex; struct date birthday; float score[4];};num score

  • intcharfloat.1.struct { .}struct struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2;

  • 2. struct { .}struct student { int num; char name[20]; char sex; int age; float score; }stu1,stu2; sizeofsizeof(struct student)sizeof(stu1)

  • .0:struct { .}struct ={} struct student { int num; char name[20]; char sex; int age; char addr[30]; }; struct student stu1={06001,Wang Lin,M,19, 200 Beijing Road};

  • :struct { .}={} struct student { int num; char name[20]; char sex; int age; char addr[30]; }stu1={06001,Wang Lin,M,19, 200 Beijing Road};

  • . ,(): struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu1.num=10;stu1.score=85.5;stu1.score+=stu2.score; stu1.age++;cin>>stu1; ()stu1={06001,Wan Lin,M,19,87.5,DaLian}; ()

  • :struct student stu1,stu2;cin>>stu1; () student1.birthday.month:student2.score=student1.score; student1.age++;struct date{ int year; int month; int day;}; struct student{ int num; char name[20] char sex; struct date birthday; float score[4];};

  • &student1.num student1 stu2=stu1;

  • 8-1void main() { struct student { int num; char name[20]; char sex; int age; float score; }stu1,stu2; stu1.num=06001; strcpy(stu1.name,"zhang"); stu1.sex='M'; stu1.age=19; stu1.score=88; stu2=stu1; cout
  • ,++1.:,.2.: enum {} enum Weekdays {sun, mon, tue, wed, thu, fri, sat}; enum Color{red, yellow, blue, white, black}; enum Sex{male, female};

  • 3.enum weekdays {sun, mon, tue, wed, thu, fri, sat};enum weekdays workday;

    enum weekdays {sun, mon, tue, wed, thu, fri, sat} workday;enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} workday;

  • :1)2) weekdays sun=0 ; mon=1; sun=mon ;

    3),: workday=2; workday=tue; workday =(enum Day)6; a=sat;

  • 4)0012 a=sat; a1 enum weekdays {Sun=, Mon ,Tue, Wed, Thu, Fri, Sat}Sun=Mon=Tue=25).,: if (workday==mon) if (workday>tue)

  • enum weekdays {Sun,Mon,Tues,Wed,Thur,Fri,Sat}nday;void main(){ int nindex;char * pstr;while (cin>>nindex) {switch(nindex){case Sun: pstr="Sunday";break;case Mon: pstr="Monday";break;case Tues: pstr="Tuesday";break;case Wed: pstr="Wednesday";break;case Thur: pstr="Thursday";break;case Fri: pstr="Friday";break;case Sat: pstr="Saturday";break;default:break; }cout
  • typedeftypedeftypedef typedef float REAL; /* REALfloat */ float a, b; REAL a, b; NUM typedef int NUM[10]; int n[10], m[10]; NUM n, m; NODE typedef struct student { char num[4]; float score;} NODE; struct student st,*p; NODE st,*p;

  • 1) ()::.,,,,,

  • 101010 float average(float a[10]) { int i; float ave,sum=0; for (i=0;i
  • n #define N 10 void main(void) { int a[N],i; void sort(int array[],int n); cout
  • 13:34 .,",,,,,,, ,,void inputarray(int array[3][4]) { int i,j; cout
  • 2),

    [7.14] int Sum(int* pData, int nNum){ int nResult=0; for(int i=0; i

  • 3)int Sum(int nData[], int nNum){ int nResult=0; for(int i=0; i
  • 4)int Sum(int* pData, int nNum){ int nResult=0; for(int i=0; i