不正确的排序和/或搜索通过一个外部文件填充数组 [英] incorrect sorting and/or searching an array populated through an external file

查看:110
本文介绍了不正确的排序和/或搜索通过一个外部文件填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,问题是,我正在输入询问用户帐号的示例文件。现在,输出是:

 什么是帐户号码的文件名?为sample.txt
什么是你正在寻找的帐号? 5552012
-858993460
4520125
5658845
7895122
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
你正在寻找的帐号为:1
preSS任意键继续。 。 。
 

这是输出下面的数字:这是什么帐号是由于一个for循环进行调试。与输出以及问题是,上面的数字(-858993460)不是数字存在的sample.txt的文件内。这应该是有数目为5658845代替。我猜负数是最小的INT可能的,但。

这部分是code说我正在使用。这似乎是冒泡排序的算法是行不通的完全正确。但是,按照指示,我们都跟着我确实做到了这本书的例子。我检查的算法函数几次,发现没有错误,但我可能是错了我的判断。这个问题导致了更大的问题,它prevents被发现的有序排列,这在它的当前状态返回1,这是不是一个数字,存在于正确的帐号。

 的#include< stdafx.h中>
#包括< stdio.h中>
#包括< fstream的>
#包括<字符串>
#包括<算法>
#包括<的iostream>
使用名字空间std;

//原型
无效sortAcctNums(INT帐户[],INT ARRAYSIZE);
布尔findAcctNum(INT账户[],INT为numElems,INT accountNumSearched);


//该计划的主要部分。这是所有魔术发生
诠释的main(){
    字符串文件名;
    INT accountNumSearched,计数= 0;
    const int的ARRAYSIZE = 18;
    INT账户[ARRAYSIZE]
    INT位置;

    fstream的INPUTFILE(文件名,内部监督办公室::中);
    COUT<< 什么是账号的文件名?;
    CIN>>文件名;

    inputFile.open(文件名);

    如果(inputFile.is_open()){//确保该文件能够被读取,如果不是则请求用户再次尝试
        COUT<< 什么是你正在寻找的帐号?;
        CIN>> accountNumSearched;
        而(INPUTFILE>>应收[计数]){
            算上++; //填充数组
        }
        inputFile.close(); //关闭该文件流

        sortAcctNums(帐户,ARRAYSIZE); //排序帐号
        位置= findAcctNum(帐户,ARRAYSIZE,accountNumSearched); //分配fundAcctNum功能定位的价值
        如果(位置== -1){
            COUT<< 该帐号未找到<< ENDL;
            出口;
        }
        其他
            COUT<< 你要找的人的账号是:<<地点<< ENDL;
    }
    其他
        COUT<< 打开文件时出错请稍后再试。<< ENDL;

    返回0;
}

//这个功能与冒泡排序算法排序的账号
无效sortAcctNums(INT帐户[],INT ARRAYSIZE){

    布尔交换;
    INT温度;

    做{
        互换= FALSE;
        的for(int i = 0;我≤(ARRAYSIZE  -  1);我++){
            如果(户口[1]  -  GT;应收[ARRAYSIZE + 1]){
                TEMP =帐户[I]
                帐户[i] =帐户[ARRAYSIZE + 1];
                帐户[ARRAYSIZE + 1] =气温;
                互换= TRUE;
            }
        }
    }而(交换);
    //这个循环是仅用于测试目的,应在最终版本中删除
    的for(int i = 0; I< ARRAYSIZE;我++){
        COUT<<账户[1]  - ;&其中; ENDL;

    }
}

//这个函数搜索指定的帐户数排序阵列,二叉排序算法
布尔findAcctNum(INT账户[],INT为numElems,诠释accountNumSearched){

    INT第一= 0,
        最后=为numElems  -  1,
        中间,
        位置= -1;
    布尔发现= FALSE;

    而(发现和放大器;!&放大器;第一< =上){
        中间=(第一+最后)/ 2;
        如果(户口[中] == accountNumSearched){
            发现= TRUE;
            位置=中间;
        }
        否则,如果(户口[中]> accountNumSearched)
            最后=中间 -  1;
        其他
            第一=中间+ 1;
    }
    返回的位置;
} //结束搜索
 

解决方案

正确的程序是:

 的#include< stdafx.h中>
#包括< stdio.h中>
#包括< fstream的>
#包括<字符串>
#包括<算法>
#包括<的iostream>
使用名字空间std;

//原型
无效sortAcctNums(INT账户[],为size_t计数);
INT findAcctNum(INT账户[],INT为numElems,INT accountNumSearched);


//该计划的主要部分。这是所有魔术发生
诠释的main(){
    字符串文件名;
    INT accountNumSearched,计数= 0;
    const int的ARRAYSIZE = 18;
    INT账户[ARRAYSIZE]
    INT位置;

    fstream的INPUTFILE(文件名,内部监督办公室::中);
    COUT<< 什么是账号的文件名?;
    CIN>>文件名;

    inputFile.open(文件名);

    如果(inputFile.is_open()){//确保该文件能够被读取,如果不是则请求用户再次尝试
        COUT<< 什么是你正在寻找的帐号?;
        CIN>> accountNumSearched;
        而(INPUTFILE>>应收[计数]){
            算上++; //填充数组
        }
        inputFile.close(); //关闭该文件流

        sortAcctNums(帐户,的sizeof(账户)/ sizeof的(*帐户)); //排序帐号
        位置= findAcctNum(帐户,计数,accountNumSearched); //分配fundAcctNum功能定位的价值



        如果(位置== -1){
            COUT<< 该帐号未找到<< ENDL;
            出口;
        }
        其他
            COUT<< 你要找的人的账号是:<<帐户[位置]<< ENDL
    }
    其他
        COUT<< 打开文件时出错请稍后再试。<< ENDL;

    返回0;
}

//这个功能与冒泡排序算法排序的账号
无效sortAcctNums(INT账户[],为size_t计数){

    布尔交换= TRUE;
    INT温度;

    而(count--&安培;&安培;掉期){
        互换= FALSE;
        用于(为size_t I = 0; I<计数;我++){
            如果(户口[1]  -  GT;应收[I + 1]){
                的std :: iter_swap(帐户+我,账号+ I + 1);
                互换= TRUE;
            }
        }
    }
}

//这个函数搜索指定的帐户数排序阵列,二进制搜索算法
INT findAcctNum(INT账户[],INT为numElems,诠释accountNumSearched){

    INT第一= 0,
        最后=为numElems  -  1,
        中间,
        位置= -1;
    布尔发现= FALSE;

    而(发现和放大器;!&放大器;第一< =上){
        中间=(第一+最后)/ 2;
        如果(户口[中] == accountNumSearched){
            发现= TRUE;
            位置=中间;
        }
        否则,如果(户口[中]> accountNumSearched)
            最后=中间 -  1;
        其他
            第一=中间+ 1;
    }
    返回的位置;
} //结束搜索
 

So the problem is that I'm taking a sample file for input asking for a user account number. Now, the output is:

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

The numbers that are output underneath: "What is the account number?" are due to a 'for' loop for debugging purposes. The problem with the output as well is that the top number (-858993460) isn't a number that exists inside the sample.txt file. The number that should be there is 5658845 instead. I'm guessing the negative number is the smallest int possible though.

This part is the code that I'm working with. It seems like the bubble sort algorithm isn't working quite right. But, as instructed, we are to follow the book example which I did exactly. I've checked the algorithm functions several times and found no errors but I could be wrong with my assessment. This issue leads to a larger issue which prevents the correct account number from being found in the sorted array, which in it's current state returns 1, which isn't a number that exists.

#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

解决方案

The correct program is:

#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

这篇关于不正确的排序和/或搜索通过一个外部文件填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆