C++入门经典-例5.2-使用指针比较两个数的大小
1:代码如下:
// 5.2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int *p1,*p2;
int *p; //临时指针
int a,b;
cout << "input a: " << endl;
cin >> a;
cout << "input b: " << endl;
cin >> b;
p1=&a;p2=&b;
if(a<b)
{
p=p1;
p1=p2;
p2=p;
}
cout << "a=" << a;
cout << " ";
cout << "b=" << b;
cout << endl;
cout << "较大的数:" << *p1 << "较小的数: "<< *p2 <<endl;
}
View Code
运行结果: