|
楼主 |
发表于 2020-3-3 09:06:37
|
显示全部楼层
我是按照小甲鱼在数据结构与算法的视频里面讲的来写的两段代码
第一段是中缀转后缀的:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10//最大缓冲区
typedef char ElemType;
typedef struct
{
ElemType* base;
ElemType* top;
int StackSize;
}sqStack;
void InitStack(sqStack* S)
{
S->base = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
//或S.base = (int*)malloc(MAXSIZE*sizeof(int));
if (!S->base)
exit(0);//存储分配失败
S->top = S->base;//栈顶指针等于栈底指针
S->StackSize = MAXSIZE;
}
void Push(sqStack* S, ElemType e)
{
if ((S->top) - (S->base) >= (S->StackSize))//栈满或超出
{
S->base = (ElemType *)realloc(S->base, (S->StackSize + STACKINCREMENT) * sizeof(ElemType));//增加空间
if (!S->base)
exit(0);
S->top = (S->base) + (S->StackSize);
S->StackSize = S->StackSize + STACKINCREMENT;
}
//*S->top++ = e;
*(S->top) = e;
S->top++;
}
void Pop(sqStack* S, ElemType* e)
{
if (S->top == S->base)
return;
/*--S->top;
*e = *(S->top);*/
*e = *--(S->top);//将栈顶元素弹出并修改栈顶指针
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
int main(void)
{
sqStack s;
char c, e;
InitStack(&s);
printf("请输入中缀表达式,以#作为结束标志:");
scanf("%c", &c);
while (c != '#')
{
while(c >= '0' && c <= '9')
{
printf("%c", c);
scanf("%c",&c);
if(c<'0' || c>'9')
{
printf(" ");
}
}
if (')' == c)
{
Pop(&s,&e);
while ('(' != e)
{
printf("%c ", e);
Pop(&s, &e);
}
}
else if ('+' == c || '-' == c)
{
if (!StackLen(s))//为空
{
Push(&s, c);
}
else
{
do
{
Pop(&s, &e);
if ('(' == e)
{
Push(&s, e);
}
else
{
printf("%c ", e);
}
} while (StackLen(s) && '(' != e );
Push(&s, c);
}
}
else if ('*' == c || '/' == c || '(' == c)
{
Push(&s, c);
}
else if('#' == c)
{
break;
}
else
{
printf("\n出错:输入格式错误!\n");
return -1;
}
scanf("%c", &c);
}
while (StackLen(s))//不为空
{
Pop(&s, &e);
printf("%c ", e);
}
//system("pause");
return 0;
}
第二段是逆波兰:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10//最大缓冲区
typedef double ElemType;
typedef struct
{
ElemType* base;
ElemType* top;
int StackSize;
}sqStack;
void InitStack(sqStack* S)
{
S->base = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
//或S.base = (int*)malloc(MAXSIZE*sizeof(int));
if (!S->base)
exit(0);//存储分配失败
S->top = S->base;//栈顶指针等于栈底指针
S->StackSize = MAXSIZE;
}
void Push(sqStack* S, ElemType e)
{
if (S->top - S->base >= S->StackSize)//栈满或超出
{
S->base = (ElemType*)realloc(S->base, (S->StackSize + STACKINCREMENT) * sizeof(ElemType));//增加空间
if (!S->base)
exit(0);
S->top = S->base + S->StackSize;
S->StackSize = S->StackSize + STACKINCREMENT;
}
//*S->top++ = e;
*(S->top) = e;
S->top++;
}
void Pop(sqStack* S, ElemType* e)
{
if (S->top == S->base)
return;
/*--S->top;
*e = *(S->top);*/
*e = *--(S->top);//将栈顶元素弹出并修改栈顶指针
}
int StackLen(sqStack S)
{
return (S.top - S.base);
}
int main(void)
{
sqStack s;
char c;
double d, e;
char str[MAXBUFFER];
int i = 0;
InitStack(&s);
printf("请按逆波兰表达式输入待计算数据,数据与运算符之间用空格隔开,以#作为结束标志:\n");
scanf("%c",&c);
while (c != '#')
{
while(isdigit(c) || c == '.')//用于过滤数字,头文件为ctype.h
{
str[i++] = c;
str[i] = '\0';
if (i >= 10)
{
printf("出错:输入的单个数据过大\n");
return -1;
}
scanf("%c",&c);
if (c == ' ')
{
d = atof(str);//将字符串转化为浮点数,返回值为double型,头文件为stdlib.h
Push(&s, d);
i = 0;
break;
}
}
switch (c)
{
case '+':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d + e);
break;
case '-':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d - e);
break;
case '*':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d * e);
break;
case '/':
Pop(&s, &e);
Pop(&s, &d);
if (e != 0)
{
Push(&s, d / e);
}
else
{
printf("\n出错:除数为零! \n");
return -1;
}
break;
}
scanf("%c",&c);
}
Pop(&s, &d);
printf("%f",d);
system("pause");
return 0;
}
现在我的问题有两个,一个是怎么把这两段代码合二为一,一个是改变我的输入形式,变成我之前的要求
感谢!!! |
|