/*设计一个简单的文字编辑器,使其具有删除打错字符的功能*/
/*约定:@表示删除前面一个字符,#表示删除前面所有字符,$表示编辑结束*/
#include
#define maxsize 64
typedef struct
{
void* data[maxsize];
int top;
}seqstack;
void SETNULL(seqstack *s){
s->top=-1;printf("cei1\n");printf("%d",s->top);
}
int EMPTY(seqstack *s){
printf("cei10\n");
if(s->top>=0)
return 0;
else
return 1;
}
char POP(seqstack *s)
{ printf("cei2\n");
if(EMPTY(s))
{
printf("underflow");
return NULL;}
else
{
s->top--;
return(s->data[s->top+1]);
}
}
seqstack *PUSH(seqstack *s,char x){
printf("cei3\n");
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){
printf("cei4\n");
int i=-1;
printf("字符为\n");
while(i!=s->top){
printf("%s\t",s->data[i+1]);
i++;
}
}
void EDIT(seqstack *s)
{
char c;
SETNULL(&s); printf("cei5\n");
c=getchar();
while(c!='$')
{
if(c=='@') {
POP(&s); printf("cei6\n");
}
else if(c=='#') {
SETNULL(&s); printf("cei7\n");
}
else {printf("1%d",s->top);
PUSH(&s,c); printf("cei8\n");
}
getchar();
shuchu(&s); printf("cei9\n");
c=getchar();
}
}
void main()
{
seqstack *s;
EDIT(s);
}
改动可以实现功能:
你打log的代码没有删掉
/*设计一个简单的文字编辑器,使其具有删除打错字符的功能*/
/*约定:@表示删除前面一个字符,#表示删除前面所有字符,$表示编辑结束*/
#include <stdio.h>
#define maxsize 64
typedef struct
{
char data[maxsize];
int top;
}seqstack;
void SETNULL(seqstack *s) {
s->top = -1; printf("cei1\n"); printf("%d", s->top);
}
int EMPTY(seqstack *s) {
printf("cei10\n");
if (s->top >= 0)
return 0;
else
return 1;
}
char POP(seqstack *s)
{
printf("cei2\n");
if (EMPTY(s))
{
printf("underflow");
return NULL;
}
else
{
return s->data[s->top--];
}
}
seqstack *PUSH(seqstack *s, char x) {
printf("cei3\n");
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) {
printf("cei4\n");
int i = -1;
printf("字符为\n");
while (i != s->top) {
printf("%c", s->data[i + 1]);
i++;
}
printf("\n");
}
void EDIT(seqstack *s)
{
char c;
SETNULL(s); printf("cei5\n");
c = getchar();
while (c != '$')
{
if (c == '@') {
POP(s); printf("cei6\n");
}
else if (c == '#') {
SETNULL(s); printf("cei7\n");
}
else {
printf("1%d\n", s->top);
PUSH(s, c); printf("cei8\n");
}
getchar();
shuchu(s); printf("cei9\n");
c = getchar();
}
}
void main()
{
seqstack *s = new seqstack;
EDIT(s);
}
话说贴代码好好贴不行啊 头文件被吞了,复制都不好复制。
改成C风格的申请释放内存了
刚刚我的忘记释放内存了。
malloc我不熟悉,应该没写错。你可以找找看博客
/*设计一个简单的文字编辑器,使其具有删除打错字符的功能*/
/*约定:@表示删除前面一个字符,#表示删除前面所有字符,$表示编辑结束*/
#include <stdio.h>
#include <stdlib.h>
#define maxsize 64
typedef struct
{
char data[maxsize];
int top;
}seqstack;
void SETNULL(seqstack *s) {
s->top = -1; printf("cei1\n"); printf("%d", s->top);
}
int EMPTY(seqstack *s) {
printf("cei10\n");
if (s->top >= 0)
return 0;
else
return 1;
}
char POP(seqstack *s)
{
printf("cei2\n");
if (EMPTY(s))
{
printf("underflow");
return NULL;
}
else
{
return s->data[s->top--];
}
}
seqstack *PUSH(seqstack *s, char x) {
printf("cei3\n");
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) {
printf("cei4\n");
int i = -1;
printf("字符为\n");
while (i != s->top) {
printf("%c", s->data[i + 1]);
i++;
}
printf("\n");
}
void EDIT(seqstack *s)
{
char c;
SETNULL(s); printf("cei5\n");
c = getchar();
while (c != '$')
{
if (c == '@') {
POP(s); printf("cei6\n");
}
else if (c == '#') {
SETNULL(s); printf("cei7\n");
}
else {
printf("1%d\n", s->top);
PUSH(s, c); printf("cei8\n");
}
getchar();
shuchu(s); printf("cei9\n");
c = getchar();
}
}
void main()
{
seqstack *s;
s = (seqstack *)malloc(4 * maxsize + 4);
EDIT(s);
free(s);
}