使用指针交换整数
Swapping Integers using pointer
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()
。 正如说明告诉您的那样。
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- 在两个整数之间交换最右边的N位
- 使用指针交换整数
- 给定一个无符号整数,很容易交换每个偶数位和奇数位。这可以推广到交换每个偶数和奇数n位吗?
- 编写一个功能,该函数将使用框架交换两个整数
- 为什么它不交换数组中的两个整数?
- 为什么 a = (a+b) - (b=a) 是交换两个整数的糟糕选择
- 性能交换整数与双精度
- 通过单行中的XOR交换整数.在c++11中真的允许这样做吗
- 如何编写constexpr交换函数来更改整数的尾数
- 如何在C++从字节数组中提取字节时交换 64 位整数
- 使用指针进行基本整数交换
- 什么是用于交换2个整数变量的C代码
- 交换整数的第I位和第j位
- 如何使用原子增量和比较与交换操作处理整数溢出