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



所以问题是,我正在取一个示例文件作为输入,要求输入用户帐号。现在,输出是:

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 . . .

下面输出的数字:"What is The account number?"是由于调试目的的'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

您的冒泡排序根本不正确。你有:

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个元素,因此您将与存储在数组末尾的内存中的某个随机值进行比较。

正确的程序是:

#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

冒泡排序应该是这样的:

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

试一试,看看你是否仍然得到不好的结果。

最新更新