拓展二(1)
进制转换(顺序栈)
//二进制转换为十进制
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if(!S.base)
{
return ERROR;
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S, SElemType e)
{
if(S.top-S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, sizeof(SElemType) * (S.stacksize+STACKINCREMENT));
if(!S.base)
{
return ERROR;
}
S.top = S.base+S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++) = e;
return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
if(S.top == S.base)
{
return ERROR;
}
e = *(--S.top);
return OK;
}
int StackLength(SqStack S)
{
return S.top-S.base;
}
int main()
{
SElemType e;
SqStack Stack;
int i, len, sum=0;
InitStack(Stack);
printf("请输入二进制数,回车结束!\n");
scanf("%c", &e);
while(e != 10)
{
Push(Stack, e);
scanf("%c", &e);
}
//getchar(); //把'\n'从缓冲区除去(因为原本选定输入'#'符号表示结束)
len = StackLength(Stack);
printf("栈的当前容量是:%d\n", len);
for(i = 0; i < len; i++)
{
Pop(Stack, e);
sum += ((e-48) * pow(2.0, (double)i));
}
printf("转换为十进制数是:%d\n", sum);
return 0;
}
逆波兰计算器(栈)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10
typedef double SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if(!S.base)
{
return ERROR;
}
S.top =S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S, SElemType e)
{
if(S.top-S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, sizeof(SElemType) * (S.stacksize+STACKINCREMENT));
if(!S.base)
{
return ERROR;
}
S.top = S.base+S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++) = e;
return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
if(S.top == S.base)
{
return ERROR;
}
e = *(--S.top);
return OK;
}
int StackLength(SqStack S)
{
return S.top-S.base;
}
int main()
{
SqStack Stack;
char c;
double d, e;
char str[MAXBUFFER];
int i=0;
InitStack(Stack);
printf("请按逆波兰表达式输入待计算数据,数据与数据符之间用空格隔开,回车结束:\n");
scanf("%c", &c);
while(10 != c)
{
//制作缓冲区 过滤数字并转换为数值
while(isdigit(c) || '.' == c) //ctype.h
{
str[i++] = c;
str[i] = '\0';
if(i >= MAXBUFFER)
{
printf("\n出错:输入的单个数值过大!\n");
return -1;
}
scanf("%c", &c);
if(' ' == c)
{
d = atof(str); //stdlib.h
Push(Stack, d);
i = 0;
break;
}
}
switch(c)
{
case '+':
Pop(Stack, e);
Pop(Stack, d);
Push(Stack, d+e);
break;
case '-':
Pop(Stack, e);
Pop(Stack, d);
Push(Stack, d-e);
break;
case '*':
Pop(Stack, e);
Pop(Stack, d);
Push(Stack, d*e);
break;
case '/':
Pop(Stack, e);
Pop(Stack, d);
if(0 != e)
{
Push(Stack, d/e);
}
else
{
printf("\n出错:除数为零!\n");
return -1;
}
break;
}
scanf("%c", &c);
}
Pop(Stack, d);
printf("\n最终的计算结果为:%f\n", d);
return 0;
}
// 计算正常表达式(中缀表达式) 5 - (6 + 7) * 8 + 9 / 4
// 逆波兰表达式(后缀表达式) 5 6 7 + 8 * - 9 4 / +
// 计算结果 -96.750000
中缀表达式转换为后缀表达式(栈)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if(!S.base)
{
return ERROR;
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S, SElemType e)
{
if(S.top-S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, sizeof(SElemType) * (S.stacksize+STACKINCREMENT));
if(!S.base)
{
return ERROR;
}
S.top = S.base+S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++) = e;
return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
if(S.top == S.base)
{
return ERROR;
}
e = *(--S.top);
return OK;
}
int StackLength(SqStack S)
{
return S.top-S.base;
}
int main()
{
SqStack Stack;
char c, e;
InitStack(Stack);
printf("请输入中缀表达式,回车结束:\n");
scanf("%c", &c);
while(10 != c)
{
while(c >= '0' && c <= '9')
{
printf("%c", c);
scanf("%c", &c);
if( c < '0' || c > '9')
{
printf(" ");
}
}
if(')' == c) //c==')'在错写成c=')'时 无法检测出错误
{
Pop(Stack, e);
while('(' != e) //可避免==错写成=时无法检测出错误
{
printf("%c ", e);
Pop(Stack, e);
}
}
else if('+' == c || '-' == c)
{
if(!StackLength(Stack))
{
Push(Stack, c);
}
else
{
do
{
Pop(Stack, e);
if('(' == e)
{
Push(Stack, e);
}
else
{
printf("%c ", e);
}
}while(StackLength(Stack) && '(' != e);
Push(Stack, c);
}
}
else if('*' == c || '/' == c || '(' == c)
{
Push(Stack, c);
}
else if(10 == c)
{
break;
}
else
{
printf("\n出错:输入格式错误!\n");
return -1;
}
scanf("%c", &c);
}
while(StackLength(Stack))
{
Pop(Stack, e);
printf("%c ", e);
}
return 0;
}
【额外】