http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net
分类: C/C++
2011-12-02 15:15:10
#include
#include
#include
using namespace std;
class Item_base {
public:
Item_base(string book,double f):isbn(book),price(f) {}
string book() const
{
return isbn;
}
virtual double net_price(int n) const
{
return n*price;
}
virtual Item_base* clone() const
{
return new Item_base(*this);
}
protected:
string isbn;
double price;
};
class Bulk_item:public Item_base {
public:
Bulk_item(string book,double f,int m,double dis):Item_base(book,f),min(m),discount(dis) {}
double net_price(int n) const
{
if (n >= min)
return n*(1-discount)*price;
else
return n*price;
}
virtual Bulk_item* clone() const ///奇怪的是,这里的const修饰符可要可不要
{
return new Bulk_item(*this);
}
private:
size_t min;
double discount;
};
class Sale_item {
public:
Sale_item(): p(0),use(new int(1)) {}
Sale_item(const Item_base& item_base):p(item_base.clone()),use(new int(1)) {}
Sale_item(const Sale_item& rsale_item):p(rsale_item.p),use(rsale_item.use)
{
(*use)++;
}
Sale_item& operator==(const Sale_item& rsale_item)
{
++(*rsale_item.use);
desc_use();
p=rsale_item.p;
use=rsale_item.use;
return *this;
}
const Item_base* operator->() const
{
if(p)
return p;
else
throw logic_error("unbound Sale_item");
}
const Item_base& operator*() const
{
if(p)
return *p;
else
throw logic_error("unbound Sale_item");
}
~Sale_item()
{
desc_use();
}
private:
void desc_use()
{
if( --(*use)==0 ){
delete p;
delete use;
}
}
private:
Item_base *p;
int *use;
};
bool compare(const Sale_item& litem,const Sale_item& ritem)
{
return litem->book() < ritem->book() ;
}
typedef bool (*Comp)(const Sale_item& litem,const Sale_item& ritem);
class Basket {
public:
typedef multiset
typedef set_type::size_type size_type;
Basket() :items(compare) {}
void add_item(const Sale_item& item)
{
items.insert(item);
cout<<"第"<<++add_count<<"次添加数据"<
}
size_type count(const Sale_item& item)
{
return items.count(item);
}
double total() const
{
cout<<"enter the total function"<
double sum=0;
for(set_type::const_iterator iter=items.begin();
iter != items.end();
iter=items.upper_bound(*iter) ) {
cout<<"计算isbn为"<<(*iter)->book()<<"的销售数"<
cout<<(*iter)->book()<<"的销售数目为:"<
sum += (*iter)->net_price( items.count(*iter) );
cout<<"目前的总销售额为:"<
}
cout<<"level the total function"<
return sum;
}
private:
multiset
static int add_count;
};
int Basket::add_count=0;
int main(int argc,char** argv)
{
Sale_item item1(Item_base("123", 45));
Sale_item item2(Bulk_item("345", 45, 3, 0.15));
Sale_item item3(Bulk_item("678", 55, 5, 0.25));
Sale_item item4(Bulk_item("abc", 35, 2, 0.10));
Sale_item item5(Item_base("def", 35));
Basket sale;
sale.add_item(item1);
cout << "added first item" << endl;
sale.add_item(item1);
sale.add_item(item1);
sale.add_item(item1);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item2);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item3);
sale.add_item(item4);
sale.add_item(item4);
sale.add_item(item4);
sale.add_item(item4);
sale.add_item(item4);
sale.add_item(item4);
sale.add_item(item5);
sale.add_item(item5);
cout << "added last item" << endl;
cout << sale.total() << endl;
getchar();
return 0;
}