1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
例题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;
*p=15;
cout<<"a= "<<a<<endl;
cout<<"p= "<<p<<endl;
cout<<"&a= "<<&a<<endl;
cout<<"*p= "<<*p<<endl;
cout<<"&p= "<<&p<<endl;
return 0;
}
例题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<<" "<<endl;
cout<<"从大到小排序后的数为:"<<*p<<" "<<*q<<endl;
return 0;
}
例题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;
}
例题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;
}
例题5
#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;
}
例题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;
}
例题7
#include<iostream>
using namespace std;
int main()
{
char*p="student";
for(;*p!='\0';p++)
cout<<p<<endl;
return 0;
}
例题8
#include<iostream>
using namespace std;
int main()
{
char*p="student";
for(int i=0;p[i]!='\0';i++)
cout<<p[i];
cout<<endl;
return 0;
}
例题9
#include<iostream>
using namespace std;
int main()
{
char s[50];
char*p;
cout<<"please input a string:";
cin.get(s,50);
p=s;
for(;*p!='\0';)
p++;
cout<<"the length is:"<<p-s<<endl;
return 0;
}
例题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;
}
例题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;
}
例题12
#include<iostream>
using namespace std;
int main()
{
char a[]="computer system",b[]="hardware",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;
}
例题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;
}
例题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;
}
例题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;
}
例题16
#include<iostream>
using namespace std;
int main()
{
int a[2][3],*p[2];
int**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++)
pp[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;
}
例题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;
}
例题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;
例题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;
}
例题20
#include<iostream>
using namespace std;
void lower(char*c);
int main()
{
char ch;
cout<<"input a uppercase character:";
cin>>ch;
lower(&ch);
cout<<"convert character:";
cout<<ch<<endl;
return 0;
}
void lower(char*pch)
{
if(*pch>='A'&&*pch<='Z')
*pch+='a'-'A';
}
例题21
#include<iostream>
using namespace std;
void swap(int*u,int*v);
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;
}
例题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;
}
例题23
#include<iostream>
using namespace std;
int main()
{
int fun(int a,int b);
int (*pf)(int a,int b);
pf=fun;
cout<<"please input two integers:"<<endl;
int m,n;
cin>>m>>n;
int result=(*pf)(m,n);
return 0;
}
int func(int a,int b)
{
return a+b;
}
例题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<<"mim=";
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;
}