在对结构数组的一个成员进行排序后,移动其成员的其余部分
Move the rest of member of an Struct Array after sorting one of its members
我知道有更好、更简单的方法可以做到这一点,但对于我的类来说,这是我唯一可以使用的东西,只是使用我所拥有的东西。
我正在处理一个库存菜单项目,我必须使用气泡排序对"sku"编号进行排序,现在我的问题是,我不知道如何同时移动其他成员。
每个 SKU 编号都有其产品名称、价格和数量。我拥有的代码仅对 sku 编号进行排序并且有效,唯一的问题是其余成员留在同一个地方。
我能做什么?
注意:该功能sortArray
#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;
// Variables
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;
indexSku = lookupSku( products, size, skuChoice );
cout << "Product Name: " << products[indexSku].name << endl
<< "Sku: " << products[indexSku].sku << endl
<< "Quantity: " << products[indexSku].quantity << endl
<< "Price: " << products[indexSku].price << endl;
break;
case LOOKUP_NAME:
cout << "Enter name of product with no spaces: ";
cin >> nameChoice;
cout << endl;
indexName = lookupName ( products, size, nameChoice );
cout << "Product Name: " << products[indexName].name << endl
<< "Sku: " << products[indexName].sku << endl
<< "Quantity: " << products[indexName].quantity << endl
<< "Price: " << products[indexName].price << 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;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if ( product[count].sku > product[count + 1].sku )
{
temp = product[count].sku;
product[count].sku = product[count + 1].sku;
product[count + 1].sku = 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;
}
您需要
交换整个struct
,而不仅仅是成员sku
。
if ( product[count].sku > product[count + 1].sku )
{
inventory temp = product[count];
product[count] = product[count + 1];
product[count + 1] = temp;
swap = true;
}
最简单的方法是保留库存结构的指针数组。在相同条件下对元素进行排序product[count]->sku > product[count + 1]->sku
但不是交换 SKU 值,而是交换product[count]
和product[count+1]
。我希望它有所帮助。您可以在此处看到如何启动指向结构的指针数组
来源: http://www.cplusplus.com/forum/beginner/13778/
相关文章:
- 类成员重新排序
- 如何在 c++11 中静态断言 std::array 类成员进行排序?
- 按类成员的顺序对包含类对象的C++向量进行排序
- 只对向量中的一个类成员进行排序,其他成员保持不变
- 合并 2 个排序的链表。必需的成员函数 bool Merge(List342 &list1):
- 对结构int成员进行排序
- C++排序向量<double>与在<Object>双成员变量上键控的向量
- 如何对类成员向量进行排序
- 如何对同一数组索引下的结构成员进行排序?
- 对多个数据成员进行排序
- 根据类成员对同一密钥的多映射元素进行排序
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- std::具有自定义比较函数结果的排序函数错误:必须调用对非静态成员函数的引用
- 排序链表-移动节点还是交换数据成员
- 如何为涉及对象成员、间接寻址和强制转换的排序算法实现lambda函数
- 使用 #include 按<algorithm>字符串数据成员对类对象的向量进行排序
- 通过私人成员对自定义对象进行排序
- 如何根据其中一个成员的值对结构数组进行排序,从而在另一个成员的基础上断开联系
- 根据数据成员对对象数组进行排序或筛选
- C++列表::按成员排序的类