fix(提交): C 代码编译不过时,提交前的自动格式化把出错的几行并成一行,学生认不出自己的代码
Deploy / deploy (push) Canceled after 0s

clang-format 不认中文分号、缺了分号的语句,会把它和下一行并起来(int n; 和 scanf 成了
一行),报错的行号也对不上学生记得的位置。现在 C / C++ 格式化之前先套上题目模板、在
判题机上编一次,编不过就原样返回、不格式化,代码照常提交、照常判成编译错误。

api 容器里没有 gcc,判题机的编译器和参数本来就是判的标准,所以借它编。这一下不进判题
队列、不建提交记录;判题机连不上时照旧格式化。代价是每次交 C 多一次编译,零点几秒。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-30 11:29:37 +08:00
1 parent fa092d334c
commit c2ef55b10b
5 files changed
+51 -3

No files matched your search

+2 -1
View File
@@ -134,7 +134,7 @@ export function submitCode(data: SubmitCodePayload) {
return api.post<CreateSubmissionResponse>("submissions", data)
}
export function formatCode(data: { code: string; language: string }) {
export function formatCode(data: { code: string; language: string; problemId: number }) {
const languages: Record<string, string> = {
Python: "python",
C: "c",
@@ -144,6 +144,7 @@ export function formatCode(data: { code: string; language: string }) {
return api.post<FormatCodeResponse>("code/format", {
code: data.code,
language: languages[data.language] ?? data.language.toLowerCase(),
problemId: data.problemId,
})
}
+3 -1
View File
@@ -103,7 +103,8 @@ export const useSubmissionStore = defineStore("submission", () => {
formattedBeforeSubmit.value = false
// 0. 提交前自动格式化(Python 用 ruff,C/C++ 用 clang-format,SQL 用 sqlparse)。
// Python 在格式化之前先由服务端的 CPython 查一遍语法,有错就不提交
// Python 在格式化之前先由服务端的 CPython 查一遍语法,有错就不提交;
// C / C++ 先在判题机上编一遍,编不过就原样交(格式化会把错的几行并成一行)
const formatLang = LANGUAGE_FORMAT_VALUE[codeStore.code.language]
if (["python", "c", "cpp", "sql"].includes(formatLang)) {
isFormatting.value = true
@@ -111,6 +112,7 @@ export const useSubmissionStore = defineStore("submission", () => {
const res = await formatCode({
code: codeStore.code.value,
language: formatLang,
problemId: problem.id,
})
if (stale()) return
if (res.code !== codeStore.code.value) {