学生在校园能用“校园一卡通”进行多种消费,如进餐、洗澡和在校内超市购买商品等。学生校园消费信息包括消费“校园一卡通”卡号、持卡学生姓名、消费日期、消费类型及消费金额。试用C++设计一学生校园消费记录系统,使之能提供以下功能: 1.录入学生的校园消费信息; 2.修改某个学生的某次校园消费信息; 3.查询某个学生的校园消费情况; 4.删除某个学生的某次校园消费信息; 5.统计某段时间内,进行校园消费的学生姓名及消费金额,按消费金额由多到少排序; 6.按消费类型分类,统计某段时间内,在每种消费类型上学生的消费总额。 7.系统以菜单方式工作。
时间: 2024-02-05 12:11:24 浏览: 95
学生个人消费管理系统(C++)
5星 · 资源好评率100%
以下是C++实现的代码:
```
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 定义学生校园消费记录结构体
struct CampusRecord
{
string cardNum; // 校园一卡通卡号
string name; // 学生姓名
string date; // 消费日期
string type; // 消费类型
double amount; // 消费金额
};
// 定义学生校园消费记录系统类
class CampusRecordSystem
{
public:
void addRecord(); // 录入学生的校园消费信息
void modifyRecord(); // 修改某个学生的某次校园消费信息
void queryRecord(); // 查询某个学生的校园消费情况
void deleteRecord(); // 删除某个学生的某次校园消费信息
void statisticByAmount(); // 统计某段时间内,进行校园消费的学生姓名及消费金额,按消费金额由多到少排序
void statisticByType(); // 按消费类型分类,统计某段时间内,在每种消费类型上学生的消费总额
void showMenu(); // 显示菜单
private:
vector<CampusRecord> records; // 学生校园消费记录
};
// 显示菜单
void CampusRecordSystem::showMenu()
{
cout << "1. 录入学生的校园消费信息" << endl;
cout << "2. 修改某个学生的某次校园消费信息" << endl;
cout << "3. 查询某个学生的校园消费情况" << endl;
cout << "4. 删除某个学生的某次校园消费信息" << endl;
cout << "5. 统计某段时间内,进行校园消费的学生姓名及消费金额,按消费金额由多到少排序" << endl;
cout << "6. 按消费类型分类,统计某段时间内,在每种消费类型上学生的消费总额" << endl;
cout << "0. 退出系统" << endl;
cout << "请选择操作:";
}
// 录入学生的校园消费信息
void CampusRecordSystem::addRecord()
{
CampusRecord record;
cout << "请输入校园一卡通卡号:";
cin >> record.cardNum;
cout << "请输入学生姓名:";
cin >> record.name;
cout << "请输入消费日期:";
cin >> record.date;
cout << "请输入消费类型:";
cin >> record.type;
cout << "请输入消费金额:";
cin >> record.amount;
records.push_back(record);
cout << "录入成功!" << endl;
}
// 修改某个学生的某次校园消费信息
void CampusRecordSystem::modifyRecord()
{
string cardNum, date;
cout << "请输入要修改记录的校园一卡通卡号和消费日期:";
cin >> cardNum >> date;
bool found = false;
for (int i = 0; i < records.size(); i++)
{
if (records[i].cardNum == cardNum && records[i].date == date)
{
found = true;
cout << "请输入新的学生姓名:";
cin >> records[i].name;
cout << "请输入新的消费类型:";
cin >> records[i].type;
cout << "请输入新的消费金额:";
cin >> records[i].amount;
cout << "修改成功!" << endl;
break;
}
}
if (!found)
{
cout << "没有找到对应的记录!" << endl;
}
}
// 查询某个学生的校园消费情况
void CampusRecordSystem::queryRecord()
{
string cardNum;
cout << "请输入要查询记录的校园一卡通卡号:";
cin >> cardNum;
bool found = false;
for (int i = 0; i < records.size(); i++)
{
if (records[i].cardNum == cardNum)
{
found = true;
cout << "校园一卡通卡号:" << records[i].cardNum << endl;
cout << "学生姓名:" << records[i].name << endl;
cout << "消费日期:" << records[i].date << endl;
cout << "消费类型:" << records[i].type << endl;
cout << "消费金额:" << records[i].amount << endl;
}
}
if (!found)
{
cout << "没有找到对应的记录!" << endl;
}
}
// 删除某个学生的某次校园消费信息
void CampusRecordSystem::deleteRecord()
{
string cardNum, date;
cout << "请输入要删除记录的校园一卡通卡号和消费日期:";
cin >> cardNum >> date;
bool found = false;
for (int i = 0; i < records.size(); i++)
{
if (records[i].cardNum == cardNum && records[i].date == date)
{
found = true;
records.erase(records.begin() + i);
cout << "删除成功!" << endl;
break;
}
}
if (!found)
{
cout << "没有找到对应的记录!" << endl;
}
}
// 按消费金额排序
bool cmpByAmount(const CampusRecord& r1, const CampusRecord& r2)
{
return r1.amount > r2.amount;
}
// 统计某段时间内,进行校园消费的学生姓名及消费金额,按消费金额由多到少排序
void CampusRecordSystem::statisticByAmount()
{
string startDate, endDate;
cout << "请输入要统计的开始日期和结束日期:";
cin >> startDate >> endDate;
vector<CampusRecord> selectedRecords;
for (int i = 0; i < records.size(); i++)
{
if (records[i].date >= startDate && records[i].date <= endDate)
{
selectedRecords.push_back(records[i]);
}
}
sort(selectedRecords.begin(), selectedRecords.end(), cmpByAmount);
cout << "学生姓名\t消费金额" << endl;
for (int i = 0; i < selectedRecords.size(); i++)
{
cout << selectedRecords[i].name << "\t" << selectedRecords[i].amount << endl;
}
}
// 按消费类型分类
struct StatisticResult
{
string type;
double amount;
};
// 统计某段时间内,在每种消费类型上学生的消费总额
void CampusRecordSystem::statisticByType()
{
string startDate, endDate;
cout << "请输入要统计的开始日期和结束日期:";
cin >> startDate >> endDate;
vector<StatisticResult> results;
for (int i = 0; i < records.size(); i++)
{
if (records[i].date >= startDate && records[i].date <= endDate)
{
bool found = false;
for (int j = 0; j < results.size(); j++)
{
if (results[j].type == records[i].type)
{
found = true;
results[j].amount += records[i].amount;
break;
}
}
if (!found)
{
StatisticResult result;
result.type = records[i].type;
result.amount = records[i].amount;
results.push_back(result);
}
}
}
cout << "消费类型\t消费总额" << endl;
for (int i = 0; i < results.size(); i++)
{
cout << results[i].type << "\t" << results[i].amount << endl;
}
}
int main()
{
CampusRecordSystem system;
int choice;
do
{
system.showMenu();
cin >> choice;
switch (choice)
{
case 1:
system.addRecord();
break;
case 2:
system.modifyRecord();
break;
case 3:
system.queryRecord();
break;
case 4:
system.deleteRecord();
break;
case 5:
system.statisticByAmount();
break;
case 6:
system.statisticByType();
break;
case 0:
cout << "谢谢使用!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文