我撰寫了一個程式,它應該執行以下操作:
從庫存檔案中讀取產品專案資料并將它們添加到矢量物件中。該程式將允許用戶查看、搜索和訂購產品。庫存檔案應在訂購任何物品后更新。
- 對于操作#2(搜索)和#3(排序),應顯示適當的訊息以輸入無效的專案名稱,然后程式應繼續處理下一個操作
預期輸出:
>: 2
Enter the item name to search: table
table not found.
>: 2
Enter the item name to search: Microwave
Microwave is in stock.
- 對于操作#3(訂購),程式應驗證輸入的訂單數量并確保庫存中有足夠的物品。否則,顯示錯誤訊息并跳過排序 -
預期輸出:
>: 3
Enter the name of the item: Microwave
Enter the nuber of items: 100
Insufficient inventory.
>: 3
Enter the name of the item: Microwave
Enter the nuber of items: 2
Total cost is $300
>: 3
Enter the name of the item: table
table not found.
但是當我嘗試使用二進制搜索按名稱搜索專案的名稱時,它并沒有給我想要的結果,例如:
1.
>: 2
Enter the item name to search: Cooking Range
Cooking Range not found.
>: 2
Enter the item name to search: Circular Saw
Circular Saw not found.
這是包含資料的檔案:
專案資料.txt
1111
Dish Washer
20 550.5
2222
Microwave
75 150
3333
Cooking Range
50 850
4444
Circular Saw
150 125
這是我嘗試實作按名稱搜索專案的函式的定義:
int
searchItemByName(vector<Item> items, string searchName)
{
int low, high, mid;
low = 0;
high = items.size() - 1;
while (low <= high) {
mid = (low high) / 2;
if (items[mid].getName() == searchName)
return (mid);
else if (items[mid].getName() > searchName)
high = mid - 1;
else
low = mid 1;
}
return (-1);
}
這是整個程式(我還添加了一個更清晰、更廣泛的“描述”,說明程式應該如何處理一些測驗。):
https://github.com/jrchavez07/project_07
uj5u.com熱心網友回復:
我檢查了你的 GitHub 專案。
我認為您必須在二進制搜索之前對資料進行排序。
示例文本檔案串列中的當前資料如下:
Dish Washer
Microwave
Cooking Range
Circular Saw
而且這些都沒有排序,所以你不能使用二分搜索。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457130.html
上一篇:回圈@with_kw結構
下一篇:無法加載資產:圖片
