C ++清单项目删除 [英] C++ inventory item removal

查看:101
本文介绍了C ++清单项目删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 将库存增加到10个项目。 DONE !!!

  • 创建一个for循环,要求用户输入库存中的初始项目。

  • 循环创建一个小故事,治疗师改变两个项目(即项目4和8)。我想要的是SWAP一个项目的另一个项目,即,你想交易你的{item [item2] y或n

  • 按字母顺序对库存进行排序。您可以根据需要使用自己的排序,但这里是一个冒泡排序算法:

 

// A simple inventory program using a struct to store data
// in an array.


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

// define a data structure
struct InventoryRecord
{
  string name;   // inventory item name
  int qty;       // how many are in stock
  double value;   // the value
};

// const for the max size of the record array
const int MAX_SIZE = 9;

// function prototypes
void addData(InventoryRecord list[], int& size);
void dispData(const InventoryRecord list[], int size);
void remData( const InventoryRecord list[], int size);
void saveFile(const InventoryRecord list[], int size);
void openFile(InventoryRecord list[], int& size);
char getMenuResponse();


int main(int argc, char *argv[])
{
  InventoryRecord recList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Hero's Inventory - " << numOfRecs << " items in your bag" << endl;
    switch ( getMenuResponse() ) 
    {
        case 'A': addData(recList, numOfRecs); break; 
        case 'D': dispData(recList, numOfRecs); break;
        case 'R': remData(recList, numOfRecs); break;
        case 'O': openFile(recList, numOfRecs); break;
        case 'S': saveFile(recList, numOfRecs); break;
        case 'Q': run = false; break;
        default : cout << "That is NOT a valid choice" << endl;
    }
  } while (run);
  cout << endl << "Program Terminated" << endl;

  // system("PAUSE"); // Program exits immediatly upon "Quit" if commented out
  return EXIT_SUCCESS;
}

// Task:     Allow data entry of one inventory item
// Accepts:  References to the inventory array and its size
// Returns:  Nothing
// Modifies: The array and size 'actual parameter'
// NOTE:     Could be modified to allow entry of more than one item
void addData(InventoryRecord list[], int& size)
{
  InventoryRecord tmp; // declare a temp item that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Please enter 10 items helpful to your quest! " << endl;
    cout << "Enter item: " << endl << endl;
    cout << "Name:     ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << "Quantity: ";
    cin >> tmp.qty;
    cout << "Value:     ";
    cin >> tmp.value;
    cout << endl;
    // see if this record should be added to the array
    cout << "Add to inventory? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Inventory is full; cannot enter more units." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const InventoryRecord list[], int size)
{
  system("cls");
  double cost = 0;

  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "All Items in your Bag" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Item Name              Qty     Value" << endl;
    cout << "~~~~~~~~~~~~~~~~~~" << endl;

    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(4)  << list[i].qty
           << setw(10) << list[i].value << left << endl;
           cost = cost + list[i].value * list[i].qty;
    }

    cout << "~~~~~~~~~~~~~~~~~~~" << endl;
    cout << right << setw(3) << size;
    cout << " items listed";
    cout << right << setw(19) << cost << endl << endl;
  }

  system("PAUSE");
  system("cls");
}

void remData(const InventoryRecord list[], int size) {
    system("cls");
    cout <<"Enter Item you wish to remove from your inventory: " << endl;// This is being displayed so user can see items in the inventory
    double cost = 0;

  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "All Items in your Bag" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Item Name              Qty     Value" << endl;// It is not displaying right the alignment is off
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    cout <<"Item Name: ";/* from here I do not know what to do! What I want is have use type the item name they want removed
                            also display an error if they enter an  item wrong*/
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(4)  << list[i].qty
           << setw(10) << list[i].value << left << endl;
           cost = cost + list[i].value * list[i].qty;
    }

    cout << "~~~~~~~~~~~~~~~~~~~" << endl;
    cout << right << setw(3) << size;
    cout << " items listed";
    cout << right << setw(19) << cost << endl << endl;
  }}
// Save records to disc
void saveFile(const InventoryRecord list[], int size) {
  ofstream outfi("Inventory.txt");

  // make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving inventory to the disc ";

    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].qty << ';'
            << list[i].value;
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " records writen to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}

// Open file and load array
void openFile(InventoryRecord list[], int& size)
{
  ifstream infi("Inventory.txt");
  string str;
  stringstream strstrm;

  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 

    system("cls");
    cout << "Reading inventory from the disc ";

    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;

      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].qty;

      // get, convert and store the cost
      getline(infi, str); 
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size++].value;
    }
    cout << endl << size << " records read from the disc." << endl;

    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}

char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
    char response;
    cout << endl << "Make your selection" << endl
         << "(A)dd Items, (D)isplay Items, (R)emove items, (O)pen File, (S)ave File, (Q)uit" << endl
         << "> ";
    cin >> response;
    cin.ignore(256, '\n');  
    // clean-up up to 256 chars including the delimiter specified (\n, the endl) 
    // OR stop when the \n is encountered after removing it.
    return toupper(response);

}


推荐答案

基本上两种方法来删除元素,最简单的是用最后一个删除的元素,并调整列表大小:

There are essentially two ways to remove the element, the easiest is swap the element which you want to delete with the last one and resize the list:

void deleteElem(Data[] list, int & listLength, int ix) {
    if (ix < listLength - 1) {
        list[ix] = list[listLength - 1];
    }
    --listLength;
}

第二个解决方案是在向左删除元素后,

The second solution is memmove everthing after the to deleting element one to the left:

void deleteElem(Data[] list, int & listLength, int ix) {
    memmove(list[ix], list[ix + 1], (listLength - ix - 1) * sizeof(Data));
    --listLength;
}

编辑:memmove的长度有一个错误,您应该阅读文档

There was an error in the length for memmove, always the same. You should read the documentation.

这篇关于C ++清单项目删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆