指针算术和冒泡排序(C ++)

时间:2017-02-15 05:23:03

标签: c++ pointers math

我似乎无法弄清楚我做错了什么。我今天在课堂上做了笔记,但遗憾的是我的OneNote从今天开始崩溃并删除了我的所有笔记。我知道我遗失了一些东西,但我无法弄清楚。我一直在搜索我的教科书,甚至在网上看了一个多小时。我似乎找不到有用的东西。

我知道我的错误可能是我在if或set变量语句中使用charPtr ++的事实,但我不记得如何做到这一点。如何检查一个元素是否小于另一个元素?

我们应该使用Pointer Arithmetic来编写10个元素的降序冒泡。有人可以解释我做错了什么以及为什么我的程序除了原始数组之外没有输出任何东西?谢谢!

我们也不能使用括号或偏移表示法。

#include <iostream>
#include <fstream>

using namespace std;

int main() {

    // DRIVER PROGRAM

    char *characters, *charPtr = nullptr; // Array or undefined size.
    fstream fs; // file stream for file arithmetic


    fs.open("array.txt", ios::in); // Open the file
    char currentChar; // Used to check if the file can still be read.
    int counter = 0; // Counter to check how many elements are in the array.
    while (fs >> currentChar) { // While data can be put into counter, continue...

            counter++; // Add one to counter

    }

    characters = new char[counter]; // Sets size of array.
    charPtr = characters;

    fs.clear(); // Clears eof flag.
    fs.seekg(0, ios::beg); // Sets pointer back to the beginning. CHECK IF YOU CAN REMOVE THIS LINE AND THE ONE ABOVE.

    for (int i = 0; i < counter; i++) { // While less than the size of array.

        fs >> charPtr; // Write to charPtr
        cout << *charPtr << " "; // Output array.
        charPtr++; // Move to next element

    }

    fs.close(); // Close file
    putchar('\n'); // Output newline efficiently.
    charPtr = characters;

    // BUBBLE SORT

    bool swapChar;
    char temp;

    do {

        swapChar = false;


        for(int count = 0; count < (counter - 1); count++) {

            if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1

                temp = *(charPtr); // Set temp to character at 0
                *(charPtr) = *(charPtr++); // set character at 0 to character at 1
                *(charPtr++) = temp; // set character at 1 to temp
                swapChar = true; // set swap to true 
                cout << *charPtr << " "; // output current swap
                charPtr++; // add 1 to charPtr
            }

        }
    } while (swapChar == true);

}

2 个答案:

答案 0 :(得分:1)

请记住,charPtr++会自行增加。在下面的代码块中,你做了4个增量,你只能期望一个。您应该将其替换为*(charPtr+1)

if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1 
    temp = *(charPtr); // Set temp to character at 0
    *(charPtr) = *(charPtr++); // set character at 0 to character at 1 
    *(charPtr++) = temp; // set character at 1 to temp 
    swapChar = true; // set swap to true 
    cout << *charPtr << " "; // output current swap
    charPtr++; // add 1 to charPtr 
}

答案 1 :(得分:1)

首先有太多++,第二,你应该考虑一些必要的功能,比如交换(这样你就可以独立测试),第三,你不能在每次运行后正确设置charPtr。

if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1
            // swap
            temp = *(charPtr); // Set temp to character at 0
            *(charPtr) = *(charPtr++); // set character at 0 to character at 1
            *(charPtr++) = temp; // set character at 1 to temp
            swapChar = true; // set swap to true 

            cout << *charPtr << " "; // output current swap


            charPtr++; // add 1 to charPtr
        }
}

让我们重写代码

            // swap
            temp = *(charPtr); // Set temp to character at 0
            *(charPtr) = *(charPtr++); // set character at 0 to character at 1
            *(charPtr++) = temp; // set character at 1 to temp

和交换功能

void swap(char *firstPtr, char *secondPtr) {
    char temp = *(charPtr); // Set temp to character at 0
    *(firstPtr) = *(secondPtr++); // set character at 0 to character at 1
    *(secondPtr++) = temp; // set character at 1 to temp
}

这会让它更好一些,但是++仍然是错误的,它们不应该在交换中,因为我们只想交换第一个和第二个。

void swap(char *firstPtr, char *secondPtr) {
    char temp = *(charPtr); // Set temp to character at 0
    *(firstPtr) = *(secondPtr); // set character at 0 to character at 1
    *(secondPtr) = temp; // set character at 1 to temp
}

程序仍然错误

char *nextPtr = charPtr; // moved this out of the condition as we need it later.
        if (*(charPtr) < *(++nextPtr)) { // If character at 0 is less than the character at 1
            // swap
            swap(charPtr, nextPtr);
            swapChar = true; // set swap to true 

            cout << *charPtr << " "; // output current swap

            charPtr++; // add 1 to charPtr
        }

条件对吗?看起来您的排序发生逆转,如果错误,请将<更改为>

最后一次

            charPtr++; // add 1 to charPtr

只有交换时才会这样做...否则你再次检查完全一样。

    char *nextPtr = charPtr;

    if (*(charPtr) > *(++nextPtr)) { // If character at 0 is greater than the character at 1
            // swap
            swap(charPtr, nextPtr);
            swapChar = true; // set swap to true 
    }
    cout << *charPtr << " "; // output current position

    charPtr = nextPtr; // check the next char
}
cout << endl; // change line after each pass through

错误比代码行更多...尝试通过调试器运行它以查找是否还有更多错误。

相关问题