Пример #1
0
int suffix_cal(char exp[])
{
    stack temp;
    stack s;
    stack s2;
    init_stack(&temp, 100);
    init_stack(&s, 100);
    init_stack(&s2, 100);
    while(*exp != '\0') {
	push(&temp, *exp);
	exp++;
    }
    while(stack_length(&temp) != 0)
	push(&s, pop(&temp));
    while(stack_length(&s) != 0) {
	push(&s2, pop(&s));
	if(gettop(&s2) == '+' ||gettop(&s2) == '-'||gettop(&s2)=='*'||gettop(&s2) == '/') {
	    char ch3 = pop(&s2);
	    char ch2 = pop(&s2);
	    char ch1 = pop(&s2);
	    int r = calculate(ch1, ch2, ch3);
	    char ch = r + 48;
	    push(&s2, ch);
	}
    }
    return gettop(&s2) - 48;
}
Пример #2
0
int symbolmatching( const char* str )
{
	struct NODE* stack = NULL;
	if( 0 != push( &stack, (int)'#' ) )
		return -1;

	while( '\0' != *str )
	{
		char ch = 0;
		if( 0 != gettop( &stack, (int*)&ch ) )
			return -1;
		int flag = 0;
		switch( *str )
		{
			case ')':
				if( '(' == ch )
					flag = 1;
				break;
			case '}':
				if( '{' == ch )
					flag = 1;
				break;
			case ']':
				if( '[' == ch )
					flag = 1;
				break;	
		}
		if( 1 == flag )
		{
			if( 0 != pop( &stack, (int*)&ch ) )
				return -1;			
		} else {
			if( 0 != push( &stack, (int)*str ) )
				return -1;
		}
		++str;	
	}

	char ch = 0;
	if( 0 != gettop( &stack, (int*)&ch ) )
		return -1;
	if( '#' == ch )
	{
		if( 0 != pop( &stack, (int*)&ch ) )
			return -1;			
	}

	return isempty( &stack );
}
Пример #3
0
bool  LuaWrapper::callLuaFunc(const char* szTableName, const char* szFuncName, const char* sig, ...)
{
	int nIndexTop = gettop();
	va_list vl;
	va_start(vl, sig);
	bool bRet = true;

	if (!loadTable(szTableName))
	{
		luaL_error(m_luaState, "load table[%s] failed.", szTableName);
		bRet = false;
	}
	lua_pushstring(m_luaState, szFuncName);
	lua_gettable(m_luaState, -2);// key从栈弹出,并获取 table[key] 的值压入栈

	if (!lua_isfunction(m_luaState, -1))
	{
		luaL_error(m_luaState, "table [%s] field [ %s ] isn't an function.", szTableName, szFuncName);
		bRet = false;
	}
	else
	{

		bRet = doCallFunction(sig, vl);
	}

	va_end(vl);
	settop(nIndexTop);

	return bRet;
}
Пример #4
0
int main(){
	int type;
	double op2;
	char s[MAXOP];
	
	while((type = gettop(s)) != EOF){
		switch(type){
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				push(pop() / op2);
				break;
			case '\n':
				printf("\t%.8g\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
		
	}
	return 0;
}
Пример #5
0
	packet from_network_layer(void){

		packet p;

		p=gettop();
		dequeue();
		next_net_pkt++;

	return p;
	}
Пример #6
0
void transform(stack *suffix, char exp[])
{
    stack s;
    init_stack(&s, 100);
    push(&s, '#');
    while(*exp != '#') {
	if(*exp != '+' && *exp != '-' && *exp != '*' && *exp != '/') {
	    push(suffix, *exp);
	}else if(stack_length(&s) != 0 && priority(*exp) > priority(gettop(&s))){
	    push(&s, *exp);
	}else {
	    while(stack_length(&s) != 1 && priority(*exp) <= priority(gettop(&s))) {
		push(suffix, pop(&s));
	    }
	    push(&s, *exp);
	    }
	exp++;
    }
    while(stack_length(&s) != 0)
	push(suffix, pop(&s));
}
Пример #7
0
main(stackstru *p)
{
    int n,i,k,h,x1,x2,select;
    printf("create a empty stack!\n");
    init(p);
    printf("input a stack length:\n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("input a stack value:\n");
        scanf("%d",&k);
        push(p,k);
    }
    printf("select 1:display()\n");
    printf("select 2:push()\n");
    printf("select 3:pop()\n");
    printf("select 4:gettop()\n");
    printf("input a your select(1-4):\n");
    scanf("%d",&select);
    switch(select)
    {
    case 1:
    {
        display(p);
        break;
    }
    case 2:
    {
        printf("input a push a value:\n");
        scanf("%d",&h);
        push(p,h);
        display(p);
        break;
    }
    case 3:
    {
        x1=pop(p);
        printf("x1->%d\n",x1);
        display(p);
        break;
    }
    case 4:
    {
        x2=gettop(p);
        printf("x2->%d",x2);
        break;
    }
    }
}
Пример #8
0
int main(int argc,char* argv[])
{
	stack s;
	char* left= "([{";
	char* right= ")]}";
	char ch,ch_prior;
	bool flag = true;

	stack_init(&s);

	while(true)
	{
		ch=getchar();
		if(ch =='\n')
			break;
		
		if(isin(ch,left))
			push(&s,&ch);
		else if(isin(ch,right))
		{
			if(gettop(&s,&ch_prior)&&match(ch_prior,ch))
				pop(&s,&ch);
			else
			{
				flag=false;
				break;
			}
		
		}
	
	}
	
	if(!stackempty)
		flag = false;

	if (flag==true)
		puts("yes");
	else
		puts("no");
	
	return 0;

}
int main( void ){

	int type;
	double operand;
	char expression[MAX_POLISH_SIZE];
	
	// instruction
	printf("use ~ to represent sin operation.\n");
	printf("use # to represent exp operation.\n");
	printf("use @ to represent power operation.\n");

	while((type=gettop(expression)) != EOF){
		switch(type){
			case NUMBER:
				{	push(atof(expression)); break;}
			case '+':
				{ push(pop() + pop());	break;}
			case '*':
				{ push(pop() * pop()); 	break;}	
			case '-':
				{ swaptop2(); push(pop() - pop());	break;}
			case '/':
				{ operand = peek(); swaptop2(); assert(operand !=  0); push(pop() / pop()); break;}
			case '%':
				{ operand = peek(); swaptop2(); assert(operand !=  0); push(mod(pop(), operand)); break;}
			case '~':		// sin	
				{	push(sin(pop()));	break;}
			case '#':		// exp
				{	push(exp(pop())); break;}
			case '@':		// power
				{	operand = peek(); swaptop2(); push(pow(pop(), operand)); break;}	
			case '\n':
				{	printf("\t%.8g\n", pop()); break;}
			default:
				{ printf("error happens.\n"); break;}
		}

	}

	return 0;
}
Пример #10
0
/*--------------------------------------------------------------*/
void main()
{
int a;
s s1;
clrscr();
settop(&s1);
push(&s1,a);
push(&s1,a);
push(&s1,a);
push(&s1,a);
push(&s1,a);
display(&s1);
pop(&s1);
pop(&s1);
pop(&s1);
display(&s1);
push(&s1,a);
gettop(&s1);
display(&s1);
getch();
}
Пример #11
0
int  main(void)
{
	char result,n;
	char *inputc;
	char buf[64];
	char top_item = '0';
	char op1 = '1',op2 = '2',op = '+';
	char e='z';
	sqstack optr,opnd;

	initstack(&optr);
	push(&optr,'#');
	initstack(&opnd);

	printf("请输入表达式\n");
//	getchar();

	fgets(buf,64,stdin);
	
	inputc = buf;
//	printf("input is %s\n",inputc);

	while(1)    
	{
		printf("input is %c\n",*inputc);
		if( *inputc == '\0' || gettop(&optr,&top_item) == '#')    
		{
			break;	
		}

		if(*inputc <= '9' && *inputc >= '0')
		{
			push(&opnd,*inputc);
			inputc++;
		}		
		else
		{
			gettop(&optr,&top_item);
		//	printf("top_item is %c\n",top_item);
			e = sign(top_item,*inputc);	
	//		printf("e is %c\n",e);
			switch(e)
			{
				case'<':push(&optr,*(inputc));
					inputc++;
					break;
				case'=':pop(&optr,&op);
					inputc++;
					break;
				case'>':pop(&optr,&op);
					pop(&opnd,&op2);
					pop(&opnd,&op1);
					n = count(op1,op2,op);
					push(&opnd,n);
					break;
			}
		}
	}

	gettop(&opnd,&result);
	printf("结果为:%d\n",result - '0');

	return 0;
}