软件工程第二次作业--文件读取

xxy02315 2024-03-03 17:02:09
这个作业属于哪个课程福州大学-2023软件工程实践
这个作业要求在哪里软件工程第二次作业--文件读取
这个作业的目标掌握文件读取、JSON解析
其他参考文献单元测试《git入门》

0、Gitcode项目地址

https://gitcode.net/xxdhd1/project-c

1、PSP表格

PSP预估耗时(分钟)实际耗时(分钟)
C++配置Json10600
获取Json6010
思考3030
编写代码120120
测试3010
得到exe2010
总计270780

2、功能实现

获取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();
}

3、测试

 4、测试结果

  

...全文
128 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜鸟求带飞_ 2024-03-13
  • 打赏
  • 举报
回复

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

122

社区成员

发帖
与我相关
我的任务
社区描述
FZU-SE
软件工程 高校
社区管理员
  • LinQF39
  • 助教-吴可仪
  • 一杯时间
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧