货品的进出库模型
貨品包括貨號和重量屬性,實現簡單的入庫出庫統(tǒng)計功能,實例用于加深指針和鏈表的認識以及類的使用。
#include <iostream> #include <string> using namespace std; class StockWeight {public:StockWeight(int c,int w = 0):weight(w),code(c){totalWeight += w;//入庫總重量增加cout << c << "stock input! total " << totalWeight << endl;}~StockWeight(){totalWeight -= weight;//出庫析構總重量減少cout << code << "stock output! left " << totalWeight << endl;}int getCode(){return code;//返回貨號 }static int getTotalWeight(){return totalWeight;//返回總重量 }StockWeight *next;private:int weight;//重量int code;//貨號static int totalWeight;//靜態(tài)成員 總共的重量 }; int StockWeight::totalWeight = 0; void inputWeight(StockWeight *&head,int code,int w) {StockWeight *g,*p = new StockWeight(code, w);//新對象結點p->next = NULL;if (head == NULL){head = p;return;}//鏈表沒有內容//循環(huán)將新結點插入到最后g = head;while (g->next != NULL){g = g->next;//指針挪動 }g->next = p; } void outputWeight(StockWeight *&head, int code) {StockWeight *g,*p;if (head == NULL){cout << " there is no goods!" << endl; return;}//鏈表沒有內容g = head;//跟蹤指針while (g->getCode() != code && g->next != NULL)//如果當前指針的code不等于特定的code 或者鏈表已經沒有下一結點便停止循環(huán) {g = g->next;//指針挪動 }if (g->getCode() != code) { cout << " there is no goods code " << code << endl; return; }//沒找到貨品code//找到了code//第一個結點特殊處理if (g == head){head = head->next;//頭指針改變delete g;//出庫析構return;}//遍歷找出它的上結點p = head;while (p->next != g){p = p->next;//指針挪動break;}p->next = g->next;//重指向delete g;//出庫析構 } void main() {StockWeight *head = NULL;//鏈表頭指針int leng,code,weight;//leng是指令。。code是貨號。。weight是重量do{cin >> leng;//輸入指令switch (leng){case 1://入貨 {cout << "inputWeight:" << endl;//入庫cout << "code:";cin >> code;//輸入貨號cout << "weight:";cin >> weight;//輸入重量inputWeight(head, code, weight);//調用方法入庫break;}case 2://出貨 {cout << "outputWeight:" << endl;//出庫cout << "code:";cin >> code;//輸入貨號outputWeight(head, code);//調用方法出庫break;}case 3://輸出總重量 {cout << "total Weight:" << StockWeight::getTotalWeight() << endl;break;}default:break;}} while (leng);//無限循環(huán)知道輸入0 getchar();getchar(); }?
轉載于:https://www.cnblogs.com/godehi/p/8320862.html
總結
- 上一篇: 如何隐藏 Safari 中 input
- 下一篇: Win2008 R2 IIS7.5+PH