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

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM