标签:str type 高效 思想 清理 ast int RoCE 必须
本篇文章主要起到引子的作用,而并非去研究栈结构、或者说堆栈在操作系统内部的机制。#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 0
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *bottom;
int stackSize;
}sqStack;
void Init_stack(sqStack *s);
void Push_stack(sqStack *s, ElemType e);
void Pop(sqStack *s, ElemType *e);
int StackLength(sqStack s);
int main(void)
{
ElemType c;
sqStack s;
int len, i, sum = 0;
Init_stack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf("%c", &c);
while ( c != ‘#‘ )
{
Push_stack(&s, c);
scanf("%c", &c);
}
getchar();
len = StackLength(s);
printf("栈的当前容量是: %d\n",len);
for(i = 0; i < len; i++)
{
Pop(&s, &c);
sum = sum + (c-48) * pow(2, i);
}
printf("二进制转换10进制之后数值: %d\n",sum);
return 0;
}
void Init_stack(sqStack *s)
{
s->bottom = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->bottom )
{
exit(-1);
}
s->top = s->bottom;
s->stackSize = STACK_INIT_SIZE; //最大容量
}
void Push_stack(sqStack *s, ElemType e)
{
if( s->top - s->bottom >= s->stackSize )
{
s->bottom = (ElemType *)realloc(s->bottom, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->bottom )
{
exit(-1);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if( s->top == s->bottom )
{
return;
}
*e = *--(s->top);
}
int StackLength(sqStack s)
{
return (s.top - s.bottom);
}
思想才是王道,语言是用来实现思想的价值,一体一用。
标签:str type 高效 思想 清理 ast int RoCE 必须
原文地址:http://blog.51cto.com/13352079/2132971