#include <stdio.h> #include <string.h>

#define N 4

int map(char ch) { char loca[7] = {'+', '*', 'i', '(', ')', '#', '\0'}; // 添加字符串结束符 char *p; p = strchr(loca, ch); if (p == NULL) return -1; else return p - loca; }

int main(int argc, char* argv[]) { char syn[15] = {'#', '\0'}; // 添加字符串结束符 int top = 0; int top_in = 1; int handle[10] = {0}; // 初始化为0 int top_h = -1; // 初始化为-1 char w; char exp[15]; int i_exp = 0; int prio[6][6] = { {3, 1, 1, 1, 3, 3}, {3, 3, 1, 1, 3, 3}, {3, 3, 0, 0, 3, 3}, {1, 1, 1, 1, 2, 0}, {3, 3, 0, 0, 3, 3}, {1, 1, 1, 1, 0, 4} }; int i, j; int code; char rules[N][10] = {" + ", " * ", "i", "( )"}; printf("please input your expression:"); scanf("%s", exp); w = exp[i_exp++]; while (1) { i = map(syn[top]); j = map(w); code = prio[i][j]; if (code == 0 || code == 4) break; if (code < 3) { syn[top_in] = w; if (code == 1) handle[++top_h] = top + 1; top = top_in++; w = exp[i_exp++]; } else { syn[top_in] = '\0'; // 添加字符串结束符 i = 0; while (strcmp(rules[i], syn + handle[top_h]) && i < N) i++; if (i == N) { code = 0; printf("err!"); break; } else { syn[handle[top_h]] = ' '; top = handle[top_h] - 1; top_in = handle[top_h] + 1; top_h--; } } } if (code) printf("OK!"); else printf("err!"); return 0;

修补并完善下面代码:#include stringh#define N 4int mapchar ch 	char loca7=+i#;	char p;	p=strchrlocach;	if p==NULL		return -1;	else		return p-loca;int mainint argc char argv	char syn15;

原文地址: https://www.cveoy.top/t/topic/hdOH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录