布尔代数求值程序tempArray值异常排查求助
布尔代数表达式求值程序的tempArray问题
我正在写一个求解A'B + C'这类布尔代数表达式的程序('表示对前一个变量取反)。现在tempArray总是存错误值,导致程序没法正确求值,我不知道问题出在哪。
我试过改tempArray的读取或创建方式,但还是没找到问题。我是编程新手,问题可能很明显,麻烦给点帮助。
是不是我给tempArray赋值的方式有问题?因为普通数组工作完全正常,我需要用tempArray拆分+分隔的项,先单独求值再做或运算。
输入输出示例
输入:
6 A'B+C'
程序输出:
RESULT IN EVAL TERM FUNCTION: 1 Term Result: 1 REAL ARRAY A'B+C' TEMP ARRAY ☺ ' ☺ 2 ⌂
原代码
#include <stdio.h> #include <stdbool.h> #include <string.h> bool evalTerm(int A, int B, int C, char *term); int main() { int s; int A = true; int B = true; int C = true; printf("Enter The Length of Your Function: "); scanf("%d", &s); char arr[s + 1]; printf("Enter a sum-of-products: "); scanf("%s", arr); char tempArray[s+1]; for(int i = 0; i<strlen(arr); i++) { if (arr[i] == '+' || arr[i] == '\0'){ for(int j = 0; j<i; j++) { if (arr[j] == 'A') { tempArray[j] = A; } if (arr[j] == 'B') { tempArray[j] = B; } if (arr[j] == 'C') { tempArray[j] = C; } if (arr[j] == '\'') { tempArray[j] = '\''; } } bool termResult = evalTerm(A,B,C,tempArray); printf("Term Result: %d\n", termResult); } } //get everything before first + and copy it to tep array, then evaluate and put in result array then get next terms and then OR result array at end printf("REAL ARRAY\n"); for (int i = 0; i < s; i++) { printf("%c", arr[i]); } printf("\n\n"); printf("TEMP ARRAY\n"); for (int i = 0; i < s; i++) { printf(" %c", tempArray[i]); } } bool evalTerm(int A, int B, int C, char *term){ for (int i = 0; i < strlen(term); i++) { if (term[i] == '\'') { switch (term[i - 1]) { case 'A': A = !A; break; case 'B': B = !B; break; case 'C': C = !C; break; } } } bool result = A && B && C; printf("RESULT IN EVAL TERM FUNCTION: %d\n", result); return result; }
问题分析与修复方案
核心问题点
- tempArray赋值错误:你把布尔变量的数值(
true=1、false=0)直接存入字符数组,这些值对应的是不可打印的ASCII控制字符,所以输出会出现乱码。正确做法是保留原表达式的字符,或者在求值时根据字符判断变量状态。 - 字符串未终止:传递给
evalTerm的tempArray没有添加字符串终止符\0,strlen会读取到内存垃圾数据,导致逻辑异常。 - 表达式拆分逻辑不完整:当前循环无法处理最后一个项(因为
arr[i] == '\0'不会触发),且没有重置tempArray来处理后续项。 - evalTerm逻辑错误:该函数错误修改了原始变量值,且默认返回三个变量的与,完全不符合布尔项的求值逻辑(比如
C'应该只返回取反后的C,而不是A&&B&&C)。
修复后的代码
#include <stdio.h> #include <stdbool.h> #include <string.h> // 求值单个布尔项(比如A'B、C') bool evalTerm(int A, int B, int C, char *term) { bool result = true; int len = strlen(term); for (int i = 0; i < len; i++) { bool currentVal = true; if (term[i] == 'A' || term[i] == 'B' || term[i] == 'C') { // 获取当前变量的原始值 switch (term[i]) { case 'A': currentVal = A; break; case 'B': currentVal = B; break; case 'C': currentVal = C; break; } // 检查下一个字符是否是取反符 if (i + 1 < len && term[i+1] == '\'') { currentVal = !currentVal; i++; // 跳过取反符,避免重复处理 } // 项内是与运算,逐个与 result = result && currentVal; } } printf("RESULT IN EVAL TERM FUNCTION: %d\n", result); return result; } int main() { int s; int A = true; int B = true; int C = true; printf("Enter The Length of Your Function: "); scanf("%d", &s); char arr[s + 1]; printf("Enter a sum-of-products: "); scanf("%s", arr); char tempArray[s + 1]; int start = 0; // 记录每个项的起始索引 bool finalResult = false; for (int i = 0; i <= strlen(arr); i++) { // 遇到+或者字符串结束,处理当前项 if (arr[i] == '+' || arr[i] == '\0') { // 复制当前项到tempArray strncpy(tempArray, arr + start, i - start); tempArray[i - start] = '\0'; // 添加字符串终止符 bool termResult = evalTerm(A, B, C, tempArray); printf("Term Result: %d\n", termResult); // 总结果是各项的或运算 finalResult = finalResult || termResult; start = i + 1; // 更新下一个项的起始索引 } } printf("\nFINAL RESULT: %d\n", finalResult); // 打印数组用于调试 printf("\nREAL ARRAY\n"); for (int i = 0; i < s; i++) { printf("%c", arr[i]); } printf("\n\nTEMP ARRAY (last term)\n"); printf("%s\n", tempArray); return 0; }
修复说明
- tempArray赋值:改用
strncpy直接复制原表达式中的项,保留字符格式,添加终止符确保字符串合法。 - 表达式拆分:用
start变量记录每个项的起始位置,遇到+或结束符时提取当前项,处理完后更新start,确保所有项都被处理。 - evalTerm重构:不再修改原始变量,而是逐个处理项中的变量,检查是否有取反符,项内做与运算,最终返回项的结果。
- 总结果计算:将每个项的结果做或运算,得到最终的布尔表达式结果。
内容的提问来源于stack exchange,提问作者Fanshiko
相关产品推荐
相关产品推荐

