日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++基础09-货物售卖和MyArray实现

發(fā)布時間:2025/3/15 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++基础09-货物售卖和MyArray实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、貨物出貨與進(jìn)貨

#if 0 #include<iostream> using namespace std; /* 某商店經(jīng)銷一種貨物。貨物購進(jìn)和賣出時以箱為單位。各箱 的重量不一樣,因此商店需要記錄目前庫存的總重量,現(xiàn)在用 C++模擬商店貨物購進(jìn)和賣出的情況 */ class Goods { public:Goods() {weight = 0;next = NULL;cout << "創(chuàng)建了一個重量為:" << weight << "的貨物" << endl;}Goods(int w) {//需要創(chuàng)建一個w的貨物,并且倉庫加這個重量weight = w;next = NULL;total_weight += w;cout << "創(chuàng)建了一個重量為:" << weight << "的貨物" << endl;}static int get_total_wight() {return total_weight;}~Goods() {//需要刪除一個w的貨物,并且倉庫減這個重量cout << "刪除了一個重量為:" << weight << "的貨物" << endl;total_weight -= weight;} public:Goods *next; private:int weight;static int total_weight; }; int Goods::total_weight = 0; void buy(Goods *&head, int w) {//創(chuàng)建一個貨物 重量為wGoods *new_goods = new Goods(w);if (head == NULL)head = new_goods;else {new_goods->next = head; //頭結(jié)點(diǎn)插入head = new_goods;} } void sale(Goods * &head) {if (head == NULL) {cout << "倉庫為空" << endl;return;}Goods *temp = head;head = head->next;delete temp;cout << "saled." << endl; } int main(void) {int choice = 0;int w;Goods *head = NULL; //利用鏈表管理 無頭指針do{cout << "1 進(jìn)貨" << endl;cout << "2 出貨" << endl;cout << "0 退出" << endl;cin >> choice;switch (choice){case 1:cout << "請輸入要創(chuàng)建貨物的重量" << endl;cin >> w;buy(head, w);break;case 2:sale(head);break;case 0:return 0;break;default:break;}cout << "當(dāng)前倉庫的總重量是:" <<Goods::get_total_wight()<< endl;} while (1); } #endif

我的思路:

#if 0 #include<iostream> using namespace std; /* 某商店經(jīng)銷一種貨物。貨物購進(jìn)和賣出時以箱為單位。各箱 的重量不一樣,因此商店需要記錄目前庫存的總重量,現(xiàn)在用 C++模擬商店貨物購進(jìn)和賣出的情況 */ class Goods { public:Goods(int num, int weight) {m_num = num;m_weight = weight;}void get_in_good() {m_sum_weight += m_num*m_weight;}void get_out_good(int num, int weight) {m_sum_weight -= num*weight;}static int get_m_sum_weight(){return m_sum_weight;} private:int m_num;int m_weight;static int m_sum_weight; }; int Goods::m_sum_weight = 0; int main() {Goods g1(1, 20);Goods g2(2, 30);Goods g3(3, 50);g1.get_in_good();g2.get_in_good();g3.get_in_good();g2.get_out_good(1, 30);cout << Goods::get_m_sum_weight() << endl;return 0; } #endif

2、數(shù)組類的封裝

MyArray.h

#pragma once #include<iostream> using namespace std; class MyArray { public:friend ostream& operator<<(ostream& os, const MyArray &obj);friend istream& operator>>(istream& os, MyArray &obj);friend bool operator==(const MyArray &a1, const MyArray& a2); //全局函數(shù)bool operator!=(const MyArray& another); //成員函數(shù)MyArray();MyArray(int length);MyArray(const MyArray &obj);MyArray& operator=(const MyArray &obi);int& operator[](int index) const; void setData(int index, int value);int getData(int index);int getLength() const;~MyArray(); private:int m_length;int *m_space; //指向堆上空間 數(shù)組首地址 };

MyArray.cpp

#include "MyArray.h"MyArray::MyArray() {cout << "MyArray()" << endl;m_length = 0;m_space = NULL; } MyArray::MyArray(int length){/*if (length < 0){m_length = 0;m_space = new int[length];}else {m_length = length;m_space = new int[length];}*///等價于if (length < 0){length = 0;}m_length = length;m_space = new int[length]; } MyArray::MyArray(const MyArray &obj) {if (obj.m_length>=0){ if (m_space != NULL) {delete[] m_space;m_space = NULL;}this->m_length = obj.m_length;this->m_space = new int[this->m_length];for (int i = 0; i < m_length; i++) //數(shù)據(jù)元素深拷貝{this->m_space[i] = obj.m_space[i];}} } MyArray::~MyArray() {if (m_space != NULL) {delete[] m_space;m_space = NULL;} } MyArray& MyArray::operator=(const MyArray &another) {if (this == &another){return *this;}if (this->m_space != NULL) {delete this->m_space;this->m_space = NULL;this->m_length = 0;}if (another.m_length >= 0){this->m_length = another.m_length;this->m_space = new int[this->m_length];for (int i = 0; i < m_length; i++) //數(shù)據(jù)元素深拷貝{this->m_space[i] = another.m_space[i];}}return *this; } int& MyArray::operator[](int index) const{return this->m_space[index]; }void MyArray::setData(int index, int value) {if (m_space != NULL) {//a1.setData(i, i);m_space[index] = value;}} int MyArray::getData(int index) {return m_space[index]; } int MyArray::getLength() const {return m_length; } bool MyArray::operator!=(const MyArray& another) {/*if (this->getLength() != another.getLength()) {return true;}for (int i = 0; i < this->getLength(); i++) {if (this->m_space[i] != another.m_space[i])return true;}return false;*///等價于return !(*this == another); } ostream& operator<<(ostream& os, const MyArray &obj) { //防止被修改添加const后報(bào)錯//原因分析:在調(diào)用getLengt()傳入的是&obj,并不是const類型//安全性高的轉(zhuǎn)換為安全性低的會報(bào)錯//下面的operator[](&obj)傳入的是&obj,并不是const類型 所以報(bào)錯//修改方法就是加上在getLength() 函數(shù)后添加const//修改方法就是加上在operator[](int index)函數(shù)后添加constos << "遍歷整個數(shù)組" << endl;//for (int i = 0; i <obj.m_length; i++) 無法修改for (int i = 0; i <obj.getLength(); i++){//os << obj.m_space[i] << " ";//等價于os << obj[i] << " ";}return os; } //istream& operator >> (istream& is,MyArray &obj) { // cout << "請輸入數(shù)組元素個數(shù):"; // cin >> obj.m_length; // obj.m_space = new int[obj.m_length]; // for (int i = 0; i < obj.m_length; i++) // { // int j; // cin >> j; // obj.m_space[i] = j; // } // return is; // //} istream& operator>>(istream& is, MyArray &array) {cout << "請輸入:" << array.getLength() << "個數(shù)" << endl;for (int i = 0; i < array.getLength(); i++){cin >> array[i];}return is; } bool operator==(const MyArray &a1, const MyArray& a2) {if (a1.getLength() != a2.getLength()) {return false;}for (int i = 0; i < a1.getLength(); i++){if (a1.m_space[i] != a2.m_space[i])return false;}return true; }

main.cpp

#include<iostream> using namespace std; #include"MyArray.h"int main() {MyArray a1(10);for (int i = 0; i < a1.getLength(); i++){//a1.setData(i, i);a1[i] = i + 10; //space[i]=i+10}cout << "print a1" << endl;for (int i = 0; i < a1.getLength(); i++){//cout<<a1.getData(i)<<" ";cout << a1[i] << " " ;}MyArray a2 = a1;cout << endl<<"print a2" << endl;for (int i = 0; i < a2.getLength(); i++){//cout<<a1.getData(i)<<" ";cout << a2[i] << " " ;}cout << endl;MyArray a3;a3 = a1;cout << endl << "print a3" << endl;for (int i = 0; i < a3.getLength(); i++){//cout << a3.getData(i) << " ";cout << a3[i] << " " ;}cout <<endl<< "<<操作符重載" << endl;cout << a3 << endl;MyArray array1(3);cin >> array1;cout << array1 << endl;if (array1 == a1) {cout << "相等" << endl;}elsecout << "不相等" << endl;if (array1 != a1) {cout << "不相等" << endl;}elsecout << "相等" << endl; }

總結(jié)

以上是生活随笔為你收集整理的C++基础09-货物售卖和MyArray实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。