繁体   English   中英

使用指针交换整数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM