本文设计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
#include <iostream>
using 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 <iostream>
#include <string>
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 <iostream>
#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 <iostream>
#include "RMB.h"
#include "member.h"
#include "memberCard.h"
#include <string>
#include <fstream>
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 << " 余额:" <<balance << endl;
p = p->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 << " " <<p->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* 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;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。