二进制 转 十进制

/*******************************
二进制转十进制 (栈)
*******************************/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct Satck
{
    char *base;
    char *top;
    int size;
}Sat;

void init (Sat *s)
{
    s->base = (char *)malloc(20*sizeof(char));
    s->top = s->base;
    s->size = 20;
}

void push(Sat *s, char c)
{
    if (s->top - s->base >= s->size)
    {
        s->base = (char *)realloc(s->base, (s->size+10)*sizeof(char));
        s->size = 10 + s->size;
    }
    *(s->top) = c;
    s->top ++;
}

void pop(Sat *s, char *c)
{
    if (s->base == s->top)
    {
        return ;
    }
    *c = *--(s->top);
}

int len(Sat s)
{
    return (s.top-s.base);
}


int main()
{
    char c;
    Sat s;
    int le, sum = 0;
    init(&s);
    scanf("%c", &c);
    while (c != '#')
    {
        push(&s, c);
        scanf("%c", &c);
    }

    getchar();
    le = len(s);

    for (int i=0;i<le ;i++)
    {
        pop(&s, &c);
        sum = sum + (c-48) * pow(2, i);
    }
    printf("%d
", sum);
}
原文地址:https://www.cnblogs.com/Kingpenguin/p/9965614.html