用c++写一个能记录个人使用轨迹,如使用时长,单词范围等,给设计鼓励体系,在不同的阶段颁发不同的鼓励徽章的代码
时间: 2023-12-03 13:43:22 浏览: 32
以下是一个简单的示例代码,用于记录个人使用轨迹和颁发不同的鼓励徽章。该代码可以用于记录用户在一个特定应用程序中的使用情况,并根据他们的使用情况颁发适当的鼓励徽章。
```
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
// 定义枚举类型,表示不同的鼓励徽章
enum Badge {
BRONZE,
SILVER,
GOLD
};
// 定义一个结构体,表示个人使用轨迹
struct Usage {
int time;
int words;
};
int main() {
// 打开文件,读取个人使用轨迹
ifstream fin("usage.txt");
map<string, Usage> usage;
string name;
int time, words;
while (fin >> name >> time >> words) {
usage[name] = {time, words};
}
fin.close();
// 统计个人使用情况,判断应该颁发哪个鼓励徽章
map<string, Badge> badges;
for (auto& p : usage) {
if (p.second.time >= 60 && p.second.words >= 1000) {
badges[p.first] = GOLD;
} else if (p.second.time >= 30 && p.second.words >= 500) {
badges[p.first] = SILVER;
} else if (p.second.time >= 10 && p.second.words >= 100) {
badges[p.first] = BRONZE;
}
}
// 输出个人使用轨迹和鼓励徽章
for (auto& p : usage) {
cout << p.first << ": " << p.second.time << " min, " << p.second.words << " words";
if (badges.find(p.first) != badges.end()) {
cout << ", Badge: ";
if (badges[p.first] == BRONZE) {
cout << "BRONZE";
} else if (badges[p.first] == SILVER) {
cout << "SILVER";
} else if (badges[p.first] == GOLD) {
cout << "GOLD";
}
}
cout << endl;
}
// 将个人使用轨迹和鼓励徽章写入文件
ofstream fout("usage.txt");
for (auto& p : usage) {
fout << p.first << " " << p.second.time << " " << p.second.words;
if (badges.find(p.first) != badges.end()) {
if (badges[p.first] == BRONZE) {
fout << " BRONZE";
} else if (badges[p.first] == SILVER) {
fout << " SILVER";
} else if (badges[p.first] == GOLD) {
fout << " GOLD";
}
}
fout << endl;
}
fout.close();
return 0;
}
```
该代码使用了 STL 中的 `map` 和枚举类型 `Badge`,用于存储个人使用轨迹和鼓励徽章。程序首先从文件中读取个人使用轨迹,并统计个人使用情况,根据使用情况判断应该颁发哪个鼓励徽章。然后输出个人使用轨迹和鼓励徽章,并将它们写回文件中。
注意,该代码只是一个简单的示例,实际应用中需要根据具体需求进行修改和扩展。例如,可以添加更多的使用统计指标,或者根据不同的使用情况颁发不同的鼓励徽章。