From ac5855208caa8ee0667561c7bb4a674818490a3c Mon Sep 17 00:00:00 2001 From: liusiyang Date: Mon, 19 Oct 2026 17:46:23 +0800 Subject: [PATCH] =?UTF-8?q?fix=20-f=E6=89=B9=E9=87=8F=E8=B7=91=E5=8D=A1?= =?UTF-8?q?=E4=BD=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/deal.h | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/example/deal.h b/example/deal.h index c4f7681..41d57ca 100644 --- a/example/deal.h +++ b/example/deal.h @@ -391,19 +391,33 @@ struct Det_File_Result_Info } for (size_t i = 0; i < qx_result_list.size(); i++) { - float fr = qx_result_list.at(i).num * 1.0 / qx_all_num * 100; + // 除零保护:没有任何缺陷统计时百分比按 0 处理 + float fr = (qx_all_num > 0) ? (qx_result_list.at(i).num * 100.0f / qx_all_num) : 0.0f; std::string str_qx = ""; - // printf("len %d \n", qx_result_list.at(i).qx_name.length()); - for (int k = 0; k < (20 - qx_result_list.at(i).qx_name.length()); k++) + // 名称补空格对齐到 20 字节。 + // 注意:qx_name.length() 返回 size_t(无符号),若写成 20 - length(), + // 当名称超过 20 字节时(中文缺陷名如"铜箔面不良标签"为 21 字节)会下溢成 + // 巨大无符号值,导致循环近似死循环(-f 批量卡住的根因)。 + size_t nNameLen = qx_result_list.at(i).qx_name.length(); + if (nNameLen < 20) { - str_qx += " "; + str_qx.append(20 - nNameLen, ' '); } str_qx += qx_result_list.at(i).qx_name; char buffer123[64]; sprintf(buffer123, ":%d %.1f%% ", qx_result_list.at(i).num, fr); std::string st1 = buffer123; str_qx += st1; - int nlen = fr; + // 百分比条长度限制在 [0,100],避免异常值造成超长字符串 + int nlen = static_cast(fr); + if (nlen < 0) + { + nlen = 0; + } + if (nlen > 100) + { + nlen = 100; + } for (int j = 0; j < nlen; j++) {