Beispiel #1
0
int main()
{
    Stack S = CreateStack(2*numberOfOperator);
    char string[1000];
    fgets(string,1000,stdin);
    char* str =NULL;
    str = strtok(string, " \n");
    while(str != NULL) {
        if(notOp(str[0]))
        {
            Token t= malloc(sizeof(struct token));
            t->str=str;
            t->Type = Term;
            Push(t,S);
        }
        else {
            Operator Op = str[0];
            Token token2 = TopAndPop(S);
            Token token1 = TopAndPop(S);
            Token token3 = concatenate(token1,token2,Op);
            Push(token3,S);
        }
        str = strtok(NULL, " \n");
    }
    printf("%s\n",TopAndPop(S)->str);
    return 0;
}
double midfixCal(void)
{
int flag;
unsigned char ope[5];
unsigned char pre_ope;
double term;
double op1,op2;
int priv,pre_priv;//优先级

Stack M = CreateStack( StackLen );//这个栈用来将中缀表达式转换成后缀
Stack S = CreateStack( StackLen );//这个栈用来计算后缀表达式的值

printf("Enter the midfix expression only + * - / () allowed.\n");

while(1){
	flag = scanf("%[+*-/()=]",ope);
	if(flag == 0){
		if(scanf("%lf",&term) == 1){
			getchar();
			Push( term ,S);
		}
	}
	else if( ope[0] == '+' || ope[0] == '*' || ope[0] == '-' || ope[0] == '/' || ope[0] == '(' ){
		getchar();
		priv = AssignPriv( ope[0] );

		while(!IsEmpty(M)){
			pre_ope = (unsigned char)Top(M);
			pre_priv = AssignPriv(pre_ope);

			if(pre_priv >= priv && pre_ope != '(' ){
				Pop(M);
				CalPostFix(pre_ope,S);//计算后缀表达式的值
			}
			else
				break;
		}
		Push( (double)ope[0],M);
	}
	else if( ope[0] == ')'){
		getchar();
		//将栈中的元素直到 '('弹出
		while( ( (unsigned char)Top(M) ) != '('){
			pre_ope = TopAndPop(M);
			CalPostFix(pre_ope, S);
		}
		Pop(M);//弹出'('
	}
	else if( ope[0] == '=' ){
		while(!IsEmpty(M)){
			pre_ope = TopAndPop(M);
			CalPostFix(pre_ope,S);
		}
		return TopAndPop(S);//返回S栈顶的元素,即是最终结果	
	}
}//while结束
}
Beispiel #3
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);
}
Beispiel #4
0
int main(){
    Stack stack;
    stack = CreateStack(1000);
    int temp;
    for(temp=0;temp<100;temp++){
        Push(temp, stack);
    }
    while(!IsEmpty(stack)){
        printf("%d\n", TopAndPop(stack));
    }
    return 0;
}
Beispiel #5
0
int main()
{
	char c;
	Stack S = Create(SIZE);
	while (scanf("%c", &c) != EOF)
	{
		int a, b;	
		switch (c)
		{
			case '+':
				b = TopAndPop(S);
				a = TopAndPop(S);
				Push(a+b, S);
				break;
			case '-':
				 b = TopAndPop(S);
				 a = TopAndPop(S);
				Push(a-b, S);
				break;
			case '*':
				 b = TopAndPop(S);
				 a = TopAndPop(S);
				Push(a*b, S);
				break;
			case '/':
				 b = TopAndPop(S);
				 a = TopAndPop(S);
				Push(a/b, S);
				break;
			default:
				Push(ToInt(c), S);
				break;	
		}
		getchar();
	}

	int ans = TopAndPop(S);
	printf("%d\n", ans);

	Dispose(S);
	
	return 0;
}	
Beispiel #6
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;
}
void CalPostFix(unsigned char ope,Stack S)
{
	double op1,op2;
	if( ope == '+' ){
		op2 = TopAndPop( S );
		op1 = TopAndPop( S );
		Push(op1 + op2, S );
	}
	else if( ope == '*'){
		op2 = TopAndPop( S );
		op1 = TopAndPop( S );
		Push(op1 * op2, S );
	}
	else if( ope == '-'){
		op2 = TopAndPop( S );
		op1 = TopAndPop( S );
		Push(op1 - op2, S );
	}
	else if( ope == '/'){
		op2 = TopAndPop( S );
		op1 = TopAndPop( S );
		Push(op1 / op2, S );
	}
}
int main()
{
    ArrayStack s;
    ElementType i;
    ElementType tmp;

    s = CreateArrayStack(10);
    printf("Print Stack: ");
    PrintArrayStack(s);
    if(IsEmpty(s)) {
        printf("\nEmpty stack.\n");
    }
    if(IsFull(s)) {
        printf("\nFull stack.\n");
    }
    printf("\n\n");

    for(i = 0; i < 10; i++) {
        printf("Push %d into stack. \n", i);
        Push(i, s);
    }
    printf("\n\n");
    printf("Print Stack: ");
    PrintArrayStack(s);
    printf("Top of stack: %d\n", Top(s));
    if(IsEmpty(s)) {
        printf("\nEmpty stack.\n");
    }
    if(IsFull(s)) {
        printf("\nFull stack.\n");
    }
    printf("\n\n");

    for(i = 0; i < 5; i++){
        tmp = TopAndPop(s);
        printf("Top of stack: %d\n", tmp);
        printf("Pop out of stack: %d\n", tmp);
    }

    printf("\n\n");
    printf("Print Stack: ");
    PrintArrayStack(s);
    if(IsEmpty(s)) {
        printf("\nEmpty stack.\n");
    }
    if(IsFull(s)) {
        printf("\nFull stack.\n");
    }
    printf("Top of stack: %d\n", Top(s));


    printf("\n\nMake Empty Stack!\n");
    MakeEmpty(s);
    if(IsEmpty(s)) {
        printf("\nEmpty stack.\n");
    }
    if(IsFull(s)) {
        printf("\nFull stack.\n");
    }


    DisposeArrayStack(s);
    return 0;
}