Exemplo n.º 1
0
int main()
{
    ElemType c, temp;
    sqStack s, r;
    int len, i, t, sum=0;

    InitStack(&s);
    InitStack(&r);

    printf("Please input a bin number, # to stop!\n");
    scanf("%c", &c);

    while(c != '#')
    {
        Push(&s, c);
        scanf("%c", &c);
    }

    getchar();  // 把'\n'从缓冲区去掉  也可用fflush(stdin)

    len = StackLen(s);
    printf("The current length of the stack is: %d\n", len);

    for(i=0; i<len; i++)
    {
        Pop(&s, &c);
        sum += (c-48) * pow(2, i%3);


        if((i+1)%3 == 0 || i+1 == len)
        {

            Push(&r, sum);
            sum = 0;
        }
    }

    len = StackLen(r);
    printf("The current length of the new stack is: %d\n", len);

    printf("Convert to Oct is: ");
    for(i=0; i<len; i++)
    {
        Pop(&r, &c);
        printf("%o", c);
    }

    printf("\n");
    return 0;
}
Exemplo n.º 2
0
int main(int argc, const char * argv[]) {
    
    
    ElemType c;
    SqStack s;
    int len,i,sum = 0;
    
    printf("Please input a Binary digit\n");
    
    initStack(&s);
    scanf("%c",&c);
    while (c!= '#')
    {
        Push(&s, c);
        scanf("%c",&c);
    }
    getchar();
    
    len = StackLen(s);
    
    for (i = 0 ; i <len ; i++)
    {
        Pop(&s, &c);
        sum = sum + (c-48) * pow(2, i);
    }
    printf("Decimal is %d\n",sum);
    return 0;
}
Exemplo n.º 3
0
int main()
{
    ElemType c;
    sqstack s;
    int len, i, sum=0;

    InitStack(&s);

    printf("Please input binary number, use symbole '#' to mark the end: ");
    scanf("%c", &c);
    while(c != '#'){
        Push(&s,c);
        scanf("%c",&c);
    }

    getchar(); // remove '\n' from cache

    len = StackLen(s);
    printf("Stack length is: %d\n",len);

    for(i=0;i<=len;i++){
        Pop(&s,&c);
        sum = sum+(c-48)*pow(2,i);
    }

    printf("The decimal number is: %d", sum);

    return 0;
}
Exemplo n.º 4
0
int main()
{
	ElemType c;
	SqStack s;
	int len, i, sum = 0;

	InitStack(&s);

	printf("请输入二进制数,输入#符号表示输入结束:\n");
	scanf("%c", &c);
	while (c != '#')
	{
		Push(&s, c);
		scanf("%c", &c);
	}

	getchar();	// 把最后的'\n'从键盘缓冲区去掉

	len = StackLen(s);
	printf("栈的当前长度是: %d\n", len);

	for (i = 0; i < len; i++)
	{
		Pop(&s, &c);
		sum += (c - 48)*pow(2, i);
	}

	printf("转换后的十进制数是:%d\n", sum);

	return 0;
}
Exemplo n.º 5
0
int main(){
	ElemType c;
	sqStack s;
	int len, i, sum = 0;

	InitStack(&s);

	printf("请输入二进制数,输入#符号表示结束\n");
	scanf("%c", &c);
	while (c != '#') {
		Push(&s, c);
		scanf("%c", &c);
	}
	
	getchar();

	len = StackLen(s);
	printf("栈的当前容量是%d\n", &len);

	for (i = 0; i < len; i++) {
		Pop(&s, &c);
		sum = sum + (c - 48) * pow(2, i);
	}

	printf("转化为十进制数为%d\n", sum);
	return 0;
}
Exemplo n.º 6
0
int main()
{
    sqStack s;

    char c, e;

    InitStack(&s);

    printf("Please input a normal expressioin('#' to stop):\n");
    scanf("%c", &c);
    while(c != '#')
    {
        while(isdigit(c) || '.' == c) //如果输入数字 直接打印
        {
            printf("%c", c);          //对于连续输入的数字 不加空格
            scanf("%c", &c);
            if(!isdigit(c) && c!='.')
                printf(" ");
        }



        if(')' == c)    //如果是 ) Pop并打印 直到(
        {
            Pop(&s, &e);
            while(e != '(')
            {
                printf("%c ", e);
                Pop(&s, &e);
            }
        }

        else if('+' == c || '-' == c)  //如果 + -
        {
            if(!StackLen(s))    //空栈 直接Push
            {
                Push(&s, c);
            }
            else               //非空
            {
                do
                {
                    Pop(&s, &e);  //取出栈顶元素
                    if('(' == e)  //如果取出的是(
                    {
                        Push(&s, e);  //则再把(放回去 结束循环
                    }
                    else
                    {
                        printf("%c ", e); //并打印栈顶元素(除了'(')
                    }
                }while( StackLen(s) && '(' != e);  //!!关键  退出条件:栈空 || e == '('
                Push(&s, c);  //这时候可以把吃屎的+-放进去了
            }
        }
        else if('*' == c || '/' == c || '(' == c)
        {
            Push(&s, c);
        }

        else if('#' == c)
            break;

        else
        {
            printf("\nWarning:Error input!\n");
            return -1;
        }
        scanf("%c", &c);
    }

    while(StackLen(s))
    {
        Pop(&s, &e);
        printf("%c ", e);
    }

    printf("\n");
    system("pause");
    return 0;
}