Skip to content

Commit

Permalink
全部功能实现
Browse files Browse the repository at this point in the history
feat(Class-Widget-CSV-import): 实现 CSV 文件导入功能

- 新增 NumCsv2Json 函数,用于将 CSV 文件转换为 Class Widgets JSON 课表
- 更新窗口界面,支持选择 CSV 文件和转换操作
- 优化错误处理和用户提示信息
- 调整窗口大小和布局,改善用户体验


Signed-off-by: Detrital <[email protected]>
  • Loading branch information
Detritalw authored Dec 15, 2024
1 parent 00d4052 commit 4bd6f82
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
58 changes: 56 additions & 2 deletions Class-Widget-CSV-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,60 @@ void NumCsv2Json(HWND hWnd, HWND hEdit) {

wstring line;
// 逐行读取输入文件
getline(inputFile, line); // 读取第一行,获取节点数量
int nodeCount = stoi(line);

wstring partJson = L" \"part\": {\n";
wstring partNameJson = L" \"part_name\": {\n";

for (int i = 0; i < nodeCount; ++i) {
getline(inputFile, line);
wistringstream iss(line);
wstring token;
vector<wstring> tokens;

// 使用逗号作为分隔符
while (getline(iss, token, L',')) {
tokens.push_back(token);
}

// 检查是否有足够的列
if (tokens.size() < 3) {
// 显示消息框,提示发现错误,并提供选项:中止、继续和取消
int result = MessageBoxW(hWnd, (L"发现格式错误的行: " + to_wstring(i + 1) + L"\n请选择操作").c_str(), L"格式错误", MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
if (result == IDABORT) {
// 中止操作
MessageBoxW(hWnd, L"操作已中止", L"中止", MB_OK | MB_ICONINFORMATION);
return;
} else if (result == IDRETRY) {
// 继续处理下一行
continue;
} else if (result == IDIGNORE) {
// 取消操作
MessageBoxW(hWnd, L"操作已取消", L"取消", MB_OK | MB_ICONINFORMATION);
return;
}
}

// 处理节点名称、小时和分钟
wstring nodeName = tokens[0];
int hour = stoi(tokens[1]);
int minute = stoi(tokens[2]);

// 构建 part 和 part_name 的 JSON 字符串
partJson += L" \"" + to_wstring(i) + L"\": [" + to_wstring(hour) + L", " + to_wstring(minute) + L"]";
partNameJson += L" \"" + to_wstring(i) + L"\": \"" + nodeName + L"\"";

if (i < nodeCount - 1) {
partJson += L",\n";
partNameJson += L",\n";
}
}

partJson += L"\n },\n";
partNameJson += L"\n }\n";

// 处理剩余的 schedule 数据
while (getline(inputFile, line)) {
wistringstream iss(line);
wstring token;
Expand Down Expand Up @@ -155,13 +209,13 @@ void NumCsv2Json(HWND hWnd, HWND hEdit) {

// 完成 timeline 和 schedule 的 JSON 字符串
timelineJson += L"\n }\n },\n";
scheduleJson += L" }\n"; // 移除最后一个换行符
scheduleJson += L"\n },"; // 移除最后一个换行符

// 在倒数第二个 } 符号之前添加换行符
scheduleJson += L"\n";

// 构建输出字符串
wstring outputJson = L"{\n" + timelineJson + scheduleJson + L"}";
wstring outputJson = L"{\n" + timelineJson + scheduleJson + partJson + partNameJson + L"}";

// 写入输出文件
outputFile << outputJson;
Expand Down
Binary file modified Class-Widget-CSV-import.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions a.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2,
AM,8,30
PM,13,10
35,�μ�,a1,a2,a3,a4,a5,a6,a7,
55,�γ�,b1,b2,b3,b4,b5,b6,b7,
65,�μ�,c1,c2,c3,c4,c5,c6,c7,
Expand Down
14 changes: 14 additions & 0 deletions right课表.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,19 @@
"f6",
"f7"
]
},
"part": {
"0": [
8,
30
],
"1": [
13,
10
]
},
"part_name": {
"0": "AM",
"1": "PM"
}
}
63 changes: 62 additions & 1 deletion 课表.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,67 @@
}
},
"schedule": {

"0": [
"a1",
"a2",
"a3",
"a4",
"a5",
"a6",
"a7"
],
"1": [
"b1",
"b2",
"b3",
"b4",
"b5",
"b6",
"b7"
],
"2": [
"c1",
"c2",
"c3",
"c4",
"c5",
"c6",
"c7"
],
"3": [
"d1",
"d2",
"d3",
"d4",
"d5",
"d6",
"d7"
],
"4": [
"e1",
"e2",
"e3",
"e4",
"e5",
"e6",
"e7"
],
"5": [
"f1",
"f2",
"f3",
"f4",
"f5",
"f6",
"f7"
]
},
"part": {
"0": [8, 30],
"1": [13, 10]
},
"part_name": {
"0": "AM",
"1": "PM"
}
}

0 comments on commit 4bd6f82

Please sign in to comment.