|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.代码借鉴小甲鱼的栈的初始化以及出栈,进栈,求取栈的长度
2.出错的范围在s8栈里
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACK_INCREAMNET 10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if (s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACK_INCREAMNET)*sizeof(ElemType));
if (!s->base)
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if (s->top == s->base)
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
int main ()
{
ElemType c, b;
sqStack s;
sqStack s8;
int len, i, j, sum;
sum = 0;
j = 0;
InitStack(&s);
InitStack(&s8);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf("%c",&c);
while( c != '#')
{
Push(&s, c);
scanf("%c", &c);
}
getchar(); //把‘\n’从缓冲区去掉
sum = 0;
j = 0;
len = StackLen(s);
for (i = 0; i < len;i++)
{
Pop(&s,&c);
printf("%c", c);
sum = sum + (c - 48) * pow(2, j);
j++;
if (j == 3)
{
j = 0;
b = sum +'0';
sum = 0;
Push(&s8, &b);
}
}
if (j > 0)
{
b = sum +'0';
Push(&s8, &b);
}
printf("八进制转换为是:");
len = StackLen(s8);
for (i = 0;i<len;i++)
{
Pop(&s8, &b);
printf("%c", b);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 20 //初始栈的大小
#define STACKINCREMNT 10 //动态扩增栈的大小
typedef char ElemType; //数据类型
typedef struct
{
ElemType* base; //栈底
ElemType* top; //栈顶
int stacksize; //栈当前的大小
}sqStack; //栈的基本结构
//初始化栈
//函数参数:
void InitStack(sqStack* s)
{
s->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));//申请栈的初始化空间
if (!s->base)//无空间分配给栈
{
exit(0);
}
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
}
//入栈函数
void Push(sqStack* s, ElemType e)
{
if ((s->top - s->base) >= s->stacksize)//判断栈是否已满
{
s->base = (ElemType*)realloc(s->base, (s->stacksize + STACKINCREMNT) * sizeof(ElemType));//为栈申请动态扩展空间
if (!s->base) //检查内存分配是否成功
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
//出栈函数
void pop(sqStack* s, ElemType* e)
{
if (s->top == s->base)
{
return;
}
*e = *--(s->top);
}
//返回栈长度的函数
int StackLen(sqStack* s)
{
return (s->top - s->base);
}
//参数:二进制栈名,八进制栈名
void Bin_To_Eig(sqStack* s, sqStack* t)
{
int i=0, j=0;
int sum=0,len=0;//出栈后int的数据
ElemType e;//临时储存出栈的数据
if (s->top == s->base) //判断s是否为空栈
{
return;
}
len = StackLen(s);
for (int i = 0; i < len; i += 3)
{
if (s->top - s->base >= 3)
{
for (j = 0; j < 3; j++)
{
pop(s, &e);
sum += (e - 48) * pow(2, j);
}
Push(t, sum + 48);
sum = 0;
}
else
{
for (j = 0; (s->top - s->base) != 0; j++)
{
pop(s, &e);
sum += (e - 48) * pow(2, j);
}
Push(t, sum + 48);
sum = 0;
}
}
}
void main()
{
ElemType c;
sqStack s,t;
int len=0, i, sum = 0;
InitStack(&s);
InitStack(&t);//初始生成一个8进制的栈
printf("请输入二进制数,输入#符号表示结束!\n");
scanf_s("%c", &c);
while (c != '#')
{
Push(&s, c);
scanf_s("%c", &c);
}
getchar(); // 把'\n'从缓冲区去掉
Bin_To_Eig(&s,&t);
printf("转化为八进制数为: ");
len = StackLen(&t);
for (i = 0; i < len; i++)
{
pop(&t, &c);
printf("%c", c);
}
}
|
|