// by lazy_cat #define _CRT_SECURE_NO_WARNINGS #include #include #define IsOpt(c) (c == '+' || c == '-' || c == '*' || c == '/') typedef struct _VAL_ITEM { int nValue; BOOL bAval; // 是否有效 } VAL_ITEM, *PVAL_ITEM; typedef struct _OPT_ITEM { char cOpt; BOOL bHasCal; // 是否计算 } OPT_ITEM, *POPT_ITEM; int GetValueIndex(PVAL_ITEM pValItem, int nOptIndex, int nMaxIndex, BOOL bIsLeft) { int nLeftIndex = nOptIndex, nRightIndex = nOptIndex + 1; switch (bIsLeft) { case TRUE: while (nLeftIndex >= 0) { if (pValItem[nLeftIndex].bAval) return nLeftIndex; nLeftIndex--; } break; case FALSE: while (nRightIndex <= nMaxIndex) { if (pValItem[nRightIndex].bAval) { pValItem[nRightIndex].bAval = FALSE; return nRightIndex; } nRightIndex++; } break; } return -1; } int CalWithOutBracket(char *szExp) { int nResult = 0; int nOptCount = 0; // 运算符个数 for (int i = 0; TRUE; i++) { if (IsOpt(szExp[i])) nOptCount++; if (!szExp[i]) break; } POPT_ITEM pOptItem = (POPT_ITEM)malloc(sizeof(OPT_ITEM) * nOptCount); PVAL_ITEM pValItem = (PVAL_ITEM)malloc(sizeof(VAL_ITEM) * (nOptCount + 1)); int nOptPos = 0, nValPos = 0; char szNum[16] = { 0 }; int nDest = 0; for (int i = 0; TRUE; i++) { if (szExp[i] >= '0' && szExp[i] <= '9') szNum[nDest++] = szExp[i]; else { if (nDest) { pValItem[nValPos].nValue = atoi(szNum); pValItem[nValPos].bAval = TRUE; nValPos++; nDest = 0; RtlZeroMemory(szNum, sizeof(szNum)); } if (IsOpt(szExp[i])) { pOptItem[nOptPos].cOpt = szExp[i]; pOptItem[nOptPos].bHasCal = FALSE; nOptPos++; } } if (!szExp[i]) break; } for (int i = 0; i < nOptCount; i++) { if (pOptItem[i].cOpt == '*' || pOptItem[i].cOpt == '/') { int nLeftIndex = GetValueIndex(pValItem, i, nOptCount, TRUE); int nRightIndex = GetValueIndex(pValItem, i, nOptCount, FALSE); switch (pOptItem[i].cOpt) { case '*': pValItem[nLeftIndex].nValue *= pValItem[nRightIndex].nValue; break; case '/': pValItem[nLeftIndex].nValue /= pValItem[nRightIndex].nValue; break; } pOptItem[i].bHasCal = TRUE; } } for (int i = 0; i < nOptCount; i++) { if (!pOptItem[i].bHasCal) { int nLeftIndex = GetValueIndex(pValItem, i, nOptCount, TRUE); int nRightIndex = GetValueIndex(pValItem, i, nOptCount, FALSE); switch (pOptItem[i].cOpt) { case '+': pValItem[nLeftIndex].nValue += pValItem[nRightIndex].nValue; break; case '-': pValItem[nLeftIndex].nValue -= pValItem[nRightIndex].nValue; break; } pOptItem[i].bHasCal = TRUE; } } nResult = pValItem[0].nValue; free(pOptItem); free(pValItem); return nResult; } void CalBracket(char *szExp, char cLeftBrc, char cRightBrc) { char *pStart = strchr(szExp, cLeftBrc); while (pStart) { char *pEnd = strchr(pStart, cRightBrc); char szPart[64]; RtlZeroMemory(szPart, sizeof(szPart)); strncpy(szPart, pStart + 1, pEnd - pStart - 1); if (!strchr(szPart, cLeftBrc)) { char szTmpExp[256] = { 0 }, szNum[16] = { 0 }; strncpy(szTmpExp, pStart + 1, pEnd - pStart - 1); sprintf(szNum, "%d", CalWithOutBracket(szTmpExp)); RtlZeroMemory(szTmpExp, sizeof(szTmpExp)); strncpy(szTmpExp, szExp, pStart - szExp); strcat(szTmpExp, szNum); strcat(szTmpExp, pEnd + 1); strcpy(szExp, szTmpExp); pStart = strchr(szExp, cLeftBrc); continue; } pStart = strchr(pStart + 1, cLeftBrc); } } void main() { SetConsoleTitle(TEXT("lazy_cat")); char szExp[256] = { 0 }; printf("输入要计算的表达式:"); scanf("%[^\n]s", szExp); CalBracket(szExp, '(', ')'); CalBracket(szExp, '[', ']'); CalBracket(szExp, '{', '}'); printf("计算结果:%d\n", CalWithOutBracket(szExp)); }