返回列表 發帖

指標 (三)

本帖最後由 tonyh 於 2014-9-13 17:31 編輯

利用指標法 (變更門牌), 將變數x與變數y的值對調.

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10, y=5;
  7.      int *xPtr=&x, *yPtr=&y;  //宣告指標xPtr與yPtr,同時將x與y的位址指派給它們
  8.      cout<<"[對調前]"<<endl;
  9.      cout<<"x="<<*xPtr<<endl;
  10.      cout<<"y="<<*yPtr<<endl;
  11.      cout<<"變數x的位址: "<<xPtr<<endl;
  12.      cout<<"變數y的位址: "<<yPtr<<endl<<endl;
  13.      xPtr=&y;
  14.      yPtr=&x;
  15.      cout<<"[對調後]"<<endl;
  16.      cout<<"x="<<*xPtr<<endl;
  17.      cout<<"y="<<*yPtr<<endl;
  18.      cout<<"變數x的位址: "<<xPtr<<endl;
  19.      cout<<"變數y的位址: "<<yPtr<<endl<<endl;
  20.      system("pause");
  21.      return 0;
  22. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10,y=5;
  7.     int *xPtr,*yPtr;
  8.     xPtr=&x;
  9.     yPtr=&y;
  10.     cout<<"[對調前]"<<endl;
  11.     cout<<"x="<<*xPtr<<endl;
  12.     cout<<"y="<<*yPtr<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數y的位址: "<<yPtr<<endl<<endl;
  15.     xPtr=&y;
  16.     yPtr=&x;
  17.     cout<<"[對調後]"<<endl;
  18.     cout<<"x="<<*xPtr<<endl;
  19.     cout<<"y="<<*yPtr<<endl;
  20.     cout<<"變數x的位址: "<<xPtr<<endl;
  21.     cout<<"變數y的位址: "<<yPtr<<endl;
  22.     system("pause");   
  23.     return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x = 10, y = 5;
  7.      int *xPtr,*yPtr;
  8.      xPtr=&x;
  9.      yptr=&y;
  10.      cout<<"對調前  "<<x<<endl;
  11.      cout<<"x=:  "<<*xPtr<<endl;
  12.      cout<<"y=:  "<<*yPtr<<endl;
  13.      cout<<"變數x的地址"<<xPtr<<endl;
  14.      cout<<"變數y的地址"<<yPtr<<endl;
  15.      xPtr=&y;
  16.      yptr=&x;
  17.      cout<<"對調後  "<<x<<endl;
  18.      cout<<"x=:  "<<*xPtr<<endl;
  19.      cout<<"y=:  "<<*yPtr<<endl;
  20.      cout<<"變數x的地址"<<xPtr<<endl;
  21.      cout<<"變數y的地址"<<yPtr<<endl;
  22.      system("pause");
  23.      return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10,y=5;
  7.     int *xPtr,*yPtr;
  8.     xPtr=&x;
  9.     yPtr=&y;
  10.     cout<<"[對調前]"<<endl;
  11.     cout<<"x="<<xPtr<<endl;
  12.     cout<<"y="<<yPtr<<endl;
  13.     cout<<"變數x的位置: "<<xPtr<<endl;
  14.     cout<<"變數y的位置: "<<yPtr<<endl;
  15.     xPtr=&y;
  16.     yPtr=&x;
  17.     cout<<"[對調後]"<<endl;
  18.     cout<<"x="<<xPtr<<endl;
  19.     cout<<"y="<<yPtr<<endl;
  20.     cout<<"變數x的位置: "<<xPtr<<endl;
  21.     cout<<"變數y的位置: "<<yPtr<<endl;
  22.     system("pause");
  23.     return 0;  
  24. }  
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10, y=5;
  7.     int *xPtr, *yPtr;
  8.     xPtr=&x;
  9.     yPtr=&y;
  10.     cout<<"[對調前]"<<endl;
  11.     cout<<"x="<<*xPtr<<endl;
  12.     cout<<"y="<<*yPtr<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數y的位址: "<<yPtr<<endl;
  15.     xPtr=&y;
  16.     yPtr=&x;
  17.     cout<<"[對調後]"<<endl;
  18.     cout<<"x="<<*xPtr<<endl;
  19.     cout<<"y="<<*yPtr<<endl;
  20.     cout<<"變數x的位址: "<<xPtr<<endl;
  21.     cout<<"變數y的位址: "<<yPtr<<endl;
  22.     system("pause");  
  23.     return 0;        
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     int x=10,y=5;
  8.     int *xPtr, *yPtr;
  9.     xPtr=&x;
  10.     yPtr=&y;
  11.     cout<<"[對調前]"<<endl;
  12.     cout<<"x= "<<*xPtr<<endl;
  13.     cout<<"y= "<<*yPtr<<endl;
  14.     cout<<"變數x的位址: "<<xPtr<<endl;
  15.     cout<<"變數y的位址: "<<yPtr<<endl;
  16.     xPtr=&y;
  17.     yPtr=&x;
  18.     cout<<"[對調後]"<<endl;
  19.      cout<<"[對調前]"<<endl;
  20.     cout<<"x= "<<*xPtr<<endl;
  21.     cout<<"y= "<<*yPtr<<endl;
  22.     cout<<"變數x的位址: "<<xPtr<<endl;
  23.     cout<<"變數y的位址: "<<yPtr<<endl;
  24.     xPtr=&y;
  25.     yPtr=&x;
  26.     system("pause");     
  27.     return 0;   
  28. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     int x, y;
  7.     x = 10;
  8.     y = 5;
  9.     int *xPtr, *yPtr;
  10.     xPtr = &x;
  11.     yPtr = &y;
  12.     cout << "[對調前]" << endl;
  13.     cout << " x = :" << *xPtr << endl;
  14.     cout << " y = :" << *yPtr << endl;
  15.     cout << "變數x的地址 :" << xPtr << endl;
  16.     cout << "變數y的地址 :" << yPtr << endl;
  17.     xPtr = &y;
  18.     yPtr = &x;
  19.     cout << "[對調後]" << endl;
  20.     cout << " x = :" << *yPtr << endl;
  21.     cout << " y = :" << *xPtr << endl;
  22.     cout << "變數x的地址 :" << yPtr << endl;
  23.     cout << "變數y的地址 :" << xPtr << endl;
  24.     system("pause");
  25.     return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10, y=5;
  7.     int *xPtr,*yPtr;
  8.     xPrt=&x;
  9.     yPrt=&y;
  10.     cout<<"[對調前]"<<endl;
  11.     cout<<"x="<<*xPtr<<endl;
  12.     cout<<"y="<<*yPtr<<endl;
  13.     cout<<"變數x的位址:"<<xPtr<<endl;
  14.     cout<<"變數y的位址:"<<yPtr<<endl;
  15.     xPtr=&y;
  16.     yPtr=&x;
  17.     cout<<"[對調後]"<<endl;
  18.     cout<<"x="<<*xPtr<<endl;
  19.     cout<<"y="<<*yPtr<<endl;
  20.     cout<<"變數x的位址:"<<xPtr<<endl;
  21.     cout<<"變數y的位址:"<<yPtr<<endl;
  22.     system("pause");
  23.     return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10,y=5;
  7.      int* xPtr=&x, *yPtr=&y;  
  8.       
  9.      
  10.            cout<<"[對調前]"<<endl;
  11.         cout<<"x="<<*xPtr<<endl;
  12.     cout<<"y="<<*yPtr<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數y的位址: "<<yPtr<<endl<<endl;
  15.     xPtr=&y;
  16.     yPtr=&x;
  17.     cout<<"[對調後]"<<endl;
  18.     cout<<"x="<<*xPtr<<endl;
  19.     cout<<"y="<<*yPtr<<endl;
  20.     cout<<"變數x的位址: "<<xPtr<<endl;
  21.     cout<<"變數y的位址: "<<yPtr<<endl<<endl;

  22.      
  23.      system("pause");
  24.      return 0;
  25. }
複製代碼

TOP

  1. #include <bits/stdc++.h>

  2. using namespace std;



  3. int main()
  4. {
  5.     int x = 10, y = 100, *xptr = &x, *yptr = &y;
  6.     cout << "x=" << *xptr << endl;
  7.     cout << "y=" << *yptr << endl;
  8.     cout << "x位置: " << xptr << endl;
  9.     cout << "y位置: " << yptr << endl;
  10.     swap(xptr ,yptr);
  11.     cout << "x=" << *xptr << endl;
  12.     cout << "y=" << *yptr << endl;
  13.     cout << "x位置: " << xptr << endl;
  14.     cout << "y位置: " << yptr << endl;
  15.     return 0;
  16. }
複製代碼

TOP

返回列表