|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
错误点:执行到InitStack含税里的malloc就崩溃
代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define MAXSIZE 100
- typedef int Elemtype;
- //建立类
- typedef struct
- {
- Elemtype *base; //尾指针
- Elemtype *top; //头指针
- int StackSize; //剩余空间
- }SqStack;
- void InitStack(SqStack **E); //初始化创建空栈
- void PopStack(SqStack **E, Elemtype N); //进栈
- void PushStack(SqStack *E); //出栈
- void PushStack(SqStack *E) //出栈
- {
- E->top--;
- E->StackSize++;
- }
- void PopStack(SqStack **E, Elemtype N) //进栈
- {
- if((*E)->top - (*E)->base >= (*E)->StackSize)
- {
- (*E)->base = (Elemtype *)realloc((*E)->base, ((*E)->StackSize + MAXSIZE) * sizeof(Elemtype )); //复制前面的base中的数据进入到扩大后的base中
- if(!(*E)->base)
- {
- printf("内存分配失败本程序结束运行\n"); //判断是内存地址是否分配成功
- exit(0);
- }
- (*E)->top = (*E)->base + (*E)->StackSize; //因为以是新的内存空间所有,top中标记的地址也是错误的,所以按照以占用多少空间从新赋予top地址(realloc已经把数据复制过来了)
- (*E)->StackSize = (*E)->StackSize + MAXSIZE; //从新赋予记述节点数据
- }
- *((*E)->top) = N;
- (*E)->top++;
- (*E)->StackSize--;
- }
- void InitStack(SqStack **E) //初始化创建空栈
- {
- (*E)->base = (Elemtype *)malloc(MAXSIZE * sizeof(Elemtype )); //向指针类中的base赋予空间
- if(!(*E)->base)
- {
- printf("内存分配失败本程序结束运行\n"); //判断是内存地址是否分配成功
- exit(0);
- }
- (*E)->top = (*E)->base; //把初始地址传给头指针
- (*E)->StackSize = MAXSIZE; //初始剩余空间数量
- }
- int main()
- {
- SqStack *T = NULL;
- char litte[32];
- int F, N;
- InitStack(&T);
- //把ASCII传给字符串
- scanf("%s", litte);
- for(int i = 0; litte[i] != 0; i++ )
- {
- litte[i] = litte[i] - 48;
- PopStack(&T,litte[i]);
- }
- for(int i = 0; T->top != T->base; i++)
- {
- PushStack(T);
- F = *(T->top) * pow(i, 2);
- N += F;
- }
- printf("%d", N);
- return 0;
- }
复制代码
错的地方有点多呀
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdbool.h>
- #define MAXSIZE 100
- typedef int Elemtype;
- //建立类
- typedef struct {
- Elemtype *base; //尾指针
- Elemtype *top; //头指针
- int StackSize; //剩余空间
- } SqStack;
- /*
- void InitStack(SqStack **E); //初始化创建空栈
- void PopStack(SqStack **E, Elemtype N); //进栈 // pop 是进栈?
- void PushStack(SqStack *E); //出栈 // 同样
- */
- //void PushStack(SqStack *E) //出栈
- void PopStack(SqStack *E) //出栈
- {
- E->top--;
- //E->StackSize++;
- }
- //void PopStack(SqStack **E, Elemtype N) //进栈
- void PushStack(SqStack **E, Elemtype N) //进栈
- {
- if((*E)->top - (*E)->base >= (*E)->StackSize) {
- (*E)->StackSize += MAXSIZE;
- //(*E)->base = (Elemtype *)realloc((*E)->base, ((*E)->StackSize + MAXSIZE) *
- (*E)->base = (Elemtype *)realloc((*E)->base, (*E)->StackSize *
- sizeof(Elemtype)); //复制前面的base中的数据进入到扩大后的base中
- if(!(*E)->base) {
- printf("内存分配失败本程序结束运行\n"); //判断是内存地址是否分配成功
- exit(0);
- }
- (*E)->top =
- (*E)->base +
- (*E)->StackSize - MAXSIZE; //因为以是新的内存空间所有,top中标记的地址也是错误的,所以按照以占用多少空间从新赋予top地址(realloc已经把数据复制过来了)
- //(*E)->StackSize = (*E)->StackSize + MAXSIZE; //从新赋予记述节点数据
- }
- *((*E)->top) = N;
- (*E)->top++;
- //(*E)->StackSize--;
- }
- Elemtype stack_top(SqStack *s) {
- return *(s->top - 1);
- }
- bool stack_empty(SqStack *s) {
- return s->base == s->top;
- }
- void InitStack(SqStack **E) //初始化创建空栈
- {
- *E = malloc(sizeof(**E));
- (*E)->base = (Elemtype *)malloc(MAXSIZE * sizeof(Elemtype)); //向指针类中的base赋予空间
- if(!(*E)->base) {
- printf("内存分配失败本程序结束运行\n"); //判断是内存地址是否分配成功
- exit(0);
- }
- (*E)->top = (*E)->base; //把初始地址传给头指针
- (*E)->StackSize = MAXSIZE; //初始剩余空间数量
- }
- // 为什么不写释放函数?
- void stack_deinit(SqStack *s) {
- free(s->base); free(s);
- }
- int main() {
- SqStack *T = NULL;
- char litte[32];
- //int F, N;
- int N = 0;
- InitStack(&T);
- //把ASCII传给字符串
- scanf("%s", litte);
- //for(int i = 0; litte[i] != 0; i++) {
- for(int i = 0; litte[i] != '\0'; i++) {
- litte[i] = litte[i] - 48;
- PushStack(&T, litte[i]);
- }
- /*
- for(int i = 0; T->top != T->base; i++) {
- PopStack(T);
- F = *(T->top) * pow(i, 2);
- N += F;
- }
- */
- for(size_t i = 0; !stack_empty(T); ++i) {
- N += stack_top(T) * pow(2, i);
- PopStack(T);
- }
- //printf("%d", N);
- printf("%d\n", N);
- stack_deinit(T);
- return 0;
- }
复制代码
|
|