字符串的二进制搜索无法正常工作
Binary Search for String not working correctly
以下程序是一个库存菜单。出于某种原因,一切似乎都可以正常工作,除非我搜索产品名称(选项 3(功能lookupName
。在我设置一个条件之前,如果没有返回任何内容,它会起作用,以给出错误消息,这与我用于lookupSku
的相同,并且该消息工作正常。我不确定代码出了什么问题。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct inventory {
string name;
int sku;
int quantity;
double price;
};
int size = 0;
void fillArray ( inventory[], int&, ifstream& );
void sortArray ( inventory[], int );
void displayIn ( inventory[], int );
int lookupSku ( inventory[], int, int );
int lookupName ( inventory[], int, string );
int main(){
// Constants for menu choices
const int DISPLAY_INVENTORY = 1,
LOOKUP_SKU = 2,
LOOKUP_NAME = 3,
QUIT_CHOICE = 4;
int choice;
inventory products [100];
ifstream fin;
fin.open ("inventory.dat");
if (!fin)
{
cout << endl << endl
<< " ***Program Terminated. *** " << endl << endl
<< " Input file failed to open. " << endl;
system ( "PAUSE>NUL" );
return 1;
}
fillArray ( products, size, fin );
sortArray ( products, size );
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
do
{
// Display the menu.
cout << "ntt Manage Inventory Menunn"
<< "1. Display inventory sorted by skun"
<< "2. Lookup a product by skun"
<< "3. Lookup a product by namen"
<< "4. Quit the Programnn"
<< "Enter your choice: ";
cin >> choice;
cout << endl;
// Validate the menu selection.
while (choice < DISPLAY_INVENTORY || choice > QUIT_CHOICE)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
// Validate and process the user's choice.
if (choice != QUIT_CHOICE)
{
int indexSku,
indexName,
skuChoice;
string nameChoice;
switch (choice)
{
case DISPLAY_INVENTORY:
displayIn ( products, size );
break;
case LOOKUP_SKU:
cout << "Enter the Sku number: ";
cin >> skuChoice;
cout << endl;
indexSku = lookupSku( products, size, skuChoice );
if ( indexSku >= 0 )
{
cout << "Product Name: " << products[indexSku].name << endl
<< "Sku: " << products[indexSku].sku << endl
<< "Quantity: " << products[indexSku].quantity << endl
<< "Price: " << products[indexSku].price << endl;
}
else
cout << "No product found with this sku!" << endl;
break;
case LOOKUP_NAME:
cout << "Enter product name with no spaces: ";
cin >> nameChoice;
cout << endl;
indexName = lookupName( products, size, nameChoice );
if ( indexName >= 0 )
{
cout << "Product Name: " << products[indexName].name << endl
<< "Sku: " << products[indexName].sku << endl
<< "Quantity: " << products[indexName].quantity << endl
<< "Price: " << products[indexName].price << endl;
}
else
cout << "No product found with this product name!" << endl;
break;
}
}
} while (choice != QUIT_CHOICE);
fin.close ();
return 0;
}
void fillArray ( inventory product[], int &size, ifstream &fin )
{
int counter = 0;
while (fin >> product[counter].name)
{
fin >> product[counter].sku>> product[counter].quantity
>> product[counter].price;
counter ++;
}
size = counter;
}
void sortArray ( inventory product[], int size )
{
bool swap;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if ( product[count].sku > product[count + 1].sku )
{
inventory temp = product[count];
product[count] = product[count + 1];
product[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void displayIn ( inventory product[], int size )
{
for ( int i = 0; i < size; i++ )
cout << product[i].sku << " " << product[i].quantity << " "
<< product[i].price << " " << setw(4) << product[i].name << endl;
}
int lookupSku(inventory product[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (product[middle].sku == value)
{
found = true;
position = middle;
}
else if (product[middle].sku > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int lookupName ( inventory product[], int size , string value )
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (product[middle].name == value)
{
found = true;
position = middle;
}
else if (product[middle].name > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
使用二进制搜索查找产品名称的代码是正确的,但数据似乎是按 SKU 排序的。 二叉搜索算法仅在数据排序时才有效。
在应用二叉搜索之前,您可以修改程序以按产品名称对数据进行排序,但在这种情况下,由于您使用的是 N^2 搜索时间的气泡排序,因此最好只对产品名称进行线性搜索。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程