不正确的排序和/或搜索通过外部文件填充的数组

时间:2014-03-24 19:56:03

标签: c++ arrays algorithm filestream

所以问题是我正在提取一个样本文件,要求输入一个用户帐号。现在,输出是:

What is the filename of account numbers? sample.txt
What is the account number you are looking for? 5552012
-858993460
4520125
5658845
7895122
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
The account number you were looking for is: 1
Press any key to continue . . .

下面输出的数字:“帐号是什么?”由于'for'循环用于调试目的。输出的问题是顶部数字(-858993460)不是sample.txt文件中存在的数字。应该在那里的数字是5658845。我猜测负数是最小的可能。

这部分是我正在使用的代码。似乎冒泡排序算法不能正常工作。但是,按照指示,我们将遵循我所做的书籍示例。我已经多次检查算法函数并发现没有错误,但我的评估可能出错了。此问题导致更大的问题,导致无法在已排序的数组中找到正确的帐号,该数组在当前状态下返回1,这不是存在的数字。

#include <stdafx.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

//prototypes
void sortAcctNums(int accounts[], int arraySize);
bool findAcctNum(int accounts[], int numElems, int accountNumSearched);


//main part of the program. This is where all the magic happens
int main(){
    string fileName;
    int accountNumSearched, count = 0;
    const int arraySize = 18;
    int accounts[arraySize];
    int location;

    fstream inputFile(fileName, ios::in);
    cout << "What is the filename of account numbers? ";
    cin >> fileName;

    inputFile.open(fileName);

    if (inputFile.is_open()){//makes sure the file is able to be read, if not then requests user to try again
        cout << "What is the account number you are looking for? ";
        cin >> accountNumSearched;
        while (inputFile >> accounts[count]){
            count++;//populates the array
        }
        inputFile.close();//closes the file stream

        sortAcctNums(accounts, arraySize);//sorts the account numbers
        location = findAcctNum(accounts, arraySize, accountNumSearched);//assigns the value of the fundAcctNum function to location
        if (location == -1){
            cout << "The account number could not be found" << endl;
            exit;
        }
        else
            cout << "The account number you were looking for is: " << location << endl;
    }
    else
        cout << "Error opening file. Please try again. " << endl;

    return 0;
}

//this function sorts the account numbers with a bubble sort algorithm
void sortAcctNums(int accounts[], int arraySize){

    bool swap;
    int temp;

    do{
        swap = false;
        for (int i = 0; i < (arraySize - 1); i++){
            if (accounts[i] > accounts[arraySize + 1]){
                temp = accounts[i];
                accounts[i] = accounts[arraySize + 1];
                accounts[arraySize + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
    //this loop is only for testing purposes and should be removed during final build
    for (int i = 0; i < arraySize; i++){
        cout << accounts[i] << endl;

    }
}

//This function searches the sorted array for the specified account number with a Binary sort algorithm
bool findAcctNum(int accounts[], int numElems, int accountNumSearched){

    int first = 0,
        last = numElems - 1,
        middle,
        position = -1;
    bool found = false;

    while (!found && first <= last){
        middle = (first + last) / 2;
        if (accounts[middle] == accountNumSearched){
            found = true;
            position = middle;
        }
        else if (accounts[middle] > accountNumSearched)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return position;
}//end search

3 个答案:

答案 0 :(得分:2)

您的冒泡排序完全没有问题。你有:

do{
    swap = false;
    for (int i = 0; i < (arraySize - 1); i++){
        if (accounts[i] > accounts[arraySize + 1]){
            temp = accounts[i];
            accounts[i] = accounts[arraySize + 1];
            accounts[arraySize + 1] = temp;
            swap = true;
        }
    }
} while (swap);

这不是泡泡排序。您需要嵌套的for循环才能进行冒泡排序。回到你的例子,确保你完全实现它。典型的冒泡排序看起来与此相似:

swap = true;
for (int i = 0; i < (arraySize - 1) && swap; ++i)
{
    swap = false;
    for (int j = 0; j < (arraySize - i - 1); ++j)
    {
        if (array[j] > array[j+1])
        {
            temp = array[j];
            array[j] = array[j+1];
            array[j+1] = temp;
            swap = true;
        }
    }
}

您在阵列中获取该虚假号码的原因是您将当前项目(accounts[i])与accounts[arraySize+1]进行比较。在这种情况下,您需要与accounts[20]进行比较。由于阵列中只有18个项目,因此您需要与阵列末尾存储在内存中的某些随机值进行比较。负数不是最小的int。

答案 1 :(得分:1)

正确的计划是:

#include <stdafx.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

//prototypes
void sortAcctNums(int accounts[], size_t count);
int findAcctNum(int accounts[], int numElems, int accountNumSearched);


//main part of the program. This is where all the magic happens
int main(){
    string fileName;
    int accountNumSearched, count = 0;
    const int arraySize = 18;
    int accounts[arraySize];
    int location;

    fstream inputFile(fileName, ios::in);
    cout << "What is the filename of account numbers? ";
    cin >> fileName;

    inputFile.open(fileName);

    if (inputFile.is_open()){//makes sure the file is able to be read, if not then requests user to try again
        cout << "What is the account number you are looking for? ";
        cin >> accountNumSearched;
        while (inputFile >> accounts[count]){
            count++;//populates the array
        }
        inputFile.close();//closes the file stream

        sortAcctNums(accounts, sizeof(accounts)/sizeof(*accounts));//sorts the account numbers
        location = findAcctNum(accounts, count, accountNumSearched);//assigns the value of the fundAcctNum function to location



        if (location == -1){
            cout << "The account number could not be found" << endl;
            exit;
        }
        else
            cout << "The account number you were looking for is: " << accounts[location] << endl
    }
    else
        cout << "Error opening file. Please try again. " << endl;

    return 0;
}

//this function sorts the account numbers with a bubble sort algorithm
void sortAcctNums(int accounts[], size_t count){

    bool swap = true;
    int temp;

    while (count-- && swap){
        swap = false;
        for (size_t i = 0; i < count; ++i){
            if (accounts[i] > accounts[i + 1]){
                std::iter_swap(accounts + i, accounts + i + 1);
                swap = true;
            }
        }
    }
}

//This function searches the sorted array for the specified account number with a Binary search algorithm
int findAcctNum(int accounts[], int numElems, int accountNumSearched){

    int first = 0,
        last = numElems - 1,
        middle,
        position = -1;
    bool found = false;

    while (!found && first <= last){
        middle = (first + last) / 2;
        if (accounts[middle] == accountNumSearched){
            found = true;
            position = middle;
        }
        else if (accounts[middle] > accountNumSearched)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return position;
}//end search

答案 2 :(得分:0)

冒泡排序应如下所示:

for (int i = 0 ; i < (arraySize - 1); i++) {
    for (int j = 0 ; j < arraySize - i - 1; j++) {
        if (array[j] > array[j+1]) {
            int swap   = array[j];
            array[j]   = array[j+1];
            array[j+1] = swap;
        }
    }
}

尝试一下,看看你是否仍然会收到不好的结果。