//输入为后缀表达式
PtrToNode CreateExprTree(const char *ExprStr)
{
	int i;
	Stack S;
	PtrToNode pTmp;

	S = CreateStack(50);
	for(i=0; ExprStr[i] != '\0'; ++i)
	{
		pTmp = (PtrToNode)malloc(sizeof(struct Node));
		if(ExprStr[i]!='+' && ExprStr[i]!='-' && ExprStr[i]!='*' && ExprStr[i]!='/')
		{
			pTmp->value = ExprStr[i];
			pTmp->pLeft = NULL;
			pTmp->pRight = NULL;
			Push(S, pTmp);
		}
		else
		{
			pTmp->value = ExprStr[i];
			pTmp->pRight = Pop(S);
			pTmp->pLeft = Pop(S);
			Push(S, pTmp);
		}
	}
	pTmp = Pop(S);
	DisposeStack(S);
	return pTmp;
}
Example #2
0
//3.18a
void languageExamine(char *s)
{
	Stack S = CreateStack(100);

	while (*s != '\0') {
		switch (*s) {
			case 'b':
				if (*(s + 1) == 'e' || *(s + 2) == 'g' || *(s + 3) == 'i' || *(s + 4) == 'n')
					Push('b', S);
				break;
			case '(':
				Push('(', S);
				break;
			case '[':
				Push('[', S);
				break;
			case '{':
				Push('{', S);
				break;
			case 'e':
				if (*(s + 1) == 'n' || *(s + 2) == 'd')
					if (TopAndPop(S) != 'b') {
						printf("warning: losing \"begin\" before \"end\"\n");
						return;
					}
				break;
			case ')':
				if (TopAndPop(S) != '(') {
					printf("warning: losing \"(\" before\")\"\n");
					return;
				}
				break;
			case ']':
				if (TopAndPop(S) != '[') {
					printf("warning: losing \"[\" before\"]\"\n");
					return;
				}
				break;
			case '}':
				if (TopAndPop(S) != '{') {
					printf("warning: losing \"{\" before\"}\"\n");
					return;
				}
				break;
			default:
				break;
		}
		s++;
	}
	if (!IsEmpty(S))
		printf("Wrong Syntax! Something missing but tired to check:)\n");
	else
		printf("Correct Sytax\n");
	
	DisposeStack(S);
}
Example #3
0
main ( )
{
	Stack s;
	int i ;
	
	s = CreateStack ( 10 ) ;

	for ( i = 1; i < 10; i++ ) 
		Push ( i, s ) ;
	for ( i = 1; i < 10; i++ )  {
		printf ( "Element: %d\n", Top ( s ) ) ;
		Pop ( s ) ;
	}
	DisposeStack ( s ) ;
	return 0 ;
}
Example #4
0
int main(void){
	Stack S;
	int i;

	S = CreateStack();
	for(i=0;i<10;i++)
		Push(i,S);
	while(!IsEmpty(S)){
		printf("%d\n",Top(S));
		Pop(S);
	}
	for(i=0;i<10;i++)
		Push(i,S);
	while(!IsEmpty(S)){
		printf("%d\n",Top(S));
		Pop(S);
	}
	DisposeStack(S);
	return 0;
}
Example #5
0
//3.19 postfixCalculator()
ElementType postfixCalculator(char *s[])
{
	Stack S = CreateStack(100);

	while (*s != NULL) {
		if (!strcmp(*s, "+"))
			Push(TopAndPop(S) + TopAndPop(S), S);
		else if (**s == '*')
			Push(TopAndPop(S) * TopAndPop(S), S);
		else if (**s == '-')
			Push(-TopAndPop(S) + TopAndPop(S), S);
		else if (**s == '/') {
			ElementType tmp = TopAndPop(S);
			Push(TopAndPop(S) / tmp, S);
		}
		else
			Push(atoi(*s), S);
		s++;
	}

	ElementType x = TopAndPop(S);
	DisposeStack(S);
	return x;
}