#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include<conio.h>
#include <iomanip>
#include "Project1.h"
using namespace std;
list<Goods> goods;
void PrintMenu() {
cout << "1 : 新增商品\n2 : 增加数量\n3 : 生成增加后表文件\n4 : 删除商品\n5 : 减少数量\n6 : 生成减少后表文件\n7 : 展示表\n8 : 查找某商品\n9 : 退出\n" << endl;
cout << "please enter an integer from 1 to 9" << endl;
return ;
}
list<Goods>::iterator match(char name[]) {
list<Goods>::iterator iter;
for (iter = goods.begin(); iter != goods.end(); iter++)
if (strcmp(iter->name, name) == 0)
return iter;
return iter = goods.end();
}bool add_goods(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter == goods.end()) {
Goods temp;
temp.count = count;
strcpy(temp.name, name);
goods.push_back(temp);
cout << "\nAdd sucessfully" << endl;
}
else {
iter->count += count;
}
return 1;
}
void AddGoods() {
cout << "please enter the good's name" << endl;
char* temp=new char[100]();
cin.getline(temp, 100);
int count;
cout << "please enter the accountment" << endl;
cin >> count;
add_goods(temp, count);
delete[] temp;
cout << "please enter your integer" << endl;
}
bool increase_count(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter == goods.end()) {
cout << "this kind of good dosn't exist" << endl;
return 0;
}
else {
iter->count += count;
return 1;
}
}
void IncreseCount() {
cout << "please enter the good's name and the count you are willing to increase\n" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
int count;
cin >> count;
increase_count(temp, count);
delete[] temp;
cout << "please enter your integer" << endl;
}
bool add_to_list(char name[], int count) {
add_goods(name, count);
ofstream file("date.txt");
if (file.is_open()) {
for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++) {
file <<"商品名称: "<< iter->name <<" 商品数目: "<< iter->count << endl;
}
file.close();
}
else {
cout << "fail to save the data";
}
return 1;
}
void AddToList() {
cout << "please enter the good's name and the count you are willing to increase\n" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
int count;
cin >> count;
add_to_list(temp, count);
delete[] temp;
cout << "please enter your integer" << endl;
}
bool delete_goods(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter == goods.end()) {
cout << "this kind of good dosen;t exist\n" << endl;
return 0;
}
if (iter->count == count) {
goods.erase(iter);
}
if (iter->count < count) {
cout << "Exceed\n" << endl;
return 0;
}
if (iter->count > count) {
iter->count -= count;
cout << iter->name << "remain " << iter->count << endl;
return 1;
}
}
void DeleteGoods() {
cout << "please enter the name" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
cout << "please enter the count" << endl;
int count;
cin >> count;
delete_goods(temp, count);
cout << "please enter your integer" << endl;
}
bool decrease_count(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter == goods.end()) {
cout << "this kind of good dosen;t exist\n" << endl;
return 0;
}
else {
iter->count -= count;
cout << iter->name << "remain " << iter->count << endl;
return 1;
}
}
void DecreaseCount() {
cout << "please enter the name" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
cout << "please enter the count" << endl;
int count;
cin >> count;
decrease_count(temp, count);
delete[] temp;
cout << "please enter your integer" << endl;
}
bool delete_from_list(char name[]) {
list<Goods>::iterator iter = match(name);
goods.erase(iter);
ofstream file("date.txt");
if (file.is_open()) {
for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++) {
file << "商品名称: " << iter->name << " 商品数目: " << iter->count << endl;
}
file.close();
}
else {
cout << "fail to save the data";
}
return 1;
}
void DeleteFromList() {
cout << "please enter the name" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
delete_from_list(temp);
delete[] temp;
cout << "please enter your integer" << endl;
}
void show_goods() {
cout << endl << setw(20) << "Name" << " " << setw(10) << "Count" << endl;
for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++)
cout << setw(20) << iter->name << " " << setw(10) << iter->count << endl;
}
void ShowGoods() {
show_goods();
cout << "please enter your integer" << endl;
}
void find_goods(char* name) {
list<Goods>::iterator iter = match(name);
if (iter == goods.end()) {
cout << "the good you want dosen't exist\n" << endl;
}
else {
cout << setw(20) << iter->name << " " << setw(10) << iter->count << endl;
}
}
void FindGoods() {
cout << "please enter the name" << endl;
char* temp = new char[100]();
cin.getline(temp, 100);
find_goods(temp);
delete[] temp;
cout << "please enter your integer" << endl;
}
void EscGoods() {
cout << "thanks for using this project\n" << endl;
exit(0);
}
#include <iostream>
#include <list>
using namespace std;
typedef struct Goods {
char name[100];
int count;
}Goods;
void PrintMenu();
list<Goods>::iterator match(char name[]);
bool add_goods(char name[], int count);
bool increase_count(char name[], int count);
bool add_to_list(char name[], int count);
bool delete_goods(char name[], int count);
bool decrease_count(char name[], int count);
bool delete_from_list(char name[]);
void show_goods();
void find_goods(char* name);
void AddGoods();
void IncreseCount();
void AddToList();
void DeleteGoods();
void DecreaseCount();
void DeleteFromList();
void ShowGoods();
void FindGoods();
void EscGoods();
#include <string>
#include <iostream>
#include <fstream>
#include <list>
#include<conio.h>
#include "Project1.h"
using namespace std;
int main()
{
int t;
char _t;
PrintMenu();
for (;;) {
cin >> _t;
t = _t - '0';
cin.ignore();
switch (t) {
case 1:AddGoods();
break;
case 2:IncreseCount();
break;
case 3:AddToList();
break;
case 4:DeleteGoods();
break;
case 5:DecreaseCount();
break;
case 6:DeleteFromList();
break;
case 7:ShowGoods();
break;
case 8:FindGoods();
break;
case 9:EscGoods();
break;
default:
cout << "wrong operation\n"<<"please reenter an integer\n";
break;
}
}
return 0;
}