(1)指针的基本操作(1)
下面的程序,输入10 100和100 10,均可以输出max=100 min=10,请补充完整程序
- #include <iostream>
- using namespace std;
- int main( )
- {
- int *p1,*p2,a,b,t;
- cin>>a>>b;
- p1=&a;
- p2=&b;
-
-
-
- cout<<"Max="<<a<<" Min="<<b<<endl;
- return 0;
- }
解答:
- #include <iostream>
- using namespace std;
- int main( )
- {
- int *p1,*p2,a,b,t;
- cin>>a>>b;
- p1=&a;
- p2=&b;
-
- if(*p1<*p2)
- {
- t=*p1;
- *p1=*p2;
- *p2=t;
- }
- cout<<"Max="<<a<<" Min="<<b<<endl;
- return 0;
- }
运行结果:
(2)指针的基本操作(2)
- 下面的程序,输入10 100和100 10,均可以输出max=100 min=10,请补充完整程序
- #include <iostream>
- using namespace std;
- int main( )
- {
- int *p1,*p2,t;
-
-
-
-
- cin>>*p1>>*p2;
-
-
-
- cout<<"Max="<<*p1<<" Min="<<*p2<<endl;
-
-
-
- return 0;
- }
解答:
- #include <iostream>
- using namespace std;
- int main( )
- {
- int *p1,*p2,t;
-
- p1=new int;
- p2=new int;
-
- cin>>*p1>>*p2;
-
- if(*p1<*p2)
- {
- t=*p1;
- *p1=*p2;
- *p2=t;
- }
- cout<<"Max="<<*p1<<" Min="<<*p2<<endl;
-
- delete p1;
- delete p2;
- return 0;
- }
运行结果: