C ++库存项目拆除 [英] C++ inventory item removal

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

问题描述


  • 增加的库存为10项。 DONE!

  • 创建一个for循环,要求用户输入初始项目在inventory.DONE !!!

  • 之后的for循环创建一个小的故事,那里的医者改变两个项目(即项目4和8)。什么我要的是交换项目的另一个项目,即要交易你{[项目]的[ITEM2 ] y或n

  • 排序按字母顺序排列的库存。您可以使用自己的排序,如果你想要的,但这里是一个冒泡排序算法:

 

  //使用结构来存储数据的简单清查方案
//在数组中。
#包括LT&; cstdlib>
#包括LT&;&iostream的GT;
#包括LT&;&了iomanip GT;
#包括LT&;&的fstream GT;
#包括LT&;串GT;
#包括LT&;&sstream GT;使用命名空间std;//定义的数据结构
结构InventoryRecord
{
  字符串名称; //存货项目名称
  INT数量; //多少有库存
  双重价值; // 价值
};//常量备案阵列的最大尺寸
const int的MAX_SIZE = 9;//函数原型
无效addData(InventoryRecord名单[],INT和放大器;大小);
无效DISPDATA(常量InventoryRecord名单[],INT大小);
无效remData(常量InventoryRecord名单[],INT大小);
无效saveFile的(常量InventoryRecord名单[],INT大小);
虚空中openFile(InventoryRecord名单[],INT和放大器;大小);
CHAR getMenuResponse();
INT主(INT ARGC,CHAR *的argv [])
{
  InventoryRecord recList [MAX_SIZE];
  INT numOfRecs = 0;
  布尔运行= TRUE;
  做
  {
    COUT<< 英雄的库存 - << numOfRecs<< &LT你包里的物品;< ENDL;
    开关(getMenuResponse())
    {
        案例'A':addData(recList,numOfRecs);打破;
        案D:DISPDATA(recList,numOfRecs);打破;
        案例'R':remData(recList,numOfRecs);打破;
        案例'O':中openFile(recList,numOfRecs);打破;
        案例'S':saveFile的(recList,numOfRecs);打破;
        案例'Q':RUN = FALSE;打破;
        默认:COUT<< 这不是一个有效的选择。<< ENDL;
    }
  }而(运行);
  COUT<< ENDL<< 程序终止<< ENDL;  //系统(暂停); //程序退出立刻对跳槽,如果注释掉
  返回EXIT_SUCCESS;
}//任务:放置1存货项目的数据录入
//接受:参考文献去库存化数组,其大小
//返回:无
//修改:数组和尺寸的实际参数
//注意:可以修改,以允许多个项目的进入
无效addData(InventoryRecord名单[],INT和放大器;大小)
{
  InventoryRecord TMP; //声明一个临时的项目,我们将阵列进入前加载
  焦炭反应;
  焦炭海峡[256]; //需要cin.getline;我们将使用字符数组
  如果(大小< MAX_SIZE){
    系统(CLS);
    COUT<< 请输入10个项目有助于你的追求!<< ENDL;
    COUT<< 输入项:<< ENDL<< ENDL;
    COUT<< 名称: ;
    //起床从键盘包括空白256个字符。
    //停止读,如果遭遇\\ n个第一。如果有任何的机会
    //超过256个字符,你将不得不清理与CIN
    // cin.ignore下一输入之前。
    cin.getline(STR,256,'\\ n'); //为字符数组;从其他函数getline不同
    tmp.name =海峡;
    COUT<< 数量;
    CIN>> tmp.qty;
    COUT<< 价值;
    CIN>> tmp.value;
    COUT<< ENDL;
    //查看是否该记录应该被添加到阵列
    COUT<< 添加到库存(Y / N)?
    CIN>>响应;
    如果(TOUPPER(响应)=='Y')
      列表[尺寸++] = tmp目录;
  }其他{
    COUT<< 库存已满;无法进入更多的单位。 << ENDL;
    系统(暂停);
  }
  系统(CLS);
}无效DISPDATA(常量InventoryRecord名单[],INT大小)
{
  系统(CLS);
  双成本= 0;  如果(大小和。1){
    COUT<< 没什么,显示<< ENDL;
  }其他{
    COUT<< &LT在你的包全部项目;< ENDL<< ENDL;
    COUT<<固定<<集precision(2);
    COUT<< 项目名称数量值<< ENDL;
    COUT<< ~~~~~~~~~~~~~~~~~~<< ENDL;    COUT<<剩下;
    的for(int i = 0; I<大小;我++){
      COUT<<运输及工务局局长(21)LT;<列表[我]。名称<<对
           <<运输及工务局局长(4)及所述;&下;名单[I] .qty
           <<运输及工务局局长(10)所述;&下;名单[I] .value的<<左<< ENDL;
           成本=成本+列表[I] .value的*名单[I] .qty;
    }    COUT<< ~~~~~~~~~~~~~~~~~~~<< ENDL;
    COUT<<右LT;<运输及工务局局长(3)所述;&下;尺寸;
    COUT<< 项目列入;
    COUT<<右LT;<运输及工务局局长(19)所述;&下;成本<< ENDL<< ENDL;
  }  系统(暂停);
  系统(CLS);
}无效remData(常量InventoryRecord名单[],INT大小){
    系统(CLS);
    COUT<<输入您想从你的清单中移除项目:<< ENDL; //这是正在显示,从而用户可以在库存见项目
    双成本= 0;  如果(大小和。1){
    COUT<< 没什么,显示<< ENDL;
  }其他{
    COUT<< &LT在你的包全部项目;< ENDL<< ENDL;
    COUT<<固定<<集precision(2);
    COUT<< 项目名称数量值<< ENDL; //这是不正确显示对齐是关闭
    COUT<< ~~~~~~~~~~~~~~~~~~<< ENDL;
    COUT<<产品名称:; / *从这里,我不知道该怎么办!我要的是有利用类型,他们希望删除的项目名称
                            如果他们进入一个项目也错误显示错误* /
    COUT<<剩下;
    的for(int i = 0; I<大小;我++){
      COUT<<运输及工务局局长(21)LT;<列表[我]。名称<<对
           <<运输及工务局局长(4)及所述;&下;名单[I] .qty
           <<运输及工务局局长(10)所述;&下;名单[I] .value的<<左<< ENDL;
           成本=成本+列表[I] .value的*名单[I] .qty;
    }    COUT<< ~~~~~~~~~~~~~~~~~~~<< ENDL;
    COUT<<右LT;<运输及工务局局长(3)所述;&下;尺寸;
    COUT<< 项目列入;
    COUT<<右LT;<运输及工务局局长(19)所述;&下;成本<< ENDL<< ENDL;
  }}
//保存记录到光盘
无效saveFile的(常量InventoryRecord名单[],INT大小){
  的ofstream outfi(Inventory.txt);  //确保文件流是在做IO前开放
  如果(!outfi.fail()){
    系统(CLS);
    COUT<< 库存保存到光盘;    的for(int i = 0; I<大小;我++){
      outfi<<列表[我]。名称<< ';'
            <<名单[I] .qty<< ';'
            <<名单[I] .value的;
      //毕竟,但是最后一个记录开始一个新行
      //,简化读取文件EOF是在最后一行的末尾
      如果(I<大小-1)outfi<< ENDL;
    }
    COUT<< ENDL<<大小和LT;< 记录writen到光盘上。 << ENDL;
    outfi.close();
    系统(暂停);
    系统(CLS);
  }
  其他{
    COUT<< 错误:与文件的问题<< ENDL;
    系统(暂停);
    系统(CLS);
  }
}//打开文件和负载阵列
虚空中openFile(InventoryRecord名单[],INT和放大器;大小)
{
  ifstream的INFI(Inventory.txt);
  字符串str;
  stringstream的strstrm;  //确保文件流是在做IO前开放
  如果(!infi.fail()){    系统(CLS);
    COUT<< 从光盘读取库存;    大小= 0; //覆盖所有现有记录
    而(infi.eof()及!&安培;大小< MAX_SIZE){
      //获取并存储该名称
      函数getline(INFI,STR,';');
      列表[大小]。名称= str中;      //获取,转换和存储量
      函数getline(INFI,STR,';');
      strstrm.str(); strstrm.clear(); //空并清除stringstream的
      strstrm<<海峡;
      strstrm>>列表【规格】.qty;      //获取,转换和存储成本
      函数getline(INFI,STR);
      strstrm.str(); strstrm.clear(); //空并清除stringstream的
      strstrm<<海峡;
      strstrm>> [尺寸++]列表值;
    }
    COUT<< ENDL<<大小和LT;< 的记载从光盘读取。 << ENDL;    系统(暂停);
    系统(CLS);
  }
  其他{//出了点问题打开文件
    COUT<< 错误:与文件的问题<< ENDL;
    系统(暂停);
    系统(CLS);
  }}CHAR getMenuResponse()
//任务:将屏幕上的菜单,并得到回应
//接受:没有
//返回:用户响应
//修改:无
//注意:字符是远远在命令更为直观
//行比数;避免使用数字。
{
    焦炭反应;
    COUT<< ENDL<< 请您选择<< ENDL
         << (A)DD项目,(D)isplay项,(R)emove项目(O)笔文件,(S)AVE文件,(Q)UIT<< ENDL
         << >中;
    CIN>>响应;
    cin.ignore(256,'\\ n');
    //清理多达256个字符,包括指定的分隔符(\\ n,则ENDL)
    //或停止时,\\ n被取出后遇到过。
    返回在toupper(响应);}


解决方案

基本上有两种方法可以删除元素,最简单的就是交换,你要删除的最后一个和调整列表中的元素:

 无效deleteElem(数据[]列表中,INT和放大器; listLength,诠释九){
    如果(九< listLength - 1){
        列表[九] =列表[listLength - 1];
    }
    --listLength;
}

第二个解决方案是后的memmove全部的寄托在删除元素之一左

 无效deleteElem(数据[]列表中,INT和放大器; listLength,诠释九){
    memmove与(名单[九]列表[IX + 1],(listLength - 九 - 1)* sizeof的(数据));
    --listLength;
}

编辑:有一个在长度的误差为的memmove,总是相同的。你应该阅读文档

  • Increase the inventory to be 10 items. DONE!!!
  • Create a for loop that asks the user to input the initial items in the inventory.DONE!!!
  • After the for loop create a minor story where the healer changes two items (i.e. items 4 and 8).What I want is to SWAP an item for another item ie would you like to trade your {[item] for [item2] y or n
  • Sort the inventory in alphabetical order. You can use your own sort if you want, but here is a bubble sort algorithm:

 

// 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;
}

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

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

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