Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;二进制搜索不';我永远也跑不成功_C++_Binary Search - Fatal编程技术网

C++ C++;二进制搜索不';我永远也跑不成功

C++ C++;二进制搜索不';我永远也跑不成功,c++,binary-search,C++,Binary Search,编辑:为了澄清,我正在搜索的对象数组确实是由正在搜索的变量按字母数字顺序进行预排序的 我制作了一个二进制搜索函数,并将其嵌套在另一个函数中。出于某种原因,每次使用二进制搜索时,它都无法找到相关的字符数组 基本上,二进制搜索应该通过getAccountNumber()的返回值来搜索名为credArray的对象数组中的每个对象。二进制搜索总是返回-1 整个程序编译得很好,只是不能产生正确的结果。使用对象数组的方法也适用于其他地方,因此这不是问题。我知道我犯了一些难以置信的简单错误,但我看不出来。这是

编辑:为了澄清,我正在搜索的对象数组确实是由正在搜索的变量按字母数字顺序进行预排序的

我制作了一个二进制搜索函数,并将其嵌套在另一个函数中。出于某种原因,每次使用二进制搜索时,它都无法找到相关的字符数组

基本上,二进制搜索应该通过getAccountNumber()的返回值来搜索名为credArray的对象数组中的每个对象。二进制搜索总是返回-1

整个程序编译得很好,只是不能产生正确的结果。使用对象数组的方法也适用于其他地方,因此这不是问题。我知道我犯了一些难以置信的简单错误,但我看不出来。这是两个功能:

//This function uses binary search to search for the relevant account
int AccountDB::searchForAccount(char* searchNumber)
{
    int low = 0;
    int high = accountsAmount - 1;
    int mid;
    //now for the loop, using strcmp because these are C strings
    while (low<=high)
    {
        mid = (low + high) / 2;
        if (strcmp(searchNumber,credArray[mid].getAccountNumber()) == 0)
            return mid;
        if (strcmp(searchNumber,credArray[mid].getAccountNumber()) > 0)
            high = mid -1;
        else
            low = mid + 1;
    }
    return -1;
}
//This function records the transactions and writes the output to an external file
void AccountDB::processTransactions(const char* transactFile)
{
    //set up the input stream from the text file
    ifstream inFile;
    //set up the variables to be read from text file
    char date[6];
    char type;
    char accountnumber[20];
    double amount; 

    //open the file
    inFile.open(transactFile);
    //standard check for file and exit if it doesn't exist
    if(!inFile)
    {
        cout << "Error, input file could not be opened.\n";
        exit(1);
    }
    //Creates a header for listing transactions
    cout << setw(5) << "Date"
         << setw(25) << "Account Number"
         << setw(5) << "Type"
         << setw(8) << "Amount"
         << setw(30) << "New Balance"
         << endl;
         inFile >> date;
         while (inFile)
         {
            inFile >> accountnumber >> type >> amount;
            cout << setw(5) << date
                 << setw(25) << accountnumber
                 << setw(5) << type
                 << setw(8) << amount;
            int relevantAccount = searchForAccount(accountnumber);
            cout << "\nThe searchForAccount returned " << relevantAccount << " by the way\n";
            if (relevantAccount != -1)
            {
                if (type == 'P')
                {
                    credArray[relevantAccount].processPayment(amount);
                    cout << setw(30) << credArray[relevantAccount].getBalance() << endl;
                }
                else
                {
                    bool chargestatus = credArray[relevantAccount].processCharge(amount);
                    if (chargestatus = 1)
                        cout << setw(30) << credArray[relevantAccount].getBalance() << endl;
                    else
                        cout << "Credit limit exceeded" << endl;
                }
            }
            else
                cout << "Invalid account number" << endl;   
            inFile >> date;
        }
        cout << "End of transaction list." << endl;
}
//此函数使用二进制搜索来搜索相关帐户
int AccountDB::searchForAccount(字符*searchNumber)
{
int低=0;
int高=accountsAmount-1;
int mid;
//现在,对于循环,使用strcmp,因为它们是C字符串
while(低0)
高=中-1;
其他的
低=中+1;
}
返回-1;
}
//此函数用于记录事务并将输出写入外部文件
void AccountDB::processTransactions(常量字符*事务文件)
{
//从文本文件设置输入流
河流充填;
//设置要从文本文件读取的变量
字符日期[6];
煤焦类型;
字符帐号[20];
双倍金额;
//打开文件
infle.open(文件);
//标准检查文件,如果文件不存在则退出
如果(!infle)
{

cout您每次都取错了一半。(如果strcmp大于零,则搜索项较大,因此您需要查看高一半(即变低)。

您是对的,有一个非常简单的小错误。
strcmp()
当第一个字符串大于第二个字符串时,返回值比较大于0。这意味着您的比较应该翻转

以下是固定代码:

int AccountDB::searchForAccount(char* searchNumber)
{
    int low = 0;
    int high = accountsAmount - 1;
    int mid;
    //now for the loop, using strcmp because these are C strings
    while (low<=high)
    {
        mid = (low + high) / 2;
        if (strcmp(searchNumber,credArray[mid].getAccountNumber()) == 0)
            return mid;
        if (strcmp(searchNumber,credArray[mid].getAccountNumber()) < 0) // this is now "<"
            high = mid -1;
        else
            low = mid + 1;
    }
    return -1;
}
int AccountDB::searchForAccount(char*searchNumber)
{
int低=0;
int高=accountsAmount-1;
int mid;
//现在,对于循环,使用strcmp,因为它们是C字符串

while(low)您确定对
credArray
进行了排序,以便
getAccountNumber()
字符串是按字母顺序排列的?二进制搜索只适用于已排序的数组。在进行此操作之前,您对数组进行了排序吗?是的,我会添加此注释。它是按帐号升序排列的。我想您需要切换对账单
high=mid-1;
low=mid+1;
。我想这也可以实现同样的效果在if/else语句中切换>和<。感谢您的回答和优化!!我知道这只是一件小事,但它确实有帮助。我明白了,我会记住这一点。我最初对strcmp输出感到困惑,这很有帮助
    while (low<=high)
    {
        mid = (low + high) / 2;
        int val = strcmp(searchNumber,credArray[mid].getAccountNumber());
        if (val == 0)
            return mid;
        if (val < 0)
            high = mid -1;
        else
            low = mid + 1;
    }