網站首頁 編程語言 正文
本文設計C++快捷店會員的簡單管理系統。基本要求如下:
(1)定義人民幣RMB類(數據成員包括元,角,分三個整型變量),實現人民幣的基本運算(對操作符+和-運算進行重載)和顯示(顯示元,角,分)。
(2)定義會員member類,表示會員的基本信息,包括:編號(按建立會員的順序自動生成),姓名,密碼,電話。提供輸入、輸出信息等功能。
(3)由RMB類和member類共同派生一個會員卡memberCar類(含有memberCar類型next指針變量),提供新建會員、充值、消費和查詢余額等功能。
(4)main函數定義一個memberCar類鏈表,保存會員卡,模擬一個快捷店的會員卡管理功能,主要包括:
1)、 新建會員(檢查輸入的會員電話號碼是否已經存在;若存在,則表示已經有該會員,需要重新輸入會員信息),則為該會員建立會員卡,將該會員卡所表示的對象插入memberCar類鏈表表尾;
2)、已有會員充值(需要對充值進行非負性檢查);
3)、已有會員消費(憑密碼,不能透支);
4)、輸出快捷店當前會員數,當前所有會員的消費總額,所有會員卡剩余的額度總數;
5)、遍歷鏈表,輸出所有會員卡的信息(不能輸出會員個人密碼);
6)、建立一個文本文件“member.txt”,將所有會員信息存入該文件中;
7) 、建立一個二進制文件“member.dat” ,將所有會員信息存入該文件中;
8) 、建立指針數組,初始化指針數組,存儲鏈表中每個會員對象的地址;對鏈表中的每個會員按照消費額度按照從小到大進行排序(要求不能改變鏈表的邏輯結構),并將排序后的會員對象的地址存在指針數組中;
9)、利用(8)中的指針數組,在屏幕上輸出會員的排序結果。
RMB頭文件:
#pragma once #includeusing namespace std; ? class RMB { public: ?? ?static double tCost; ?? ?int m_Yuan; ?? ?int m_Jiao; ?? ?int m_Fen; ?? ?RMB(); ?? ?RMB& operator+(double rmb); ?? ?RMB& operator-(double rmb); ?? ?void show(); };
RMB源文件
#include "RMB.h" ? double RMB::tCost = 0; RMB::RMB() { ?? ?m_Yuan = 0; ?? ?m_Jiao = 0; ?? ?m_Fen = 0; } RMB& RMB::operator+(double rmb) { ?? ?int yuan = ((int)(rmb * 100)) / 100; ?? ?int jiao = ((int)(rmb * 10)) % 10; ?? ?int fen = ((int)(rmb * 100)) % 10; ?? ?this->m_Yuan += yuan; ?? ?this->m_Jiao += jiao; ?? ?this->m_Fen += fen; ?? ?return *this; } RMB& RMB::operator-(double rmb) { ?? ?int yuan = ((int)(rmb * 100)) / 100; ?? ?int jiao = ((int)(rmb * 10)) % 10; ?? ?int fen = ((int)(rmb * 100)) % 10; ?? ?this->m_Yuan -= yuan; ?? ?this->m_Jiao -= jiao; ?? ?this->m_Fen -= fen; ?? ?return *this; } void RMB::show() { ?? ?cout << "您的余額為:" << this->m_Yuan << "元" << this->m_Jiao << "角" << this->m_Fen << "分" << endl; }
member頭文件
#pragma once #include#include using namespace std; ? class member { public: ?? ?static long preId; ?? ?long m_id; ?? ?string m_Name; ?? ?string m_Passport; ?? ?string m_PhoneNumber; ?? ?member(); ?? ?void inIft(); ?? ?void outIft(); };
member源文件
#include "member.h" ? long member::preId = -1L; member::member() { ?? ?this->m_id = preId + 1; ?? ?preId = this->m_id; } void member::inIft() { ?? ?string name; ?? ?cout << "請輸入您的姓名:"; ?? ?cin >> name; ?? ?this->m_Name = name; ?? ?cout << endl; ?? ?string passport; ?? ?cout << "請輸入您的密碼:"; ?? ?cin >> passport; ?? ?this->m_Passport = passport; ?? ?cout << endl; ?? ?string phoneNum; ?? ?cout << "請輸入您的電話:"; ?? ?cin >> phoneNum; ?? ?this->m_PhoneNumber = phoneNum; ?? ?cout << endl; } void member::outIft() { ?? ?cout << "編號:" << m_id << " 姓名:" << m_Name << " 密碼:" << m_Passport << " 電話:" << m_PhoneNumber << endl; }
memberCard頭文件
#pragma once #include#include "RMB.h" #include "member.h" using namespace std; ? class memberCard:public RMB,public member{ public: ?? ?static int num_; ?? ?memberCard* next; ?? ?double Con = 0; ?? ?memberCard(); ?? ?void create(); ?? ?void deposit(); ?? ?void consume(); ?? ?void balanceCheck(); ?? ?~memberCard(); };
memberCard源文件
#include "memberCard.h" ? int memberCard::num_ = -1; memberCard::memberCard() { ?? ?num_++; ?? ?this->next = NULL; } void memberCard::create() { ?? ?inIft(); } void memberCard::deposit() { ?? ?cout << "請輸入充值的金額:"; ?? ?double money; ?? ?cin >> money; ?? ?cout << endl; ?? ?if (money < 0) { ?? ??? ?cout << "充值金額不能為負!" << endl; ?? ??? ?return; ?? ?} ?? ?this->operator+(money); } void memberCard::consume() { ?? ?cout << "請輸入消費金額:"; ?? ?double money; ?? ?cin >> money; ?? ?Con += money; ?? ?cout << endl; ?? ?cout << "請輸入密碼:"; ?? ?string passport; ?? ?cin >> passport; ?? ?if (strcmp(passport.c_str(), m_Passport.c_str()) == 0) { ?? ??? ?if (((int)(money * 100)) / 100 > m_Yuan) { ?? ??? ??? ?cout << "余額不足!" << endl; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?else if (((int)(money * 100)) / 100 == m_Yuan) { ?? ??? ??? ?if (((int)(money * 10)) % 10 > m_Jiao) { ?? ??? ??? ??? ?cout << "余額不足!" << endl; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ??? ?else if (((int)(money * 10)) % 10 == m_Jiao) { ?? ??? ??? ??? ?if (((int)(money * 100)) % 10 > m_Fen) { ?? ??? ??? ??? ??? ?cout << "余額不足!" << endl; ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?this->operator-(money); ?? ??? ?RMB::tCost += money; ?? ?} ?? ?else { ?? ??? ?cout << "密碼錯誤!" << endl; ?? ??? ?return; ?? ?} } void memberCard::balanceCheck() { ?? ?this->show(); } memberCard::~memberCard() { ?? ?if (this->next != NULL) { ?? ??? ?delete this->next; ?? ??? ?this->next = NULL; ?? ?} }
主函數
#include#include "RMB.h" #include "member.h" #include "memberCard.h" #include #include using namespace std; ? #define FILENAME1 "member.txt" #define FILENAME2 "member.dat" void AddMember(memberCard*& head, memberCard*& pre) { ?? ?if (head == NULL) { ?? ??? ?head = new memberCard; ?? ??? ?pre = head; ?? ??? ?return; ?? ?} ?? ?memberCard* p = new memberCard; ?? ?p->create(); ?? ?p->next = NULL; ?? ?pre->next = p; ?? ?pre = p; ?? ?cout << "添加成功!" << endl; ?? ?system("pause"); } bool check(memberCard*& head, memberCard*& pre) { ?? ?if (head == NULL) { ?? ??? ?return false; ?? ?} ?? ?cout << "請輸入要辦理會員的電話號:"; ?? ?string phoneNum; ?? ?cin >> phoneNum; ?? ?cout << endl; ?? ?memberCard* p = head->next; ? ?? ?while (p != NULL) { ?? ??? ?if (strcmp(phoneNum.c_str(), p->m_PhoneNumber.c_str()) == 0) { ?? ??? ??? ?cout << "該手機號已有會員!" << endl; ?? ??? ??? ?system("pause"); ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?p = p->next; ?? ?} ?? ?return false; } void deposit_(memberCard* head) { ?? ?if (head == NULL) { ?? ??? ?return; ?? ?} ?? ?memberCard* p = head->next; ?? ?cout << "請輸入要充值的會員卡手機號:" << endl; ?? ?string phoneNum; ?? ?cin >> phoneNum; ?? ?while (p != NULL) { ?? ??? ?if (strcmp(phoneNum.c_str(), p->m_PhoneNumber.c_str()) == 0) { ?? ??? ??? ?p->deposit(); ?? ??? ??? ?cout << "充值成功!" << endl; ?? ??? ??? ?system("pause"); ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?p = p->next; ?? ?} ?? ?cout << "未查詢到會員卡!" << endl; ?? ?system("pause"); } void consume_(memberCard* head) { ?? ?if (head == NULL) { ?? ??? ?return; ?? ?} ?? ?cout << "請輸入要消費的會員手機號:" << endl; ?? ?string phoneNum; ?? ?cin >> phoneNum; ?? ?memberCard* p = head->next; ?? ?while (p != NULL) { ?? ??? ?if (strcmp(phoneNum.c_str(), p->m_PhoneNumber.c_str()) == 0) { ?? ??? ??? ?p->consume(); ?? ??? ??? ? ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?p = p->next; ?? ?} ?? ?cout << "未查詢到會員卡!" << endl; ?? ?system("pause"); } void total(memberCard* head) { ?? ?if (head == NULL) { ?? ??? ?return; ?? ?} ?? ?memberCard* p = head->next; ?? ?int sum = 0; ?? ?double totalBalance = 0; ?? ?while (p != NULL) { ?? ??? ?sum++; ?? ??? ?double balance = p->m_Yuan * 1.0 + p->m_Jiao * 0.1 + p->m_Fen * 0.01; ?? ??? ?totalBalance += balance; ?? ??? ?p = p->next; ?? ?} ?? ?cout << "快捷店當前會員數為:" << sum << endl; ?? ?cout << "當前所有會員的消費總額為:" << p->tCost << endl; ?? ?cout << "所有會員卡剩余額度總數為:" << totalBalance << endl; ?? ?system("pause"); } void display(memberCard*& head) { ?? ?if (head == NULL) { ?? ??? ?cout << "無任何會員卡" << endl; ?? ??? ?return; ?? ?} ?? ?memberCard* p = head->next; ?? ?while (p != NULL) { ?? ??? ?double balance = p->m_Yuan * 1.0 + p->m_Jiao * 0.1 + p->m_Fen * 0.01; ?? ??? ?cout << "id:" << p->m_id << " 姓名:" << p->m_Name << " 電話:" << p->m_PhoneNumber << " 余額:" < next; ?? ?} ?? ?system("pause"); } void save(memberCard*& head) { ?? ?if (head == NULL) { ?? ??? ?return; ?? ?} ?? ?ofstream oft; ?? ?oft.open(FILENAME1, ios::out); ? ?? ?memberCard* p = NULL; ?? ?p = head->next; ?? ?while (p != NULL) { ?? ??? ?oft<< p->m_id << " " << p->m_Name << " " << p->m_Passport << " " << p->m_PhoneNumber << " " << p->m_Yuan << " " << p->m_Jiao << " " < m_Fen<< " " << p->preId<< " " << p->tCost<< endl; //<< "會員總人數:" << p->num_ ?? ??? ?p = p->next; ?? ?} ?? ?oft.close(); } void save2(memberCard* head) { ?? ?if (head == NULL) { ?? ??? ?return; ?? ?} ?? ?ofstream oft; ?? ?oft.open(FILENAME2, ios::binary | ios::out); ?? ?memberCard* p = head->next; ?? ?while (p != NULL) { ?? ??? ?oft.write((const char*)&p, sizeof(memberCard)); ?? ??? ?p = p->next; ?? ?} ?? ?cout << "保存成功!" << endl; ?? ?system("pause"); } void readFile() { ?? ?ifstream ifs; ?? ?ifs.open(FILENAME1, ios::in); ?? ?if (ifs.is_open()) { ?? ??? ?char buf[1024] = { 0 }; ?? ??? ?while (ifs >> buf) { ?? ??? ??? ?cout << buf << endl; ?? ??? ?} ?? ?} ?? ?ifs.close(); ?? ?system("pause"); } void toArray(memberCard* head,memberCard**& arr) { ?? ?/*memberCard** arr = new memberCard*[memberCard::num_];*/ ?? ?memberCard* p = head->next; ?? ?int index = 0; ?? ?while (p != NULL) { ?? ??? ?arr[index] = p; ?? ??? ?index++; ?? ??? ?p = p->next; ?? ?} ?? ?for (int i = 0; i < memberCard::num_; i++) { ?? ??? ?for (int j = i + 1; j < memberCard::num_; j++) { ?? ??? ??? ?if (arr[i]->Con > arr[j]->Con) { ?? ??? ??? ??? ?memberCard* temp = arr[i]; ?? ??? ??? ??? ?arr[i] = arr[j]; ?? ??? ??? ??? ?arr[j] = temp; ?? ??? ??? ?} ?? ??? ?} ?? ?} } ? int main() { ?? ?memberCard* head = NULL; ?? ?memberCard* pre = NULL; ?? ?FileIsEmpty(head,pre); ?? ?while (true) { ? ?? ??? ?cout << "*************************************" << endl; ?? ??? ?cout << "*********快捷店會員管理系統**********" << endl; ?? ??? ?cout << "************1.新建會員***************" << endl; ?? ??? ?cout << "***********2.已有會員充值************" << endl; ?? ??? ?cout << "***********3.已有會員消費************" << endl; ?? ??? ?cout << "*******4.輸出快捷店當前會員數********" << endl; ?? ??? ?cout << "*******當前所有會員的消費總額********" << endl; ?? ??? ?cout << "*******所有會員卡剩余的額度總數******" << endl; ?? ??? ?cout << "********5.輸出所有會員卡的信息*******" << endl; ?? ??? ?cout << "****6.采用二進制方式保存會員信息*****" << endl; ?? ??? ?cout << "******7.讀取文件中所有會員信息*******" << endl; ?? ??? ?cout << "**8.用數組存儲地址,并按消費額度排序*" << endl; ?? ??? ?cout << "*************************************" << endl; ?? ??? ?int d_select; ?? ??? ?cout << "請輸入您的選擇:" << endl; ?? ??? ?cin >> d_select; ? ?? ??? ?switch (d_select) { ?? ??? ?case 1: ?? ??? ?{ ?? ??? ??? ?if (check(head,pre)) { ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?AddMember(head, pre); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?case 2: ?? ??? ??? ?deposit_(head); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?consume_(head); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?total(head); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?display(head); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?save2(head); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?readFile(); ?? ??? ??? ?break; ?? ??? ?case 8: ?? ??? ?{ ?? ??? ??? ?memberCard** arr = new memberCard * [memberCard::num_]; ?? ??? ??? ?toArray(head, arr); ?? ??? ??? ?for (int i = 0; i < memberCard::num_; i++) { ?? ??? ??? ??? ?double balance = arr[i]->m_Yuan * 1.0 + arr[i]->m_Jiao * 0.1 + arr[i]->m_Fen * 0.01; ?? ??? ??? ??? ?cout << "id:" << arr[i]->m_id << " 姓名:" << arr[i]->m_Name << " 電話:" << arr[i]->m_PhoneNumber << " 余額:" << balance << endl; ?? ??? ??? ?} ?? ??? ??? ?system("pause"); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?} ?? ??? ?save(head); ?? ??? ?system("cls"); ?? ?} ? ?? ? ?? ?system("pause"); ?? ?return 0; }
原文鏈接:https://blog.csdn.net/lumen1023/article/details/117951463
相關推薦
- 2023-01-13 python中的txt文件轉換為XML_python
- 2022-06-25 C++文件讀寫操作詳解_C 語言
- 2022-10-21 Go錯誤和異常CGO?fallthrough處理教程詳解_Golang
- 2022-07-03 python爬蟲lxml庫解析xpath網頁過程示例_python
- 2023-09-18 【解決】npm ERR A complete log of this run can be foun
- 2022-06-01 React函數式組件與類組件的不同你知道嗎_React
- 2022-03-31 C#客戶端HttpClient請求認證及數據傳輸_C#教程
- 2024-02-25 maven打包測試jar包沖突
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支