1003_我要通过!(20)

本文介绍了一个类似于浙江大学研究生入学考试中的机试题目,该题涉及通过C语言编程来解析特定格式的字符串,并根据解析结果判断字符串是否符合特定数学规律。详细介绍了程序结构、算法思路以及实现细节。
#include<stdio.h>
int main()
{
	int x,y,z;
	int n,i,j;
	char str[101];
	while(~scanf("%d",&n))
	{
		if(n<0||n>=10) continue;
		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			x=y=z=0;j=0;
			while(str[j]=='A') {
				x++; j++;
			}
			if(str[j++]!='P'){
				printf("NO\n");
				continue;
			}
			while(str[j]=='A'){
				y++; j++;
			}
			if(str[j++]!='T'){
				printf("NO\n");
				continue;
			}
			while(str[j]=='A'){
				z++;j++;
			}
			if(str[j]!='\0'){
				printf("NO\n");
				continue;
			}
			if(x*y!=z||y==0) printf("NO\n");
			else printf("YES\n");
		}
	}
}
这个题目跟浙大的一道研究生入学考试的机试题很像!每次做都有不一样的感受!做过的题再做,只是为了更加熟悉!
issue/ ├── config/ │ └── config.json # 主配置:频段、模式、功率表映射 ├── mode_config.py # 可选:额外的 rate_set 映射逻辑 ├── excel_to_clm.py # 主脚本 ├── templates/ │ ├── tx_limit_table.c.j2 # 输出 C 内容模板 │ └── clm_macros.h.j2 # 可选:输出 REGION 宏模板 ├── input/ │ └── Archer BE900US 2.xlsx # 输入 Excel │ └── wlc_clm_data_6726b0.c #需要插入内容的C文件 ├── output/ │ ├── tx_limit_table.c # 输出数据 │ └── clm_macros.h # 输出区域宏定义 └── README.md └── main.py ├── UI/ │ └── __init__.py │ └──ui_main_window.py ├── rate_set/ └──2G_20M_rate_set.c ~~~~速率集集合 我的文件目录如上 现在我要把生成tx_limit_table.c内容插入到wlc_clm_data_6726b0.c 的特定位置,并且这涉及到在数组中添加内容,在枚举里添加内容类似这样/** 2.4G 20M ext rate set indices */ enum rate_set_2g_20m_ext { RATE_SET_2G_20M_EXT_0 = 0, RATE_SET_2G_20M_EXT_1 = 1, RATE_SET_2G_20M_EXT_2 = 2, RATE_SET_2G_20M_EXT_3 = 3, RATE_SET_2G_20M_EXT_4 = 4, RATE_SET_2G_20M_EXT_5 = 5, RATE_SET_2G_20M_EXT_6 = 6, RATE_SET_2G_20M_EXT_7 = 7, RATE_SET_2G_20M_EXT_8 = 8, RATE_SET_2G_20M_EXT_9 = 9, RATE_SET_2G_20M_EXT_10 = 10, RATE_SET_2G_20M_EXT_11 = 11, RATE_SET_2G_20M_EXT_12 = 12, RATE_SET_2G_20M_EXT_13 = 13, RATE_SET_2G_20M_EXT_14 = 14, RATE_SET_2G_20M_EXT_15 = 15, RATE_SET_2G_20M_EXT_16 = 16, RATE_SET_2G_20M_EXT_17 = 17, RATE_SET_2G_20M_EXT_18 = 18, RATE_SET_2G_HT_20M_EXT_11b = 19, RATE_SET_2G_HT_20M_EXT_11g = 20, RATE_SET_2G_HT_20M_EXT_11n = 21, RATE_SET_2G_HT_20M_EXT_11AC = 22, RATE_SET_2G_HT_20M_EXT_11AX = 23, RATE_SET_2G_HT_20M_EXT_11BE = 24, }; static const unsigned short rate_sets_index_2g_20m_ext[] = { 0, 117, 234, 403, 576, 761, 950, 975, 1003, 1036, 1041, 1091, 1144, 1203, 1276, 1354, 1440, 1529, 1626, 1726, 1731, 1748, 1805, 1830, 1903, };我该怎么做能保证可检测性和鲁棒性
最新发布
10-15
你当前的需求是:**将 `excel_to_clm.py` 生成的 `tx_limit_table.c` 文件中的内容,插入到 `wlc_clm_data_6726b0.c` 的指定位置(如枚举、数组等),并确保操作具有可检测性和鲁棒性(robustness)**。 --- ### ✅ 核心目标 1. **精准定位插入点**:在 `wlc_clm_data_6726b0.c` 中识别出要插入的位置(如 `enum rate_set_2g_20m_ext` 和 `rate_sets_index_2g_20m_ext[]` 数组)。 2. **安全修改 C 源码**:不破坏原有结构,保留注释、格式。 3. **可检测性(Detectability)**:能判断是否已存在相同内容,避免重复插入。 4. **鲁棒性(Robustness)**:处理文件不存在、格式错误、编码问题等异常情况。 --- ## ✅ 推荐方案:使用“标记锚点 + 正则替换 + 增量更新” 我们通过在 `wlc_clm_data_6726b0.c` 中设置 **特殊注释标记** 来标识插入区域,然后用 Python 脚本进行安全替换。 --- ### 🔧 第一步:在 `wlc_clm_data_6726b0.c` 中添加插入锚点 ```c /** 2.4G 20M ext rate set indices */ // BEGIN_AUTOGENERATED_ENUM: rate_set_2g_20m_ext enum rate_set_2g_20m_ext { RATE_SET_2G_20M_EXT_0 = 0, RATE_SET_2G_20M_EXT_1 = 1, RATE_SET_2G_20M_EXT_2 = 2, RATE_SET_2G_20M_EXT_3 = 3, RATE_SET_2G_20M_EXT_4 = 4, RATE_SET_2G_20M_EXT_5 = 5, RATE_SET_2G_20M_EXT_6 = 6, RATE_SET_2G_20M_EXT_7 = 7, RATE_SET_2G_20M_EXT_8 = 8, RATE_SET_2G_20M_EXT_9 = 9, RATE_SET_2G_20M_EXT_10 = 10, RATE_SET_2G_20M_EXT_11 = 11, RATE_SET_2G_20M_EXT_12 = 12, RATE_SET_2G_20M_EXT_13 = 13, RATE_SET_2G_20M_EXT_14 = 14, RATE_SET_2G_20M_EXT_15 = 15, RATE_SET_2G_20M_EXT_16 = 16, RATE_SET_2G_20M_EXT_17 = 17, RATE_SET_2G_20M_EXT_18 = 18, RATE_SET_2G_HT_20M_EXT_11b = 19, RATE_SET_2G_HT_20M_EXT_11g = 20, RATE_SET_2G_HT_20M_EXT_11n = 21, RATE_SET_2G_HT_20M_EXT_11AC = 22, RATE_SET_2G_HT_20M_EXT_11AX = 23, RATE_SET_2G_HT_20M_EXT_11BE = 24, }; // END_AUTOGENERATED_ENUM: rate_set_2g_20m_ext static const unsigned short rate_sets_index_2g_20m_ext[] = { // BEGIN_AUTOGENERATED_ARRAY: rate_sets_index_2g_20m_ext 0, 117, 234, 403, 576, 761, 950, 975, 1003, 1036, 1041, 1091, 1144, 1203, 1276, 1354, 1440, 1529, 1626, 1726, 1731, 1748, 1805, 1830, 1903, // END_AUTOGENERATED_ARRAY: rate_sets_index_2g_20m_ext }; ``` > ✅ 这些 `BEGIN/END_AUTOGENERATED_*` 是你的“锚点”,用于自动化脚本识别插入区域。 --- ### 🛠️ 第二步:Python 脚本实现插入逻辑(推荐写入 `main.py`) ```python # main.py import os import re def read_file(filepath): """安全读取文件""" if not os.path.exists(filepath): raise FileNotFoundError(f"文件不存在: {filepath}") try: with open(filepath, 'r', encoding='utf-8') as f: return f.read() except Exception as e: raise RuntimeError(f"读取文件失败: {e}") def write_file(filepath, content): """安全写入文件""" try: with open(filepath, 'w', encoding='utf-8') as f: f.write(content) print(f"✅ 成功写入: {filepath}") except Exception as e: raise RuntimeError(f"写入文件失败: {e}") def insert_content_at_marker(source_code, marker_name, new_content, indent=0): """ 在 BEGIN/END 标记之间插入内容 :param source_code: 原始代码字符串 :param marker_name: 标记名,如 "rate_set_2g_20m_ext" :param new_content: 要插入的新内容(字符串) :param indent: 缩进空格数(用于对齐) """ begin_pattern = rf"(//\s*BEGIN_AUTOGENERATED_{marker_name}.*?\n)(.*?)\s*(//\s*END_AUTOGENERATED_{marker_name})" match = re.search(begin_pattern, source_code, re.DOTALL) if not match: raise ValueError(f"未找到标记区域: {marker_name}") before = match.group(1) after = match.group(3) # 缩进处理 indented_content = '\n'.join(' ' * indent + line for line in new_content.strip().split('\n')) replacement = f"{before}{indented_content}\n{after}" updated_code = source_code[:match.start()] + replacement + source_code[match.end():] print(f"🔄 已更新区域: {marker_name}") return updated_code def load_generated_file(filepath): """加载生成的 tx_limit_table.c 内容(提取你需要的部分)""" content = read_file(filepath) # 示例:提取整个文件内容(你可以按需提取 enum 或 array 部分) return content.strip() def main(): # 路径配置 input_c_file = "input/wlc_clm_data_6726b0.c" generated_c_file = "output/tx_limit_table.c" # 由 excel_to_clm.py 生成 output_c_file = "output/wlc_clm_data_6726b0_modified.c" # 输出新文件或覆盖原文件 try: # 1. 读取原始 C 文件 source_code = read_file(input_c_file) # 2. 加载自动生成的内容(假设它包含完整的 enum 和数组定义) generated_content = load_generated_file(generated_c_file) # 3. 插入 enum(示例只插入一个,可根据需要扩展) source_code = insert_content_at_marker( source_code, "rate_set_2g_20m_ext", "enum rate_set_2g_20m_ext {\n RATE_SET_2G_20M_EXT_0 = 0,\n RATE_SET_2G_20M_EXT_1 = 1,\n // ... 更多条目\n};", indent=4 ) # 4. 插入数组 source_code = insert_content_at_marker( source_code, "rate_sets_index_2g_20m_ext", " 0, 117, 234, 403, 576, 761, 950, 975, 1003, 1036,\n 1041, 1091, 1144, 1203, 1276, 1354, 1440, 1529, 1626, 1726,\n 1731, 1748, 1805, 1830, 1903", indent=4 ) # 5. 写回文件 write_file(output_c_file, source_code) print("🎉 所有内容已成功插入!") except Exception as e: print(f"❌ 操作失败: {e}") exit(1) if __name__ == "__main__": main() ``` --- ### ✅ 可检测性与鲁棒性保障措施 | 特性 | 实现方式 | |------|----------| | **可检测性** | 使用唯一标记(如 `BEGIN_AUTOGENERATED_enum_xxx`),便于脚本识别是否已有内容 | | **防重复插入** | 替换而非追加,每次运行都覆盖中间内容,避免重复 | | **异常处理** | 捕获文件不存在、读写权限、编码错误等 | | **正则安全匹配** | 使用 `re.DOTALL` 匹配多行,精确捕获 BEGIN-END 区域 | | **备份建议** | 可先复制原文件为 `.bak` 再修改 | --- ### 💡 扩展建议 1. **支持多个枚举/数组自动注入**: - 将 `marker -> file_section` 映射写入 `config.json` - 动态解析每个区域 2. **diff 检测变化**: ```python import difflib diff = difflib.unified_diff(old.splitlines(), new.splitlines()) print('\n'.join(diff)) ``` 3. **日志记录变更内容** 4. **Git 集成**:提交变更前自动检查格式 --- ### ✅ 总结 你现在可以通过以下流程安全地插入内容: 1. 在 C 文件中加入 `BEGIN/END_AUTOGENERATED` 锚点 2. 使用 Python 正则匹配这些锚点 3. 安全替换中间内容 4. 添加异常处理和日志输出 这样既保证了**可维护性**,也具备良好的**可检测性与鲁棒性**。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值