栈的应用举例
一 行编辑处理
取终端输入字符,约定:
#-------退格键
@------退行键
*-------结束键
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct node
{
char data;
node* next;
}snode,*slink;
int EmptyStack(slink top)
{
if (top = NULL)
{
return 1;
}
else
{
return 0;
}
}
char Pop(slink* top) //注意使用指针是因为,top也在不停的改变
{
char e;
slink p;
if (*top == NULL)
{
printf("栈空");
return -1;
}
else
{
p = *top;
e = p->data;
*top = p->next;
free(p);
return e;
}
}
void Push(slink* top, char e)
{
slink p = (slink)malloc(sizeof(slink));
p->next = *top;
p->data = e;
*top = p;
}
void ClearStack(slink* top)
{
if (*top != NULL)
{
Pop(top);
}
}
int StackLen(slink top)
{
int i = 0;
slink p = top;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
/*按规则处理字符*/
void LineEdit(char d[],char s1[])
{
int i = 0;
slink q=NULL;
while (s1[i] != '*'&&s1[i] != '\0')
{
switch (s1[i])
{
case '#': Pop(&q); break;
case '@': ClearStack(&q); break;
default: Push(&q, s1[i]);
}
i++;
}
for (i = StackLen(q); i > 0; i--)
{
d[i - 1] = Pop(&q);
}
}
void main()
{
char s[255] = "";
char d[255] = "";
printf("请输入要处理的行(@退行,#退格,*结束)");
scanf_s("%s",s,255);
LineEdit(d, s);
printf("处理好的文本是");
printf("%s", d);
}
二 表达式求值
表达式求值是栈技术应用的典型例子,也是编译系统中比较重要的问题
1 表达式的组成部分:表达式一般是由操作数和操作码(运算符)组成的
2 操作数:变量和常量
3 运算符 :
算数运算符 + - * /
关系运算符 == <= >= !=
逻辑运算符 && || ! 等
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct node
{
char data;
node* next;
}snode,*slink;
int EmptyStack(slink top)
{
if (top = NULL)
{
return 1;
}
else
{
return 0;
}
}
char Pop(slink* top)
{
char e;
slink p;
if (*top == NULL)
{
printf("栈空");
return -1;
}
else
{
p = *top;
e = p->data;
*top = p->next;
free(p);
return e;
}
}
void Push(slink* top, char e)
{
slink p = (slink)malloc(sizeof(slink));
p->next = *top;
p->data = e;
*top = p;
}
void ClearStack(slink* top)
{
if (*top != NULL)
{
Pop(top);
}
}
int StackLen(slink top)
{
int i = 0;
slink p = top;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
char GetStop(slink top)
{
if (top!=NULL)
return top->data;
return 0;
}
/*符号优先级比较*/
int Precede(char x, char y)
{
switch (x)
{
case '(':x = 0; break;
case '+':
case '-': x = 1; break;
case '*':
case '/':x = 2; break;
}
switch (y)
{
case '+':
case '-': y = 1; break;
case '*':
case '/':y = 2; break;
case '(':y = 3; break;
}
if (x >= y)
{
return 1;
}
else
{
return 0;
}
}
/*将中缀表达式转换为后缀表达式*/
void mid_post(char post[], char mid[])
{
int i = 0, j = 0;
char x;
slink S= NULL;
Push(&S, '#');
do
{
x = mid[i++];
switch (x)
{
case '#':
{
while (!EmptyStack)
{
post[j++] = Pop(&S);
}
} break;
case ')':
{
while (GetStop(S) != '(')
{
post[j++] = Pop(&S);
Pop(&S);
}
} break;
case '+':
case '-':
case '*':
case '/':
case '(':
while (Precede(GetStop(S), x))
{
post[j++] = Pop(&S);
}
Push(&S, x);
break;
default:post[j++] = x;
}
} while (x != '#');
}
/*后缀表达式求值*/
int postcount(char post[])
{
int i = 0;
char x;
float z, a, b;
slink S = NULL;
x = post[i];
while (post[i] != '#')
{
x = post[i];
switch (x)
{
case '+': b = Pop(&S); a = Pop(&S); z = a + b; Push(&S, z); break;
case '-': b = Pop(&S); a = Pop(&S); z = a - b; Push(&S, z); break;
case '*': b = Pop(&S); a = Pop(&S); z = a *b; Push(&S, z); break;
case '/': b = Pop(&S); a = Pop(&S); z = a / b; Push(&S, z); break;/*执行相应的结果并进栈*/
default:x = post[i] - '0'; Push(&S, x);
}
i++;
}
if (!EmptyStack(S))
return (GetStop(S));
}
void main()
{
char post[255], mid[255] = "";
printf("请输入要处理的字符串");
scanf_s("%s", mid,255);
printf("响应的后缀表达式为:");
mid_post(post, mid);
printf("%s\n", post);
printf("表达式的值为:%d",postcount(post));
}
浙公网安备 33010602011771号