在用堆栈写行编辑程序时遇到的棘手问题,大家来帮帮忙啊!!!!

ddxz_111 2006-03-15 09:05:16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10

typedef char datatype;

typedef struct st
{
datatype *top; //栈顶指针
datatype *base; //栈尾指针
int stacksize; //堆栈大小
} STACK;

void InitStack(STACK &s) //初始化堆栈
{
s.base=(datatype *)malloc(sizeof(datatype)*STACK_INIT_SIZE);
if (!s.base) exit(0);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
}

void push(STACK &s,datatype e) //压栈
{
if (s.top - s.base > s.stacksize){
s.base=(datatype *)realloc(s.base,(s.stacksize+STACK_INCREMENT)*sizeof(datatype));
if (!s.base) exit (0);
s.top=s.base+s.stacksize;
s.stacksize+=STACK_INCREMENT;
}
*s.top++=e;
}

void clearstack(STACK &s) //清空堆栈
{
s.top=s.base;
}

datatype pop(STACK &s) //出栈
{
if (s.top==s.base) return 0;
return *--s.top;
}

void main()
{
STACK s;
char b[100],*p=b,c;
InitStack(s);
printf ("请输入字符串\(以\".\"\)结束:\n");
scanf ("%[^.]",b); //接收字符串
//c=0;
//while(c<strlen(b)+1) printf ("%d ",b[c++]);

while (*p!='.') //处理字符串
{
// printf ("*p=%c ",*p);
switch(*p) //根据不同情况处理
{
case '#':
pop(s);
break;
case '@': ///问题行
//printf ("%c",*p);
while (pop(s)!=10);
push(s,10);
break;
default:
push(s,*p);
}
p++;
}
p=s.base;
printf ("\n处理后的字符串为:\n%s\n",p); //显示处理后的字符串
}



题目要求是:遇到"#",则删除前一字符,遇到"@",删除整行,以"."表示字符串结束

例如用户输入
hello!wwww###elcome
Enjoy your time !@aaaaa
gogogo.
则经过程序处理后应输出
hello!welcome
aaaaa
gogogo

现在,我遇到了棘手问题.
当问题行为"@"时,程序能正确处理"#",也能正确处理"@",但最后一行不显示。
例如上述的处理后的结果为:
hello!welcome
aaaaa

-----------------------------上边是空行


如果问题行改为"&"时,程序一切正常,输出结果正确。

请高手帮忙!!!!!
...全文
171 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Acoolice 2006-03-16
  • 打赏
  • 举报
回复
学习!支持一楼的……
SEUU 2006-03-15
  • 打赏
  • 举报
回复
很长的看起来就很晕啊.

看看编程规范,要加注释,看得冬一点,偶水平烂没法子啊.

还有,你的分多给我一点吧,我是来得分的,我只要星星!
ykzhujiang 2006-03-15
  • 打赏
  • 举报
回复
并不是一切正常的,首先@功能肯定不能用了
程序能够退出是偶然现象,这和内存的布局有关,就是说p偶然只向了一个'.'
我试了一下,在我的机器上一共循环了400多次才结束
ddxz_111 2006-03-15
  • 打赏
  • 举报
回复
谢谢,改为'\0'后确实可以解决问题,但是,为什么把'@'改为'&'后一切正常呢
jixingzhong 2006-03-15
  • 打赏
  • 举报
回复
一个参考程序 :

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 50
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define ESC 27
typedef char ElemType;
typedef struct STACK /*定义栈类型*/
{
ElemType *base;
ElemType *top;
int stacksize;
int length;
}SqStack,*Stack;
typedef int Status;
void InitStack(Stack *S) /*初始化栈*/
{
*S=(SqStack *)malloc(sizeof(SqStack));
(*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!(*S)->base)exit(-1);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
(*S)->length=0;
}
Status DestroyStack(Stack *S) /* 销毁栈*/
{
free((*S)->base);
free((*S));
return OK;
}
void ClearStack(Stack *S) /*把栈置为空*/
{
(*S)->top=(*S)->base;
(*S)->length=0;
}
Status StackEmpty(SqStack S) /*判断栈空否*/
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
void Push(Stack *S,ElemType e) /*把数据压入栈*/
{
if((*S)->top - (*S)->base>=(*S)->stacksize)
{
(*S)->base=(ElemType *) realloc((*S)->base,
((*S)->stacksize + 10) * sizeof(ElemType));
if(!(*S)->base)exit(-1);
(*S)->top=(*S)->base+(*S)->stacksize;
(*S)->stacksize +=10;
}
*((*S)->top++)=e;
++(*S)->length;
}
Status Pop(Stack *S,ElemType *e)/*删除栈顶元素*/
{
if((*S)->top==(*S)->base) return ERROR;
*e=*((*S)->top-1);
--(*S)->length;
(*S)->top--;
return OK;
}
void LineEdit()
{ Stack S;
FILE *fp;
char ch,c,a[50];
int i,b;
InitStack(&S); /* 构造空栈 ,用来当缓冲区*/
fp=fopen("wenjian.c","w");
if(!fp)
{ printf(" cannot");exit(0);}
ch=getchar();
while(ch!='Q')
{while(ch!='Q'&&ch!='\n')
{ switch(ch)
{ case '#':Pop(&S,&c);break;/*当栈非空时退栈*/
case '@':ClearStack(&S);break;
default:Push(&S,ch);break;/*有效字进栈*/
}
ch=getchar();/*接收下一个字符*/
}
if(S->length!=0)
{ b=S->length;
for(i=S->length-1;i>=0;i--)
Pop(&S,&a[i]); /*把栈逆序输出到 a */
for(i=0;i<b;i++) /*输出到文件去*/
fputc(a[i],fp);
if(ch=='\n') /*为还行符一起输到文件去*/
fputc(ch,fp);
ClearStack(&S);} /*把栈重置为空,以便再当缓冲区*/
if(ch!='Q') ch=getchar();
}
DestroyStack(&S);fclose(fp);
}
main()
{ LineEdit();}
逸学堂 2006-03-15
  • 打赏
  • 举报
回复
case '@': ///问题行
//printf ("%c",*p);
while (pop(s)!=10);
push(s,10);
break;
~~~~~~~~
不过一直没有看懂,这行怎么是&就没有问题。
duduhaha 2006-03-15
  • 打赏
  • 举报
回复
while (*p!='\0')
我觉得这么写好像欠缺点东西吧?数组还未初始化呢?你不能保证结尾就一定是0吧?
最前面应该加一条语句吧。
memset(b,0,100);
ykzhujiang 2006-03-15
  • 打赏
  • 举报
回复
.根本就没有在b中,所以不能用来判断结束
应该以'\0'来判断结束
ykzhujiang 2006-03-15
  • 打赏
  • 举报
回复
小问题,以改正:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10

typedef char datatype;

typedef struct st
{
datatype *top; //栈顶指针
datatype *base; //栈尾指针
int stacksize; //堆栈大小
} STACK;

void InitStack(STACK &s) //初始化堆栈
{
s.base=(datatype *)malloc(sizeof(datatype)*STACK_INIT_SIZE);
if (!s.base) exit(0);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
}

void push(STACK &s,datatype e) //压栈
{
if (s.top - s.base > s.stacksize){
s.base=(datatype *)realloc(s.base,(s.stacksize+STACK_INCREMENT)*sizeof(datatype));
if (!s.base) exit (0);
s.top=s.base+s.stacksize;
s.stacksize+=STACK_INCREMENT;
}
*s.top++=e;
}

void clearstack(STACK &s) //清空堆栈
{
s.top=s.base;
}

datatype pop(STACK &s) //出栈
{
if (s.top==s.base) return 0;
return *--s.top;
}

void main()
{
STACK s;
char b[100],*p=b,c;
InitStack(s);
printf ("请输入字符串\(以\".\"\)结束:\n");
scanf ("%[^.]",b); //接收字符串
//c=0;
//while(c<strlen(b)+1) printf ("%d ",b[c++]);

while (*p!='\0') //处理字符串
{
// printf ("*p=%c ",*p);
switch(*p) //根据不同情况处理
{
case '#':
pop(s);
break;
case '@': ///问题行
//printf ("%c",*p);
while (pop(s)!=10);
push(s,10);
break;
default:
push(s,*p);
}
p++;
}
push(s,'\0');
p=s.base;
printf ("\n处理后的字符串为:\n%s\n",p); //显示处理后的字符串
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧