|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define STACK_INIT_SIZE 20
- #define STACKINCREMENT 10
- typedef char ElemTyppe();
- typedef struct{
- ElemTyppe *base;
- ElemTyppe *top;
- int size;
- }sqStack;
- void InitStack(sqStack *s){//初始化
- s->base = (ElemTyppe *)malloc(STACK_INIT_SIZE * sizeof(ElemTyppe));
- if (!s->base ){
- exit (0);
- }
- s->top = s->base;
- s->size = STACK_INIT_SIZE;
- }
- void push(sqStack *s, ElemTyppe e){//入栈
- if (s->top - s->base >= s->size){
- s->base = (ElemTyppe *)realloc(s->base, (s->size + STACK_INIT_SIZE) * sizeof(ElemTyppe));
- if (!s->base ){
- exit (0);
- }
- }
- *(s->top) = e;
- s->top++;
- }
- void pop(sqStack *s, ElemTyppe e){//弹栈
- if (s->top == s->base){
- return;
- }
- *e = *--(s->top);
- }
- int StackLen(sqStack s){//求栈当前容量
- return (s.top - s.base);
- }
- int main(){
- ElemTyppe 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 = sum + (c-48) * pow(2, i);
- }
- printf("转换为十进制是:%d\n", sum);
- return 0;
- }
复制代码
第8行“()”去掉;
pop函数改为:
- void pop(sqStack *s, ElemTyppe *e)
复制代码
保证定义的函数和调用的函数 字母大小写一致
|
|