/*设计一个简单的文字编辑器,使其具有删除打错字符的功能*/
/*约定:@表示删除前面一个字符,#表示删除前面所有字符,$表示编辑结束*/
#include < stdio.h >
#define maxsize 64
typedef struct
{
void* data[maxsize];
int top;
}seqstack;
void SETNULL(seqstack *s) {
s->top = -1;
}
int EMPTY(seqstack *s) {
if (s->top >= 0)
return 0;
else
return 1;
}
char POP(seqstack *s)
{
if (EMPTY(s))
{
printf("underflow");
return NULL;
}
else
{
return s->data[s->top--];
}
}
seqstack *PUSH(seqstack *s, char x) {
int i;
if (s->top == maxsize - 1)
{
printf("overflow");
return NULL;
}
else
{
s->top++;
s->data[s->top] = x;
}
return s;
}
void shuchu(seqstack *s) {
int i = -1;
printf("字符为\n");
while (i != s->top) {
printf("%c\t", s->data[i + 1]);
i++;
}
printf("\n");
}
void EDIT(seqstack *s)
{
char c;
SETNULL(s);
c = getchar();
while (c != '$')
{
if (c == '@') {
POP(s);
}
else if (c == '#') {
SETNULL(s);
}
else {
PUSH(s, c);
}
getchar();
shuchu(s);
c = getchar();
}
}
void main()
{
seqstack s;s=(seqstack *)malloc(sizeof(int)(maxsize+1));
EDIT(s);
free(s);
}