Beispiel #1
0
void ConvToRPNExp(char exp[LEN])
{
	Stack stack;
	int expLen = strlen(exp);
	char convExp[LEN];

	int i, idx = 0;
	int flag_d = 0;
	char tok, popOp;
	
	StackInit(&stack);

	for(i=0; i<expLen; i++)
	{
		tok = exp[i];
		if (isdigit(tok)){
			convExp[idx++] = tok;
		}
		else if (tok == '.'){
			convExp[idx++] = tok;
		}
		else
		{
			convExp[idx++] = ' ';
			switch(tok)
			{
			case '(':
				SPush(&stack, tok);
				break;

			case ')':
				while(1)
				{
					popOp = SPop(&stack);
					if(popOp == '(')
						break;
					convExp[idx++] = popOp;
				}
				break;

			case '+': case '-': 
			case '*': case '/':
				while(!SIsEmpty(&stack) && 
						WhoPrecOp(SPeek(&stack), tok) >= 0)
					convExp[idx++] = SPop(&stack);

				SPush(&stack, tok);
				break;
			}
		}
	}

	while(!SIsEmpty(&stack))
		convExp[idx++] = SPop(&stack);

	convExp[idx] = '\0';

	strcpy(exp, convExp);
}
Beispiel #2
0
void DFShowGraphVertex(ALGraph* pg, int startV)
{
	Stack stack;
	int visitV = startV;
	int nextV;

	StackInit(&stack);
	VisitVirtex(pg, visitV);
	SPush(&stack, visitV);

	
	while( LFirst(&(pg->adjList[visitV]), &nextV) == TRUE ) 
	{
		int visitFlag = FALSE;

		if(VisitVirtex(pg, nextV) == TRUE)  // if nextV is the virtex that was visited 
		{
			SPush(&stack, visitV);			// Push nextV in the stack
			visitV = nextV;
			visitFlag = TRUE;
		}
		else	// if nextV is not the virtex that was visited before   
		{
			while(LNext(&(pg->adjList[visitV]),&nextV) == TRUE)
			{
				if( VisitVirtex(pg, nextV) == TRUE) 
				{
					SPush(&stack, visitV);
					visitV = nextV;
					visitFlag = TRUE;
					break;
				}
			}

		}

		if(visitFlag == FALSE)
		{
			if(SIsEmpty(&stack) == TRUE)
				break;
			else 
				visitV = SPop(&stack);
		}

	}


	memset(pg->visitInfo, 0, sizeof(int) * pg->numOfVertex);

}
Beispiel #3
0
int infix2postfix(char *p) {
	// inplace replacing, newp will point to the end of the new expression.
	char *newp = p;
	int opr;
	stack op_stk = SNew(TYPE_INT);
	for(; *p != '\0'; ++p) {
		switch(OPNUM(*p)) {
			case IS_OPERATOR: // beware it's a GNU extension.
				if(*p == ')') {
					while(SPop(op_stk, &opr), opr != '(')
						*(newp++) = opr;
					break;
				}
				else if(SEmpty(op_stk));
				else if(isp[OPNUM(STop(op_stk))] >= icp[OPNUM(*p)]) {
					SPop(op_stk, &opr);
					*(newp++) = opr;
				}
				SPush(op_stk, *p);
				break;
			default:
				*(newp++) = *p;
		}
	}
	while(!SEmpty(op_stk)) {
		SPop(op_stk, &opr);
		*(newp++) = opr;
	}
	*newp = '\0';
	return 0;
}
int main()
{
	Stack stack;
	StackInit(&stack);

	SPush(&stack, 1);SPush(&stack, 2);
	SPush(&stack, 3);SPush(&stack, 4);
	SPush(&stack, 5);

	while(!SIsEmpty(&stack))
		printf("%d ", SPop(&stack));

	printf("\n");

	return 0;
}
int main(void)
{
	// Stack의 생성 및 초기화 ///////
	Stack stack;
	StackInit(&stack);

	// 데이터 넣기 ///////
	SPush(&stack, 1);  SPush(&stack, 2);
	SPush(&stack, 3);  SPush(&stack, 4);
	SPush(&stack, 5);

	// 데이터 꺼내기 ///////
	while (!SIsEmpty(&stack))
		printf("%d ", SPop(&stack));

	return 0;
}
Beispiel #6
0
int evaluate_postfix(char *p) {
	stack opr_stk = SNew(TYPE_INT);
	int a, b, c;
	for(; *p != '\0'; ++p) {
		switch(OPNUM(*p)) {
			case IS_OPERATOR: // beware it's a GNU extension.
				if(SPop(opr_stk, &b) == STACK_ERROR) EV_ERR_RET(-1);
				if(SPop(opr_stk, &a) == STACK_ERROR) EV_ERR_RET(-1);
				if(eval(a, b, &c, OPNUM(*p)) == -1)	 EV_ERR_RET(-1);
				if(SPush(opr_stk, c) == STACK_ERROR) EV_ERR_RET(-1);
				break;
			case -1:
				if(SPush(opr_stk, *p - '0') == STACK_ERROR) EV_ERR_RET(-1);
				break;
			default: EV_ERR_RET(-1); // should not be here
		}
	}
	if(SPop(opr_stk, &a) == STACK_ERROR) EV_ERR_RET(-1);
	return a;
}
Beispiel #7
0
int match_parentheses(char *p) {
	int op;
	stack pr_stk = SNew(TYPE_INT);
	for(; *p != '\0'; ++p) {
		if(*p == '(' || *p == '[' || *p == '{')
			SPush(pr_stk, *p);
		else if(*p == ')' || *p == ']' || *p == '}') {
			if(SEmpty(pr_stk)) return -1;
			SPop(pr_stk, &op);
			if(*p == ')' && op != '(') return -1;
			if(*p ==']' && op != '[') return -1;
			if(*p =='}' && op != '{') return -1;
		}
	}
	if(!SEmpty(pr_stk)) return -1;
	return 0;
}