使用指针交换整数

Swapping Integers using pointer

本文关键字:整数 交换 指针      更新时间:2023-10-16

1 : http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment3

我将使用指针在主函数类型之外使用 SwapInteger 函数交换整数。用户输入一个数字,然后计算机将编译并将结果更改为我们教授分配的给定结果。

我尝试创建一个 void swapInteger 函数并输入一些代码以查看它是否交换代码,但这没有任何作用。所以我只是在 main 函数中添加了一些代码,但我认为这不是我们的教授希望我们做的。他确实表示"不要修改主要功能">

#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;
}

我希望计算机编译用户输入的两个整数,然后向用户显示交换的整数。如果您有疑问,请查看我的代码

swapIntegers()内部,您交换的是指针本身,而不是它们指向的变量的值。 调用方的变量未更新。

swapIntegers()需要看起来更像这样:

void swapIntegers(int *first, int *second)
{
int saved = *first;
*first = *second;
*second = saved;
}

另外,你的main()问题。 它动态分配 2 个泄漏的int变量,并且从不将用户的输入值分配给它们。 最后的"After swapping"输出是从这些指针中打印出值,而不是从实际交换的变量中打印出来。 代码不会显示预期的输出。 因此,尽管说明说了什么,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()。 正如说明告诉您的那样。