#include<stdio.h>
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define MAXSIZE 20
#define ADD 5
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
void Init(Stack *S);
int GetLen(Stack S);
void Push(Stack *S, char e);
char Pop(Stack *S);
void Transform(Stack *S);
int main()
{
char n;
Stack S;
Init(&S);
while(scanf("%c", &n) != EOF)
{
Push(&S, n);
}
Transform(&S);
return 0;
}
void Init(Stack *S)
{
S -> base = (char *)malloc(MAXSIZE*sizeof(Stack));
S -> top = S -> base;
S -> stacksize = MAXSIZE;
}
int GetLen(Stack *S)
{
return (S -> top - S -> base);
}
void Push(Stack *S, char e)
{
if(GetLen(S) >= S -> stacksize)
{
S -> base = (char *)realloc(S -> base, (S -> stacksize + ADD)*sizeof(Stack));
S -> stacksize += ADD;
}
*(S -> top) = e;
S -> top ++;
}
char Pop(Stack *S)
{
if(GetLen(S) == 0)
return -1;
S -> top --;
return *(S -> top);
}
void Transform(Stack *S)
{
int sum = 0, i = 0;
while(S -> top != S -> base)
{
char c = Pop(S);
sum += (c - '0')*pow(2, i++);
}
printf("%d\n", sum);
}
体会:对类型对应有了一定的了解和运用