function
image.pngimage.png
[图片上传中...(image.png-946e19-1511250191317-0)]
uml.png#ifndef _MYLIST_H_
#define _MYLIST_H_
#include<iostream>
#include<string>
#include <fstream>
#include <ostream>
#include <stdlib.h>
#include <vector>
#include <string.h>
using namespace std;
struct snode {
string name;
string target;
int quantity;
};
enum Order {
ITEM,
CUSTOMER
};
class mylist {
private:
vector<snode>INV;
vector<snode>SALE;
public:
mylist();
void insert(string name,string target,int quantity,Order order);
void remove(string name);
void readFile(string fileName);
void saveFile(string fileName,Order order);
void outputFile(string fileName);
};
#endif
#include "mylist.h"
mylist::mylist() {
}
void mylist::insert(string name,string target,int quantity,Order order) {
snode temp;
temp.name = name;
temp.target = target;
temp.quantity = quantity;
if(order==ITEM) {
INV.push_back(temp);
}
else if(order==CUSTOMER) {
SALE.push_back(temp);
}
}
void mylist::remove(string name) {
}
void mylist::readFile(string fileName) {
string operator_;
string name_;
string target_;
int quantity_;
ifstream file(fileName.c_str());
string word;
/*
while(!file.eof()){
file>>word;
cout<<word<<endl;
}
*/
while(getline(file,word)) {
//cout<<word<<endl;
operator_ = word[0];
//cout<<operator_<<endl;
//load name
int namepos=2;
int cnt=0;
while(word[cnt+2]!='#') {
cnt++;
}
string temp = word;
name_= temp.substr(namepos,cnt);
//cout<<name_<<endl;
//load target
int targetpos = cnt+3;
int cnt2=targetpos;
while(word[cnt2]!='#'&&cnt2<=word.length()) {
//cout<<word[cnt2]<<" ";
cnt2++;
}
target_ = word.substr(targetpos,cnt2-targetpos);
//cout<<target_<<endl;
//load quantity
if(word[cnt2]=='#') {
int quantitypos = cnt2+1;
int cnt3 = quantitypos;
string quantityStr = word.substr(quantitypos);
//string to int
quantity_ = atoi(quantityStr.c_str());
//cout<<quantity_<<endl;
}
else {
quantity_=0;
}
cout<<endl;
//Buy
if(operator_=="B") {
insert(name_,target_,quantity_,ITEM);
}
else if(operator_=="S") {
insert(name_,target_,quantity_,CUSTOMER);
}
else if(operator_ =="R") {
remove(name_);
}
}
}
void mylist::saveFile(string fileName,Order order) {
//save file
ofstream file(fileName.c_str());
string word;
if(order == ITEM) {
for(int i=0; i<INV.size(); i++) {
//char* qStr;
//itoa(INV[i].quantity,qStr,10);
//char* qs;
//sprintf(qs, "%d", INV[i].quantity);
//cout<<qs;
word = INV[i].name+"#"+INV[i].target+"#";
file<<word<<INV[i].quantity<<endl;
}
}
else if(order == CUSTOMER) {
for(int i=0; i<SALE.size(); i++) {
//char* qStr;
//itoa(SALE[i].quantity,qStr,10);
//char* qs;
//sprintf(qs, "%d", SALE[i].quantity);
word = SALE[i].name+"#"+SALE[i].target+"#";
file<<word<<SALE[i].quantity<<endl;
}
}
}
void mylist::outputFile(string fileName) {
ifstream file(fileName.c_str());
string word;
while(getline(file,word)) {
cout<<word<<endl;
}
}
#include "mylist.h"
int main() {
mylist Inventory;
mylist Sale;
string totalchoice;
string transdata;
string invdata;
string salesdata;
cout<<"SHOW TOTAL DATA?(YES|NO)"<<endl;
cin>>totalchoice;
if(totalchoice=="YES")
Sale.outputFile("testdata.txt");
cout<<endl<<"Enter the Buys report filename: ";
cin>>invdata;
Inventory.readFile("testdata.txt");
Inventory.saveFile(invdata,ITEM);
Inventory.outputFile(invdata);
cout<<endl<<"Enter the Sales report filename: ";
cin>>salesdata;
Sale.readFile("testdata.txt");
Sale.saveFile(salesdata,CUSTOMER);
Sale.outputFile(salesdata);
}
网友评论