[英]Swapping Integers using pointer
1: http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment3
我將使用指針在主 function 類型之外使用 SwapInteger function 交換 integer 。 用戶輸入一個數字,然后計算機將編譯並將結果更改為我們教授分配的給定結果。
我嘗試創建一個 void swapInteger function 並輸入一些代碼以查看是否交換了代碼,但這無濟於事。 所以我只是在主要的 function 中添加了一些代碼,但我認為這不是我們的教授希望我們做的。 他確實說過“不要修改主要功能”
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
// TODO: Implement the "SwapIntegers" function
void swapIntegers(int *first, int *second)
{
int *pSwapIntegers = first;
first = second;
second = pSwapIntegers;
}
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
int *pFirst = new int (first);
int *pSecond = new int (second);
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
我希望計算機編譯用戶輸入的兩個 integer,然后將交換的 integer 顯示給用戶。 如果您有任何問題,請查看我的代碼
在您的swapIntegers()
內部,您正在交換指針本身,而不是它們指向的變量的值。 調用者的變量沒有被更新。
swapIntegers()
需要看起來更像這樣:
void swapIntegers(int *first, int *second)
{
int saved = *first;
*first = *second;
*second = saved;
}
此外,您的main()
是錯誤的。 它動態分配它泄漏的 2 個int
變量,並且從不分配用戶的輸入值。 最終的"After swapping"
output 打印出這些指針的值,而不是實際交換的變量。 該代碼不會顯示預期的 output。 所以,不管說明書怎么說, main()
需要修改才能正常運行,如果你的教授對此有疑問,那就很難了。 他在給你的代碼中犯了一個錯誤。
main()
應該看起來更像這樣:
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
或者,像這樣:
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
int *pFirst = &first;
int *pSecond = &second;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
或者,像這樣:
int main()
{
int *pFirst = new int (0);
int *pSecond = new int (0);
cout << "Enter the first integer: ";
cin >> *pFirst;
cout << "Enter the second integer: ";
cin >> *pSecond;
cout << "\nYou entered:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
swapIntegers(pFirst, pSecond);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
delete pFirst;
delete pSecond;
cout << "\nPress any key to quit.";
_getch();
return 0;
}
更新:哦等等,這不是你教授的錯,而是你的錯。 您在此處提供的main()
() 與實際作業中給出的main()
不匹配! . 這是原始main()
的樣子:
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
SwapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
這段代碼是正確的。 因此,您是在main()
中引入了錯誤使用指針的人。 因此,只需恢復為您提供的原始main()
代碼。 然后正確實施swapIntegers()
。 正如指示告訴你的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.