1. C语言编写“超市结账系统” 急急急!!!

#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>

using namespace std;

// Item info base class
class ItemInfo{
public:
ItemInfo(){}
ItemInfo(string barcode, string name, float price)
{
this->barcode = barcode;
this->name = name;
this->price = price;
}
ItemInfo(string barcode)
{
this->barcode = barcode;
}
void Display() {
cout << barcode <<"\t"<<name<<"\t"<<price<< endl;
}
void Input() {
cout << "输入条形码:" << endl;
cin >> barcode;
cout << "输入名称:" << endl;
cin >> name;
cout << "输入价格:" << endl;
cin >> price;
}
void Modify() {
cout << "输入名称:" << endl;
cin >> name;
cout << "输入价格:" << endl;
cin >> price;
}
friend ostream& operator<<(ostream& stream, ItemInfo& item){
stream << item.barcode <<'\t'<< item.price <<'\t'<< item.name<<endl;
return stream;
}
friend istream& operator>>(istream& stream, ItemInfo& item){
stream >> item.barcode >> item.price >> item.name;
return stream;
}

public:
string barcode;
string name;
float price;
};

// Interface class
class DataProvider{
public:
virtual void GetFullData(string barcode, string& name, float& price) = 0;
};

// Purchase item class
class ItemPurchaseInfo : public ItemInfo{
public:
ItemPurchaseInfo():ItemInfo(){}
ItemPurchaseInfo(string barcode, int count=1) :
ItemInfo(barcode)
{
this->count = count;
}

// Rember to call this when barcode set
void GetFullData(DataProvider& aPro) {
aPro.GetFullData(barcode, name, price);
}
void Input() {
cout << "输入条形码:" << endl;
cin >> barcode;
cout << "输入数量:" << endl;
cin >> count;
}
void Display() {
cout << barcode <<"\t"<<name<<"\t"<<price<<"\t"<<count<< endl;
}
public:
string barcode;
int count;
};

// Item list class
class ItemList {
public:
ItemList(){items.clear();}
friend ostream& operator<<(ostream& stream, ItemList& list){
unsigned int count = list.items.size();
stream <<count<<endl;
for(unsigned int i(0);i<count;i++)
stream<< list.items.at(i);
return stream;
}
friend istream& operator>>(istream& stream, ItemList& list){
unsigned int count(0);
stream >>count;
list.items.clear();
for(unsigned int i(0);i<count;i++){
ItemInfo item;
stream >> item;
list.items.insert(list.items.end(), item);
}
return stream;
}
void Add(ItemInfo item) {
items.insert(items.end(), item);
}
void Modify() {
string barcode;
cout << "输入条形码:" << endl;
cin >> barcode;
for(unsigned int i(0);i<items.size();i++) {
if(items.at(i).barcode == barcode ) {
items.at(i).Modify();
}
}
}
public:
vector<ItemInfo> items;
};

// Purchase item list class
class PurchaseItemList {
public:
PurchaseItemList(){items.clear();}
void Add(ItemPurchaseInfo item)
{items.insert(items.end(), item);}
public:
vector<ItemPurchaseInfo> items;
};

// Implements the interface class
class Cashier : public DataProvider {
public:
Cashier() : purchase(), stock(){}
~Cashier(){}
public: // User funcs
void CheckIn(){
purchase.items.clear();
int opt(0);
do {
unsigned int i(0);
ItemPurchaseInfo ipi;
ipi.Input();
purchase.Add(ipi);
cout << "按0退出,任意键继续" << endl;
cin >> opt;
} while(opt);
}
void CheckOut(){
for(unsigned int i(0); i < purchase.items.size(); i++) {
purchase.items.at(i).GetFullData( *this );
}
float checkin(0);
cout << "输入收款数:" << endl;
cin >> checkin;
DisplayList(checkin);
}
void Display()
{
cout << endl<<"商品清单 "<< stock.items.size() << endl;
cout <<"--------------------------------------" << endl;
for(unsigned int i(0);i<
stock.items.size(); i++){
stock.items.at(i).Display();
}
cout <<"--------------------------------------" << endl;
}
void DisplayList(float checkin)
{
cout << endl<<"购物小票清单" << endl;
cout <<"--------------------------------------" << endl;
float total(0.0);
for(unsigned int i(0); i < purchase.items.size(); i++) {
purchase.items.at(i).Display();
total += purchase.items.at(i).price * purchase.items.at(i).count;
}
cout <<"--------------------------------------" << endl;
cout <<"货款合计:"<< total << "元" << endl;
cout <<"收款数:"<< checkin << "元" << endl;
float change(checkin-total);
assert( change >= 0.0);
cout <<"找零:"<< change << "元" << endl<< endl;

}
friend ostream& operator<<(ostream& stream, Cashier& c){
stream << c.stock;
return stream;
}
friend istream& operator>>(istream& stream, Cashier& c){
c.stock.items.clear();
stream >> c.stock;
return stream;
}

public: // interface func
void GetFullData(string barcode, string& name, float&price) {
// go through stock and find the item by barcode matching
for(unsigned int i(0); i < stock.items.size(); i++) {
if(stock.items.at(i).barcode == barcode) {
name = stock.items.at(i).name;
price = stock.items.at(i).price;
}
}
}

public:
PurchaseItemList purchase;
ItemList stock;
};

int main()
{
int opt(0);
Cashier cashier;
ifstream fin("data.bin", ios::in | ios::binary);
fin.seekg(0, ios::beg);
//cashier.stock.Load(fin);
fin >> cashier;
fin.close();

ofstream fout;

ItemInfo item;
do{
cout << "1. 新购买" << endl;
cout << "2. 输入新商品信息" << endl;
cout << "3. 修改商品信息" << endl;
cout << "4. 显示商品信息" << endl;
cout << "0. 退出" << endl;
cin >> opt;
switch(opt)
{
case 1:
cashier.CheckIn();
cashier.CheckOut();
break;
case 2:
item.Input();
cashier.stock.Add(item);
fout.open("data.bin", ios::out| ios::binary);
fout.seekp(0,ios::beg);
fout << cashier;
fout.close();
break;
case 3:
cashier.stock.Modify();
fout.open("data.bin", ios::out| ios::app | ios::binary);
fout << cashier;
fout.close();
break;
case 4:
cashier.Display();
break;
default:
break;
}
} while(opt);
return 0;
}