第四课数组和指针.pptVIP

  • 0
  • 0
  • 约7.52千字
  • 约 36页
  • 2019-09-12 发布于浙江
  • 举报
例:使用指针,实现以下功能:输入三个整数,按照从大到小的顺序输出,使用指针变量进行交换。 #include iostream.h void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; } int main() { int a,b,c; int *pa,*pb,*pc; pa=a; pb=b; pc=c; cinabc; cout输入数顺序为:*pa,*pb,*pcendl; if(*pa*pb) 例:使用指针,实现以下功能:输入三个整数,按照从大到小的顺序输出,使用指针变量进行交换。 swap(pa,pb); if(*pa*pc) swap(pa,pc); if(*pb*pc) swap(pb,pc); cout排序后数据为:*pa,*pb,*pcendl; return 0; } 例:求出下列程序的运算结果 #includeiostream.h int k=56; void f1(int a,int b) { coutin fl:a,b=a,bendl; a+=10;b+=20;} int f2(int a,int b) { coutin f2:a+b=a+bendl; if((a+b)%2==0) return a; else return b;} void main() { int x=1,y=2,z=3,k=4,w=0; f1(x,y); coutmain1:x,y=x,yendl; coutmain1:z,k,w=z,k,wendl; w=f2(z,k)++; coutmain2:z,k,w=z,k,wendl; w=f2(z,k)++; coutmain3:z,k,w=z,k,wendl;} #includeiostream.h void print(int),print(char),print(char *); void main() { int m=2000; print(m); print(m); print(good); } void print(char x) { coutxendl; } void print(int x) { coutxendl; } void print(char *x) { coutxendl; } #include iostream.h int x=1,y=2; max(int x,int y) {return xy?x:y;} void main() {int x=3; coutmax=max(x,y) endl;} #include iostream.h int a=100; void main() { int a=200; couta,::aendl; } #include iostream.h #include iomanip.h void main() { int i,j; for(i=4;i=1;i--) { coutsetw(i+5) ; for(j=1;j=4;j++) cout* ; coutendl; } } #includeiostream.h int main() { int n,i,j,k,m; coutinput number: ; cinn; //输入数字金字塔数目n for( i=0;in; i++) //第一层循环,控制有多少层金字塔 { for(m=0;mn-i; m++) cout ; //第二层循环,输出每层的空格。 for(j=i+1;j0; j--) cout*; //第二层循环,输出每层左半边数。 for(k=1;k=i; k++) cout*; //第二层循环,输出每层右半边数。 coutendl; //输出每层的换行。 } } #includeiostream.h int main() { int n,i,j,k,m; coutinput number: ; cinn; //输入数字金字塔数目n for( i=0;in;++i) //第一层循环,控制有多少层金字塔 { for(m=0;mn-i;++m) cout ; //第二层循环,输出每层的空格。 for(j=i+1;j0;--j) cout*; //第二层循环,输出每层左半

文档评论(0)

1亿VIP精品文档

相关文档