删除类中的元素的程序如何编写

huangapple go评论55阅读模式
英文:

How to get the program to delete an element from the class

问题

我目前正在学习C++,但无法弄清楚如何在项目列表中的数量变为0后删除项目。

class Item {
private:
    std::string name;
    int quantity;
    float price;

public:
    Item(
            std::string name,
            int quantity,
            float price
    ) :
            name{std::move(name)},
            quantity{quantity},
            price{price} {

    }

    std::string get_name() const {
        return name;
    }

    int get_quantity() const {
        return quantity;
    }

    void set_quantity(int new_quantity) {
        quantity = new_quantity;
    }

    float get_price() const {
        return price;
    }

    bool is_match(const std::string &other) {
        return name == other;
    }
};

class Inventory {
private:
    Item *items[20];
    float total_money;
    int item_count;

    static void display_data(Item &item) {
        std::cout << "\nItem name: " << item.get_name();
        std::cout << "\nQuantity: " << item.get_quantity();
        std::cout << "\nPrice: " << item.get_price();
    }

public:
    Inventory() :
            items{},
            total_money{0},
            item_count{0} {

    }

    void sell_item() {
        std::string item_to_check;
        std::cin.ignore();
        std::cout << "\nEnter item name: ";
        std::cin >> item_to_check;

        for (int i = 0; i < item_count; i++) {
            if (items[i]->is_match(item_to_check)) {
                remove_item(i);
                return;
            }
        }
        std::cout << "\nThis item is not in your Inventory";
    }

    void remove_item(int item_index) {
        int input_quantity;
        Item *item = items[item_index];
        std::cout << "\nEnter number of items to sell: ";
        std::cin >> input_quantity;

        int quantity = item->get_quantity();
        if (input_quantity <= quantity) {
            float price = item->get_price();
            float money_earned = price * input_quantity;
            item->set_quantity(quantity - input_quantity);
            std::cout << "\nItems sold";
            std::cout << "\nMoney received: " << money_earned;
            total_money += money_earned;
        } else {
            std::cout << "\nCannot sell more items than you have.";
        }
    }
};

我尝试使用了来自javatpoint等网站的基本方法,比如使用循环来删除元素,但是不起作用。有关如何删除元素的建议吗?

英文:

I'm currently learning C++ and can't figure out how to get the program to delete an item from the item list once the quantity becomes 0.

class Item {
private:
std::string name;
int quantity;
float price;
public:
Item(
std::string name,
int quantity,
float price
) :
name{std::move(name)},
quantity{quantity},
price{price} {
}
std::string get_name() const {
return name;
}
int get_quantity() const {
return quantity;
}
void set_quantity(int new_quantity) {
quantity = new_quantity;
}
float get_price() const {
return price;
}
bool is_match(const std::string &amp;other) {
return name == other;
}
};
class Inventory {
private:
Item *items[20];
float total_money;
int item_count;
static void display_data(Item &amp;item) {
std::cout &lt;&lt; &quot;\nItem name: &quot; &lt;&lt; item.get_name();
std::cout &lt;&lt; &quot;\nQuantity: &quot; &lt;&lt; item.get_quantity();
std::cout &lt;&lt; &quot;\nPrice: &quot; &lt;&lt; item.get_price();
}
public:
Inventory() :
items{},
total_money{0},
item_count{0} {
}
void sell_item() {
std::string item_to_check;
std::cin.ignore();
std::cout &lt;&lt; &quot;\nEnter item name: &quot;;
std::cin &gt;&gt; item_to_check;
for (int i = 0; i &lt; item_count; i++) {
if (items[i]-&gt;is_match(item_to_check)) {
remove_item(i);
return;
}
}
std::cout &lt;&lt; &quot;\nThis item is not in your Inventory&quot;;
}
void remove_item(int item_index) {
int input_quantity;
Item *item = items[item_index];
std::cout &lt;&lt; &quot;\nEnter number of items to sell: &quot;;
std::cin &gt;&gt; input_quantity;
int quantity = item-&gt;get_quantity();
if (input_quantity &lt;= quantity) {
float price = item-&gt;get_price();
float money_earned = price * input_quantity;
item-&gt;set_quantity(quantity - input_quantity);
std::cout &lt;&lt; &quot;\nItems sold&quot;;
std::cout &lt;&lt; &quot;\nMoney received: &quot; &lt;&lt; money_earned;
total_money += money_earned;
} else {
std::cout &lt;&lt; &quot;\nCannot sell more items than you have.&quot;;
}
}
};

I tried using basic methods from sites like javatpoint like using a loop to remove the element, but it won't work. Any suggestions on how i can remove the elements?

答案1

得分: 2

你无法从固定数组中删除元素。但你可以将数组中后续元素的值向前移动一个位置,然后减少item_count,例如:

void remove_item(int item_index) {
    int input_quantity;
    Item *item = items[item_index];
    std::cout << "\nEnter number of items to sell: ";
    std::cin >> input_quantity;

    int quantity = item->get_quantity();
    if (input_quantity <= quantity) {
        ...
        item->set_quantity(quantity - input_quantity);
        ...
        if (item->get_quantity() <= 0) {
            delete item; // <-- 假设你的Items是用'new'创建的...
            for(int i = item_index + 1; i < item_count; ++i) {
                Items[i-1] = Items[i];
            }
            --item_count;
            Items[item_count] = nullptr;
        }
    } else {
        std::cout << "\n不能卖出比库存更多的物品。";
    }
}

话虽如此,你真的应该使用std::vector而不是固定数组,例如:

#include <vector>
#include <memory>

...

class Inventory {
private:
    std::vector<std::unique_ptr<Item>> items;
    // 不再需要item_count了!
    ...

    ...

public:
    ...

    void sell_item() {
        ...

        for (size_t i = 0; i < items.size(); ++i) {
            if (items[i]->is_match(item_to_check)) {
                remove_item(i);
                return;
            }
        }

        ...
    }

    void remove_item(size_t item_index) {
        int input_quantity;
        auto &item = items[item_index];
        std::cout << "\nEnter number of items to sell: ";
        std::cin >> input_quantity;

        int quantity = item->get_quantity();
        if (input_quantity <= quantity) {
            ...
            item->set_quantity(quantity - input_quantity);
            ...
            if (item->get_quantity() <= 0)
                items.erase(items.begin() + item_index);
        } else {
            std::cout << "\n不能卖出比库存更多的物品。";
        }
    }

    void add_item() {
        ...
        items.push_back(std::make_unique<Item>(name, quantity, price));
    }
};
英文:

You can't remove an element from a fixed array. But, what you can do is move the values of subsequent elements down a slot in the array, and then decrement your item_count, eg:

void remove_item(int item_index) {
int input_quantity;
Item *item = items[item_index];
std::cout &lt;&lt; &quot;\nEnter number of items to sell: &quot;;
std::cin &gt;&gt; input_quantity;
int quantity = item-&gt;get_quantity();
if (input_quantity &lt;= quantity) {
...
item-&gt;set_quantity(quantity - input_quantity);
...
if (item-&gt;get_quantity() &lt;= 0) {
delete item; // &lt;-- assuming your Items are created with &#39;new&#39;...
for(int i = item_index + 1; i &lt; item_count; ++i) {
Items[i-1] = Items[i];
}
--item_count;
Items[item_count] = nullptr;
}
} else {
std::cout &lt;&lt; &quot;\nCannot sell more items than you have.&quot;;
}
}

That being said, you really should be using std::vector instead of a fixed array, eg:

#include &lt;vector&gt;
#include &lt;memory&gt;
...
class Inventory {
private:
std::vector&lt;std::unique_ptr&lt;Item&gt;&gt; items;
// don&#39;t need item_count anymore!
...
...
public:
...
void sell_item() {
...
for (size_t i = 0; i &lt; items.size(); ++i) {
if (items[i]-&gt;is_match(item_to_check)) {
remove_item(i);
return;
}
}
...
}
void remove_item(size_t item_index) {
int input_quantity;
auto &amp;item = items[item_index];
std::cout &lt;&lt; &quot;\nEnter number of items to sell: &quot;;
std::cin &gt;&gt; input_quantity;
int quantity = item-&gt;get_quantity();
if (input_quantity &lt;= quantity) {
...
item-&gt;set_quantity(quantity - input_quantity);
...
if (item-&gt;get_quantity() &lt;= 0)
items.erase(items.begin() + item_index);
} else {
std::cout &lt;&lt; &quot;\nCannot sell more items than you have.&quot;;
}
}
void add_item() {
...
items.push_back(std::make_unique&lt;Item&gt;(name, quantity, price));
}
};

答案2

得分: 0

你可以在释放内存中的项目后使用简单的循环和数组移位。由于数组是静态的,这是唯一的方法。在移除相关项目后,所有元素都需要向后移动一个槽位。

void remove_item(int item_index)
{
    int input_quantity;
    Item *item = items[item_index];
    cout << "\n输入要出售的物品数量: ";
    cin >> input_quantity;

    int quantity = item->get_quantity();
    if (input_quantity <= quantity)
    {
        float price = item->get_price();
        float money_earned = price * input_quantity;
        item->set_quantity(quantity - input_quantity);
        cout << "\n物品已售出";
        cout << "\n收到的钱: " << money_earned;
        total_money += money_earned;
    }
    else
    {
        cout << "\n无法出售比您拥有的物品更多的物品。";
    }
    if (item->get_quantity() == 0)
    {
        removeItemFromList(item);
    }
}

// 当数量为0时,从列表中移除物品
void removeItemFromList(Item *item)
{
    delete item;
    for (int i = 0; i < item_count; i++)
    {
        if (items[i] == item)
        {
            for (int j = i; j < item_count - 1; j++)
            {
                items[j] = items[j + 1];
            }
            item_count--;
            break;
        }
    }
    item = nullptr;
}
英文:

You can use a simple loop and array shifting after freeing the item from the memory. Since the array is static this is the only way. All elements need to be shifted once slot back after removing the concerned item.

void remove_item(int item_index)
{
int input_quantity;
Item *item = items[item_index];
cout &lt;&lt; &quot;\nEnter number of items to sell: &quot;;
cin &gt;&gt; input_quantity;
int quantity = item-&gt;get_quantity();
if (input_quantity &lt;= quantity)
{
float price = item-&gt;get_price();
float money_earned = price * input_quantity;
item-&gt;set_quantity(quantity - input_quantity);
cout &lt;&lt; &quot;\nItems sold&quot;;
cout &lt;&lt; &quot;\nMoney received: &quot; &lt;&lt; money_earned;
total_money += money_earned;
}
else
{
cout &lt;&lt; &quot;\nCannot sell more items than you have.&quot;;
}
if (item-&gt;get_quantity() == 0)
{
removeItemFromList(item);
}
}
// remove item from list once quantity is 0
void removeItemFromList(Item *item)
{
delete item;
for (int i = 0; i &lt; item_count; i++)
{
if (items[i] == item)
{
for (int j = i; j &lt; item_count - 1; j++)
{
items[j] = items[j + 1];
}
item_count--;
break;
}
}
item = nullptr;
}

huangapple
  • 本文由 发表于 2023年3月10日 01:31:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定