/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘畅
* 完成日期:2014 年 12 月 3 日
* 版 本 号:v1.0
*
* 问题描述:试试使用了指针的程序;
* 输入描述:啥也不用输;
* 程序输出:输出该输出的。
。
(1)
#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)
#include <iostream>
using namespace std;
int main( )
{
int *p1,*p2,t;
p1=new int (100);
p2=new int (100);
cin>>*p1>>*p2;
if (*p1<*p2)
{
t=*p1;
*p1=*p2;
*p2=t;
}
cout<<"Max="<<*p1<<" Min="<<*p2<<endl;
delete p1;
delete p2;
return 0;
}
运行结果:
(3)
#include <iostream>
using namespace std;
void jiaohuan(int *p1,int *p2);
int main( )
{
int a,b;
cin>>a>>b;
jiaohuan(&a,&b);
cout<<a<<" "<<b<<endl;
return 0;
}
void jiaohuan(int *p1,int *p2)
{
int t;
t=*p1;
*p1=*p2;
*p2=t;
}
运行结果:
(4)
#include <iostream>
using namespace std;
void ast(int x,int y,int *cp,int *dp)
{
*cp=x+y;
*dp=x-y;
}
int main( )
{
int a,b,c,d;
cin>>a>>b;
ast(a,b,&c,&d);
cout<<c<<" "<<d<<endl;
return 0;
}
运行结果:
学习心得:
做完这几个题,基本上是理解指针的运用和作用了。