例题6.1
#include<iostream>
using namespace std;
int main()
{
int a=10;
int *p;
p=&a;
cout<<"a= "<<a<<endl;
cout<<"p= "<<p<<endl;
cout<<"&a= "<<&a<<endl;
cout<<"*p= "<<*p<<endl;
cout<<"&p= "<<&p<<endl;
cout<<endl;
}
例题6.2
#include<iostream>
using namespace std;
int main()
{
int a,b;
int *p=&a,*q=&b,*tp;
cout<<"请输入两个数:";
cin>>a>>b;
if(a<b)
{
tp=p;p=q;q=tp;
}
cout<<"初始的两个数为:"<<a<<" "<<b<<endl;
cout<<"从大到小排序后的数为:"<<*p<<" "<<*q<<endl;
return 0;
}
例题6.3
#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
int *p=&a,*q=&b;
cout<<"please input a and b:";
cin>>a>>b;
cout<<"初始的两个数为:"<<a<<" "<<b<<endl;
if(a<b)
{
temp= *p;*p=*q;*q=temp;
}
cout<<"从大到小排序后的数:"<<*p<<" "<<*q<<endl;
return 0;
}
例题6.4
#include<iostream>
using namespace std;
int main()
{
int a=10,b=10,*ptr1,*ptr2;
ptr1=&a;
ptr2=&b;
cout<<boolalpha<<(*ptr1==*ptr2)<<endl;
cout<<boolalpha<<(ptr1==ptr2)<<endl;
return 0;
}
例题6.6
#include<iostream>
using namespace std;
int main()
{
char a[]="Hello world",b[20],*p,*q;
p=a,q=b;
for(;*p!='\0';p++,q++)
*q=*p;
*q='\0';
cout<<"string1 is;";
cout<<a<<endl;
cout<<"string2 is:";
cout<<b<<endl;
return 0;
}
例题6.7
#include<iostream>
using namespace std;
int main()
{
char *p="student";
for(; *p!='\0';p++)
cout<<p<<endl;
return 0;
}
例题6.8
#include<iostream>
using namespace std;
int main()
{
char *p="student";
for(int i=0; *p!='\0';p++)
cout<<p[i];
cout<<endl;
return 0;
}
例题6.9
#include<iostream>
using namespace std;
int main()
{
char s[50];
char *p=s;
cout<<"please input a string:";
cin.get(p,50);
for(;*p!='\0';)
p++;
cout<<"the length is:"<<p-s<<endl;
return 0;
}
例题6.10
#include<iostream>
using namespace std;
int main()
{
int i=5,*p,**pp;
p=&i;
pp=&p;
cout<<&i<<'\t'<<i<<endl;
cout<<&p<<'\t'<<p<<'\t'<<*p<<endl;
cout<<&pp<<'\t'<<pp<<'\t'<<*pp<<'\t'<<**pp<<endl;
return 0;
}
例题6.11
#include<iostream>
using namespace std;
int main()
{
int a[5]={1,2,3,4,5};
int *p[5]={&a[0],&a[1],&a[2],&a[3],&a[4]};
for(int i=0;i<5;i++)
cout<<*p[i]<<'\t';
cout<<endl;
return 0;
}
例题6.12
#include<iostream>
using namespace std;
int main()
{
char a[]="computer system",b[]="hareware",c[]="software";
char *p[4];
p[0]=a;
p[1]=b;
p[2]=c;
p[3]=NULL;
for(int i=0;p[i]!=NULL;i++)
cout<<p[i]<<endl;
return 0;
}
例题6.13
#include<iostream>
using namespace std;
int main()
{
char *weekname[]={"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
int i;
while(1)
{
cout<<"please input week No.:";
cin>>i;
if(i<0||i>6)
break;
cout<<"week No."<<i<<"-->"<<weekname[i]<<endl;
}
return 0;
}
例题6.14
#include<iostream>
using namespace std;
int main()
{
char *p[]={"Rebacca","Heaven","Michael Chang","Linda Tsai"};
char *temp;
int i,j,n=4;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(strcmp(p[j],p[j+1])>0)
{
temp=p[j];p[j]=p[j+1];p[j+1]=temp;
}
for(i=0;i<n;i++)
cout<<p[i]<<endl;
return 0;
}
例题6.15
#include<iostream>
using namespace std;
int main()
{
int a[2][3],*p[2];
int i,j;
p[0]=a[0];
p[1]=a[1];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=j+i;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]:";
cout<<*(p[i]+j)<<endl;
}
return 0;
}
例题6.16
#include<iostream>
using namespace std;
int main()
{
int a[2][3],*p[2],**pp;
int i,j;
p[0]=&a[0][0];
p[1]=&a[1][0];
pp=p;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=j+i;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]:";
cout<<*(*(pp+i)+j)<<endl;
}
return 0;
}
例题6.17
#include<iostream>
using namespace std;
int main()
{
char *a[]={"Rebacca","Heaven","Michael Chang",NULL};
char **pp;
pp=a;
while(*pp!=NULL)
cout<<*pp++<<endl;
return 0;
}
例题6.18
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[2][3]={1,2,3,4,5,6};
int (*p)[3];
int i,j;
p=a;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<setw(3)<<(*p)[j];
p++;
}
cout<<endl;
return 0;
}
例题6.19
#include<iostream>
using namespace std;
void display(int*, int*);
int main()
{
int a=5,b=10;
int *pa=&a;
int *pb=&b;
display(pa,pb);
return 0;
}
void display(int *p1,int *p2)
{
cout<<"parameter 1 is "<<*p1<<endl;
cout<<"parameter 2 is "<<*p2<<endl;
}
例题6.20
#include<iostream>
using namespace std;
void lower(char *);
int main()
{
char ch;
cout<<"input a uppercase character:";
cin>>ch;
lower(&ch);
cout<<"converted character:";
cout<<ch<<endl;
return 0;
}
void lower(char *pch)
{
if(*pch>='A'&&*pch<='Z')
*pch+='a'-'A';
}
#include<iostream>
using namespace std;
void swap(int*,int*);
int main()
{
int a=3,b=4;
cout<<"交换前的数据为:";
cout<<"a= "<<a<<"\tb= "<<b<<endl;
swap(&a,&b);
cout<<"交换后的数据为:";
cout<<"a= "<<a<<"\tb= "<<b<<endl;
return 0;
}
void swap(int *u,int *v)
{
int temp= *u;
*u=*v;
*v=temp;
}
例题6.22
#include<iostream>
using namespace std;
void input(int *s,int n);
int min(int *s,int n);
int main()
{
int a[10],small;
input(a,10);
small=min(a,10);
cout<<"the minimum is:"<<small<<endl;
return 0;
}
void input(int *s,int n)
{
cout<<"please input"<<n<<"integers:"<<endl;
for(int i=0;i<n;i++)
cin>>s[i];
}
int min(int *s,int n)
{
int min,i;
min= *s;
for(i=1;i<n;i++)
if(s[i]<min)
min=s[i];
return min;
}
例题6.23
#include<iostream>
using namespace std;
int main()
{
int func(int a,int b);
int (*pf)(int a,int b);
pf=func;
cout<<"please input two integers:"<<endl;
int m,n;
cin>>m>>n;
int result=(*pf)(m,n);
cout<<"the result is "<<result<<endl;
return 0;
}
int func(int a,int b)
{
return a+b;
}
例题6.24
#include<iostream>
using namespace std;
int max(int,int);
int min(int,int);
int add(int,int);
void process(int,int,int(*fun)(int,int));
int main()
{
int a,b;
cout<<"enter a and b:"<<endl;
cin>>a>>b;
cout<<"max= ";
process(a,b,max);
cout<<"min= ";
process(a,b,min);
cout<<"sum= ";
process(a,b,add);
return 0;
}
int max(int x,int y)
{
int z;
if (x>y)z=x;
else z=y;
return z;
}
int min(int x,int y)
{
int z;
if(x<y)z=x;
else z=y;
return z;
}
int add(int x,int y)
{
int z;
z=x+y;
return z;
}
void process(int x,int y,int(*fun)(int,int))
{
int result;
result=(*fun)(x,y);
cout<<result<<endl;
}
习题1
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
a[i]=2*(i+1);
for(i=0;i<10;i++)
cout<<setw(4)<<a[i];
cout<<endl;
return 0;
}
习题2
#include<iostream>
using namespace std;
void lower(char *);
int main()
{
char ch;
cout<<"input a uppercase character:";
cin>>ch;
lower(&ch);
cout<<"converted character:";
cout<<ch<<endl;
return 0;
}
void lower(char *pch)
{
if(*pch>='A'&&*pch<='Z')
*pch+='a'-'A';
}
习题3
#include<iostream>
using namespace std;
int main()
{
char a[20],*p;
cout<<"please input a string:";
cin.get(a,20);
for(p=a;*p!='\0';p++)
if(*p!=' ')
cout<<*p;
cout<<endl;
return 0;
}
习题4
#include<iostream>
using namespace std;
int main()
{
char *monthname[]={"January","February","March","April","May","June" ,
"July","August","September","October","November","December"};
int i;
while(1)
{
cout<<"please input month No.:";
cin>>i;
if(i<1||i>12)
break;
cout<<"month No."<<i<<"-->"<<monthname[i-1]<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a[2][3],*p[2],**pp;
int i,j;
p[0]=&a[0][0];
p[1]=&a[1][0];
pp=p;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=j+i;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]:";
cout<<*(*(pp+i)+j)<<endl;
}
return 0;
}