因为找了一份新工作,公司视觉平台是用C++来写的。以前Cognex用的C#。。。。。。。。
现在用C#在公司也就是能写写上位机软件了,哎心塞
别说还挺像的,第一次写。。。。
#include <iostream> #include <string> using namespace std;
class Goods { public: Goods*next; Goods() //无参构造函数 { weight_ = 0; next = NULL; } Goods(double weight) //有参构造函数 { this->weight_ = weight; this->next = NULL; this->total_weight += weight; } ~Goods() { std::cout << "删除一箱重量为:"<<weight_<<"货物"<<endl; this->total_weight -= weight_; } static double Get_total_weight() { return total_weight; } private: double weight_ = 0; //货物的重量 static double total_weight; //仓库的总重量 }; double Goods::total_weight = 0; //初始化仓库的重量 void Buy(Goods *&head,double weight)//购货 { Goods*new_good = new Goods(weight); if (head==NULL) { head = new_good; } new_good->next = head; head = new_good; } void Sale(Goods*&head)//出货 { if (head==NULL) { std::cout << "当前仓库中已经没有货物了"<<endl; return; } Goods*temp = head; head = head->next; delete(temp);//必须是delete,free不能够触发析构函数 } void main(void) { Goods*head; //货物链表头 do { std::cout << "输入 1 为进货" << endl; cout << "输入 2 为出货" << endl; std::cout << "输入 0 为退出程序" << endl;
int inputNumber; std::cin >> inputNumber; switch (inputNumber) { case 1: { std::cout << "请输入进货的重量:" << endl; double weight; std::cin >> weight; Buy(head, weight); } break; case 2: Sale(head); break; case 0: return; default: cout << "\nError! Press any key to select again." << endl; } std::cout << "仓库的总重量为:" << Goods::Get_total_weight() << endl; } while (1); }