122
社区成员
发帖
与我相关
我的任务
分享| 这个作业属于哪个课程 | 福州大学-2023软件工程实践 |
|---|---|
| 这个作业要求在哪里 | 软件工程第二次作业--文件读取 |
| 这个作业的目标 | 掌握文件读取、JSON解析 |
| 其他参考文献 | 单元测试、《git入门》 |
https://gitcode.net/xxdhd1/project-c
| PSP | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|
| C++配置Json | 10 | 600 |
| 获取Json | 60 | 10 |
| 思考 | 30 | 30 |
| 编写代码 | 120 | 120 |
| 测试 | 30 | 10 |
| 得到exe | 20 | 10 |
| 总计 | 270 | 780 |
获取input.txt中每一行命令,对于每个命令都会产生一个编号,不同编号传递到myoutput函数中对应不同的输出模式。
#include <iostream>
#include <fstream>
#include <string>
#include <json\json.h>
#include <unordered_map>
#include <vector>
using namespace std;
using namespace Json;
string input;
string output;
void myoutput(string file, int mod);
int main(int argc, char** argv)
{
input = argv[1];
output = argv[2];
ofstream ofs(output);
ofs.close();
unordered_map < string, int> command2mod;
{
command2mod["players"] = 0;
command2mod["result men 1m springboard"] = 1;
command2mod["result men 3m springboard"] = 1;
command2mod["result men 3m synchronised"] = 3;
command2mod["result men 10m platform"] = 1;
command2mod["result men 10m synchronised"] = 3;
command2mod["result woman 1m springboard"] = 1;
command2mod["result women 3m springboard"] = 1;
command2mod["result women 3m synchronised"] = 3;
command2mod["result women 10m platform"] = 1;
command2mod["result women 10m synchronised"] = 3;
command2mod["result men 1m springboard detail"] = 45;
command2mod["result men 3m springboard detail"] = 61;
command2mod["result men 3m synchronised detail"] = 39;
command2mod["result men 10m platform detail"] = 61;
command2mod["result men 10m synchronised detail"] = 39;
command2mod["result woman 1m springboard detail"] = 45;
command2mod["result women 3m springboard detail"] = 61;
command2mod["result women 3m synchronised detail"] = 39;
command2mod["result women 10m platform detail"] = 61;
command2mod["result women 10m synchronised detail"] = 39;
}
ifstream ifs(input);
char chs[1024];
while (true) {
ifs.getline(chs, 1024);
string str = chs;
if (str == "") break;
string test1 = "";
for (int i = 0; i < str.size() && str[i] != ' '; i++) {
test1 += str[i];
}
if (test1 != "players" && test1 != "result") {
ofstream ofs(output, ios::out | ios::app);
ofs << "Error\n-----\n";
ofs.close();
continue;
}
if (command2mod.count(str)) {
string file = str;
if (str[str.size() - 1] == 'l') {
file = file.substr(0, file.size() - 7);
}
file += ".json";
myoutput(file, command2mod[str]);
}
else {
ofstream ofs(output, ios::out | ios::app);
ofs << "N/A\n";
ofs.close();
}
ofstream ofs(output, ios::out | ios::app);
ofs << "-----\n";
ofs.close();
}
}
void myoutput(string file, int mod) {
ofstream ofs(output, ios::app | ios::out);
ifstream ifs(file);
Reader reader;
Value value;
reader.parse(ifs, value);
ifs.close();
if (mod == 0) {
for (int i = 0; i < value.size(); i++) {
Value sonValue = value[i]["Participations"];
for (int j = 0; j < sonValue.size(); j++) {
ofs << "Full Name:" << sonValue[j]["PreferredLastName"].asString() << " " << sonValue[j]["PreferredFirstName"].asString() << "\n";
ofs << "Gender:" << (sonValue[j]["Gender"].asInt() == 0 ? "Male" : "Female") << "\n";
ofs << "Country:" << sonValue[j]["NAT"].asString() << "\n";
ofs << "-----\n";
}
}
}
else {
if ((mod & (1 << 2)) == 0) {
Value results = value["Heats"][0]["Results"];
for (int i = 0; i < results.size(); i++) {
Value res = results[i];
ofs << "Full Name:";
if ((mod & (1 << 1)) == 0) {
ofs << res["FullName"].asString() << "\n";
}
else {
ofs << res["Competitors"][0]["FullName"].asString() << " & " << res["Competitors"][1]["FullName"].asString() << "\n";
}
ofs << "Rank:" << res["Rank"].asString()<<"\n";
ofs << "Score:";
for (int j = 0; j < res["Dives"].size(); j++) {
if (j) ofs << " + ";
ofs << res["Dives"][j]["DivePoints"].asString();
}
ofs << " = " << res["TotalPoints"].asString() << "\n";
ofs << "-----\n";
}
}
else {
unordered_map<string, vector<string>> name2grade;
int which = 6;
for (int i = 0; i < value["Heats"].size(); i++) {
Value heat = value["Heats"][i];
while ((mod & (1 << which)) == 0) which--;
for (int j = 0; j < heat["Results"].size(); j++) {
Value res = heat["Results"][j];
string name;
if ((mod & 1 << 1) == 0) {
name = res["FullName"].asString();
}
else
name = res["Competitors"][0]["FullName"].asString() + " & " + res["Competitors"][1]["FullName"].asString();
if (!name2grade.count(name)) {
name2grade[name] = { "*","*","*","*","*","*" };
}
name2grade[name][which - 3] = res["Rank"].asString();
string score;
for (int k = 0; k < res["Dives"].size(); k++) {
if (k) score += " + ";
score += res["Dives"][k]["DivePoints"].asString();
}
score += " = ";
score += res["TotalPoints"].asString();
name2grade[name][which] = score;
}
which--;
}
for (auto it = name2grade.begin(); it != name2grade.end(); it++) {
ofs << "Full Name:" << it->first << "\n";
ofs << "Rank:" << (it->second)[0] << " | " << (it->second)[1] << " | " << (it->second)[2] << "\n";
ofs << "Preliminary Score:" << (it->second)[3] << "\n";
ofs << "Semifinal Score:" << (it->second)[4] << "\n";
ofs << "Final Score:" << (it->second)[5] << "\n";
ofs << "-----\n";
}
}
}
ofs.close();
}
![]()

基本地完成了作业要求,但是细节方面还需要一些补充。首先作业缺少文件目录部分,关键代码缺少必要的注释;测试部分也需要添加一些必要的说明。希望将来继续加油哦!