C 语言程序设计

Click here to load reader

download C 语言程序设计

of 132

description

C 语言程序设计. 王正杰. 第四章 数组、指针与字符串. [ 本章重点 ] 数组的定义、初始化和使用;指针的定义、访问和运算;指针与数组和函数的联系;字符串与数组和指针的关系;结构体的应用;几种常用的算法和数据结构。 [ 本章难点 ] 指针的定义、赋值和运算;数组、字符串、结构体的操作以及动态存储分配。. - PowerPoint PPT Presentation

Transcript of C 语言程序设计

  • C

  • []

    []

  • CCC

  • []int a[5];

  • 123arrry[10]arrry 100

  • []={};{} int array[5]={ 1,2,3,4,5};5arrayarray [0]=1array[1]=2array[2]=3array[3]=4array[4]=5

  • int array[5]={1,2,3};array [0]=1array[1]=2array[2]=30array[3]=0array[4]=0

  • int array[]={ 1,2,3,4,5};5

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

  • void main(){ int array[5],i ; //5 for (i=0; i
  • Cn0n

  • Fibonaccivoid main(){ int i; int f[20]={1,1}; //01 for(i=2;i
  • R[1..i-1] R[i..n] i R[1..i] R[i+1..n]

  • int min ( int a[], int length){ int i,m=0; //mfor (i=1; i
  • void main(){int array[10]={18,26,23,13,15,25,27, 14,29,31},tag;tag=min(array,10); //min, //cout
  • void sort(int a[],int length){//tag//bufferint i,j,tag,buffer;for(i=0;i
  • tag=i; //tag // // //ilengthfor(j=i;ja[j]) tag=j; ////a[tag] //a[i]buffer=a[tag];a[tag]=a[i];a[i]=buffer;

  • void main(){int array[10]={6,2,10,3,7,5,9,4,8,1},i;cout
  • a [0]a [1]sorttagiimainarraymainarraysorta

  • 10()

  • R[1..n] i R[1..n-i+1]

    R[n-i+2..n]

    n-i+1R[1..n-i]

    R[n-i+1..n]

    n-i+1

  • void main(){ int a[10]={6,2,10,3,7,5,9,4,8,1}; int i,j,t; for(j=1;j
  • X-YXY

  • [1][2];12 int array[2][3]; array

  • 0nm0n-10m-1

  • int array[2][3]={1,2,3,4,5,6};int array[2][3]={{1,2,3},{4,5,6}};

  • int array[2][3]={1,2,3};array[0][0]array[0][1]array[0][2]1230int array[2][3]={{0,2},{4}};array[0][0]=0array[0][1]=2array[0][2]=0array[1][0]=4array[1][1]=0array[1][2]=0

  • int array[2][3]={1,2,3,4,5,6};int array[][3]={{2},{4,5,6}};

  • [] [][]int array[2][3]={ 1,2,3,4,5,6};a=array[1][0]; array[1][0]=6;array4a6

  • 4*4 void main(){ int c[4],r[4], i,j; //c[4] // r[4] int array[4][4]= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; for(i=0;i
  • for(i=0;i
  • void RowSum(int A[][4], int nrow){ int sum; for (int i = 0; i < nrow; i++){sum = 0;for(int j = 0; j < 4; j++) sum += A[i][j];cout
  • void main(void){ int i; int Table[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}}; for (i = 0; i < 3; i++) { for (int j = 0; j < 4; j++)cout
  • C++C++C++

  • 41

  • &int var;&var var

  • C

  • C

  • *; int *p; //pfloat *q; //qchar f ,*r; //fr

  • *=;& &int a=5,*p=&a; appa

  • int a=5, *p; p=&a;0stdio.hNULL

  • *

  • void main(){ int a=10,b,*p=&a,*q; q=p; // b=*q; //*q //b cout
  • void splitfloat(float x, int *intpart,float *fracpart){ //intpart fracpart *intpart = int(x);// x *fracpart = x - *intpart; //x}

  • void main(void){int i, n;float x, f;cout x;splitfloat(x,&n,&f); //cout
  • Swap(int *p1,int *p2){ int temp; temp=*p1; *p1=*p2; *p2=temp;}

  • void main(){ int a,b; int *pointer_1,*pointer_2; cin>>a>>b; pointer_1=&a; pointer_2=&b; cout
  • p++p--p+ip-ip+=ip-=i(p-q) p==qpqp>qpp
  • i()ishort array[8],*p;p=array; p=p+5;parraya[0]parray[5]

  • parrayp+i,p-i,p++,,p--,p+=i11ii short *p; p++;p22int *p; p++;p4

  • int array[10],*p,*q,n; p=array; q=p+2;n=q-p;parraya[0]parray[2]qpnn2

  • 00 p==0p p!=0p0int *p;int b=*p;p*p

  • array[10]arrayarrayarray[0]array+iarray[i]array+i& array[i]array[2]*array+2

  • int a[10], *pa; pa=&a[0]; pa=a;*paa[0]*(pa+1)a[1]... *(pa+i)a[i].a[i], *(pa+i), *(a+i), pa[i] a++a

  • inta10

  • for(i=0; i
  • int array[3][4]arrayarray[0] array[1] array[2]array[0] array[1] array[2]array[0] array[1] array[2]

  • arrayarray[0] array[1] array[2]array+12array[1]array[0]+1array[0][1]array1array[0] array[1] array[2]

  • * *arrayarray[0] *array+1array[0]+1**arrayarray[0][0]* array[0]**array**array**

  • array[0]+1*(array +0)+1array[0][1]*(array[0]+1)array[0][1]*(*(array +0)+1)*(* array +1)array[0][1]*(array[i]+j)*(*(array +i)+j)array[i][j]*(array +i)array[i]

  • arrayarray+iarray [i]*(array+i)*(array+i)+jarray [i]+j*(array[i]+j)*(*(array+i)+j) array [i][j]array [i][j]

  • 4void main(){ int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}}; int (*p)[4],i,j; p=a; cin>>i>>j; cout
  • ;

  • 34naverage()search ()i

  • void average(float *p,int n);void search(float (*p)[4],int n);void main(){ float score[3][4]={{65,67,70,60}, {80,87,90,81},{90,99,100,98}}; average(*score,12);//12 search(score,2); //2}

  • void average(float *p,int n){ float *p_end; float sum=0,aver; p_end=p+n-1; for(;p
  • void search(float (*p)[4],int n){ int i; cout
  • *[]char *pc[5];5

  • void main(){ int line1[]={1,0,0}; int line2[]={0,1,0}; int line3[]={0,0,1}; int *p_line[3];//p_line[0]=line1;//p_line[1]=line2;p_line[2]=line3; cout
  • int fun(int x,int y ,int z)int(*pfun)(int,int,int) int *pfun(int,int,int)pfun=fun;

  • int minx(int x,int y){ if(x
  • void main(){ int a,b,c;int (*pmin)(int x,int y); //pmin=minx; //min // pmin=&minxcout
  • *

  • *float *fun(int a , int b);

  • float *compute(float x,float y){ //floatresult // float *result=new float[4];//result*result=x+y;*(result+1)=x-y;*(result+2)=x*y;*(result+3)=x/y;return result; //result}

  • void main(){ float a,b,*p; //floatp couta; coutb; p=compute(a,b);//presultif(p) //result{ //pcout
  • compute4resultmainpcomputeresultpp

  • C++

  • C++newdeletenewnew ;

  • NULL:int *p; p=new int(2); newint2pp

  • newnew []; NULL int *parr;parr=new int [5]; //5

  • deletedeletedelete p ; // delete delete [ ]parr; // delete []

  • void main(){float *parr; //int i,length; //lengthcoutlength;parr=new float[length]; //lengthfloatif(parr) //{}elsecout
  • for(i=0;i
  • Cvoid *malloc(unsigned int size);size()(void)()(NULL)

  • void free(void *p);ppmallocfree

  • C++C++

  • charchar c[10]; // 10 char c[10]= hello c++;char c[10]={hello c++}; char c2[]={ hello c++};

  • '\0''\0'

  • char week[][3]={Sun,Mon,The, wed,Thu,Fri}; 6week3

  • ASCII void main(){char letter[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";intasc; //ASCII coutasc;if(asc>=A && asc
  • void main(){intasc; coutasc; cout
  • char *string, c[10]=hello c++; string=c;

  • int str_len(char str[]){char *p=str; //pstrwhile(*p!='\0') // p++; //'\0'return p-str;}void main(){char string[]="abcdefghijklmnopqrstuvwxyz"; cout
  • stringcchar *string=hello c++; // char *string; // string=hello c++; //

  • char c[10]=hello c++; //// char c[10]; //c=hello c++;//

  • str2str1void str_cpy(char str1[] , char str2[]){char *p1=str1,*p2=str2; int i=0;while(p2[i]!='\0') { p1[i]=p2[i]; i++; //i } p1[i]=\0; //str1'\0'}

  • void main(){char letter1[]="abcdefghijklmn", letter2[]="I love C++";cout
  • C++"stdio.h""string.h"

  • strlen( str ) (\0)

    strcpy (str1,str2) str2str1\0

  • C++longcharintfloat

  • structstruct student{ long num; char *name;int majornum;float scores;};

  • 1. ()struct student:struct student student1, student2;2. (), structstudent {} student1student2() struct {} student1student2

  • .zhangsan.numzhangsannumzhangsan.num=20061001; zhangsan.majornum=10;

  • = lisi=zhangsan;zhangsanlisi

  • struct student{ long num; char *name; int majornum; float scores;};

  • void main(){ //studentlisiwangwualisistudent lisi={20061002, "",10,612},wangwu,a;wangwu.num=20061003;wangwu.name="";wangwu.majornum=10; wangwu.scores=607; cout
  • student group[10]; 10studentgroup

  • .a[1].num=20061002;student lisi; lisi=a[1];

  • struct student *ps; studentstruct&student zhangsan, *ps; ps=&zhangsan;

  • *student *ps=&zhangsan; lisi=*ps;(*ps).num=20061001;20061001zhangsannum.*(*ps) .num*p.num*(pnum)

  • ->->.->->ps->num=20061001

  • n an an-1 ananan-1a1

  • #define LEN sizeof(struct student)struct student{ long num; float score; struct student *next;};

  • struct student *creat(void) { // int n=1; struct student *head; struct student *p1,*p2; head=( struct student*) malloc(LEN); //*/ head->next=NULL; p1=(struct student*)malloc(LEN); cout
  • while(p1->num!=0) { p1->next=head->next; head->next=p1; p1=(struct student*)malloc(LEN); cout
  • void print1(struct student *head){ cout
  • main(){ struct student *head; //creat head=creat() ; print1(head);}

  • sp

  • C++\0

  • C++newdelete