購物車(復合類)

2021-10-05 13:47:16 字數 1857 閱讀 9847

商品類包含私有資料成員:商品編號、名稱、顏色、尺碼、單價、數量。

購物車類包含私有資料成員:商品物件集合、商品總數、購物車所有商品總價。

購物車操作分別有add、delete、up、down表示,具體格式描述如下:

add 商品編號 商品名稱 顏色 尺碼 單價  數量    //新增1個或多個同類商品,若購物車已有指定編號商品,只需增加數量;若無,新增在購物車首部。

delete 商品編號    //刪除購物車中給定商品編號的所有商品,不存在刪除不成功的情況,即購物車中一定有給定編號的物品。

up 商品編號       //購物車中商品編號的商品數量加1,不存在操作不成功的情況。

down 商品編號   //購物車中商品編號的商品數量減1,且最小為1。

測試次數t

每組測試資料為:

購物車操作次數n,後跟n行操作。

add 2018040801 格力變頻冷暖空調kfr-26gw 大1匹 變頻掛機 2999 1

add 2018040802 長虹65d2p高畫質hdr平板led液晶 1 1 4799 1

add 2018040803 康佳led55x9人工智慧平板電視機 null 55寸 4999 1

up 2018040802

up 2018040803

down 2018040803

delete 2018040802

add 2018040802 長虹65d2p高畫質hdr平板led液晶 1 1 4799 2

商品清單:

商品,顏色,尺碼,單價,數量,小計

長虹65d2p高畫質hdr平板led液晶,1,1,4799.00,2,9598.00

康佳led55x9人工智慧平板電視機,null,55寸,4999.00,1,4999.00

格力變頻冷暖空調kfr-26gw,大1匹,變頻掛機,2999.00,1,2999.00

4件商品,總商品金額17596.00

#include #include #include #include using namespace std;

class goods;

istream &operator>>(istream &strm,goods &obj)

ostream &operator<<(ostream &strm,goods &obj)

}//執行到此處說明購物車中沒有與待新增商品相同的商品

goods[qty]=new goods();//在物件陣列中為新商品物件分配乙個空間

goods[qty]->id=id;//先將已獲取的id賦給當前商品物件

cin>>*goods[qty];//然後通過goods過載的》輸入剩餘的商品資料

totalamount+=goods[qty]->amount;

totalprice+=goods[qty]->amount*goods[qty]->price;

++qty;//新的商品種類所以qty+1

}void delete()}}

void down()

return;}}

}};istream &operator>>(istream &strm,shoppingcart &obj)

}}ostream &operator<<(ostream &strm,shoppingcart &obj)

return 0;

}

購物車(註冊 登入 購物 購物車 結帳)

購物車 註冊 登入 購物 購物車 結帳 shopping car dict dict money 0 def input username pwd username input username pwd input pwd return username,pwd def goods get with...

打破沙鍋 購物車類

using system using system.collections serializable public class shoppingcart the sum total of the prices public decimal total add a new item to the sh...

購物車原理

1.cookie n cookie儲存在客戶端,且占用很少的資源,瀏覽器允許存放300個cookie,每個cookie的大小為4kb,足以滿足購物車的要求,同時也減輕了伺服器的負荷 n cookie為瀏覽器所內建,使用方便。即使使用者不小心關閉了瀏覽器視窗,只要在cookie定義的有效期內,購物車中...