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;
*p=15;
cout<<"a= "<<a<<endl;
cout<<"p= "<<p<<endl;
cout<<"&a= "<<&a<<endl;
cout<<"*p= "<<*p<<endl;
cout<<"&p="<<&p<<endl;
return 0;
}
6-2
#include <iostream>
using namespace std;
int main()
{
int a,b;
int *p=&a,*q=&b,*tp;
cout<<"请输入两个数"<<endl;
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<<"请输入两个数"<<endl;
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,*p1,*p2;
p1=&a,p2=&b;
cout<<boolalpha<<(*p1==*p2)<<endl;
cout<<boolalpha<<(p1==p2)<<endl;
return 0;
}
6-5
#include <iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
a[i]=2*i+1;
cout<<a[i]<<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<<"s1= "<<a<<endl;
cout<<"s2= "<<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="HELLO";
for(int a=0;p[a]!='\0';a++)
cout<<p[a]<<endl;
return 0;
}
6-9
#include<iostream>
using namespace std;
int main()
{
char a[50];
char *p;
cout<<"input what you want to think";
cin.get(a,50);
p=a;
for(; *p!='\0';)
p++;
cout<<"length is: "<<p-a<<endl;
return 0;
}
6-10
#include<iostream>
using namespace std;
int main()
{
int a=100,*b,**c;
b=&a,c=&b;
cout<<&a<<'\t'<<endl;
cout<<&b<<'\t'<<b<<'\t'<<*b<<endl;
cout<<&c<<'\t'<<c<<'\t'<<*c<<'\t'<<**c<<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<<"a["<<i<<"]= "<<*p[i]<<endl;
return 0;
}
6-12
#include<iostream>
using namespace std;
int main()
{
char a[]="come on",b[]="you",c[]="everyone";
char *p[3];
p[0]=b,p[1]=c,p[2]=a;
for(int i=0;i<3;i++)
cout<<p[i]<<endl;
return 0;
}
第九周作业
最新推荐文章于 2023-05-06 23:20:10 发布