求大神助力!运行出错,,,不明觉厉啊。。。
//huiwen.h
#include <iostream.h>
#define m 100
struct stackstru // 定义栈
{
char stack[m];
int *base;
int *top;
int stacksize;
};
struct queuestru //定义队列
{
int *base;
int front;
int rear;
};
class Huiwen
{
public:
Huiwen();
virtual ~Huiwen();
int InitStack(stackstru *s); //初始化栈
void ClearStack(stackstru *s); //清空栈
int StackEmpty(stackstru *s); //判断栈是否空
int Push(stackstru *s,char x); //入栈操作
char Pop(stackstru *s); //出栈操作
int InitQueue(queuestru *q); //初始化为一个空的循环队列
void ClearQueue(queuestru *q); //清空队列
int QueueEmpty(queuestru *q); //判断队列是否为空
int EnQueue(queuestru *q,char e); //入队操作
char DeQueue(queuestru *q); //出队操作
};
//huiwen.cpp
#include "Huiwen.h"
#include "iostream.h"
#include "stdlib.h"
#define INCREASE 10
#define OVERFLOW 0
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
Huiwen::Huiwen()
{
}
Huiwen::~Huiwen()
{
}
//初始化栈
int Huiwen::InitStack(stackstru *s)
{
s->base = (int*)new int[m];
if(!s->base) exit(OVERFLOW);
s->top = s->base ;
s->stacksize = m;
return OK;
}
//清空栈
void Huiwen::ClearStack(stackstru *s)
{
s->top=0;
}
//判断栈是否空
int Huiwen::StackEmpty(stackstru *s)
{
if (s->top==0) //栈顶为空
return FALSE;
else
return TRUE;
}
//入栈操作
int Huiwen::Push(stackstru *s,char x)
{
if (s->top - s->base >= s->stacksize) //栈满
{
cout<<"该栈已满"<<endl; //输出提示信息
return 0;
}
*(s->top)++ = x;
return OK;
}
//出栈操作
char Huiwen::Pop(stackstru *s)
{
char y;
if (s->top==s->base )
{
cout<<"The Stack is Empty!"<<endl;
return ERROR;
}
y=*s->top ; //取出栈顶元素
s->top --;
return y;
}
//初始化为一个空的循环队列
int Huiwen::InitQueue(queuestru *q)
{
q->base = (int*)malloc(m*sizeof(int));
if(!q->base) exit(OVERFLOW);
q->front = q->rear = 0;
return OK;
}
//清空循环队列
void Huiwen::ClearQueue(queuestru *q)
{
q->front=0;
q->rear=0;
}
//判断队列是否为空
int Huiwen::QueueEmpty(queuestru *q)
{
if (q->rear == q->front) //队头和队尾相等
{
return FALSE;
}
else
{
return TRUE;
}
}
//入队操作
int Huiwen::EnQueue(queuestru *q,char e)
{
if ((q->rear+1)%m==q->front) //循环队列已满
{
cout<<"The Queue is Full!"<<endl;
return ERROR;
}
else
{
q->base[q->rear] = e; //入队操作
q->rear = (q->rear +1) % m; //移动队尾指针
return OK;
}
}
//出队操作
char Huiwen::DeQueue(queuestru *q)
{
char f;
if (q->front==q->rear) //循环队列为空
{
cout<<"The Queue is Empty!"<<endl;
return ERROR;
}
else
{
f = q->base[q->front]; //取出队首元素
q->front=(q->front+1)%m; //移动对头指针
return f;
}
}
//main.cpp
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "Huiwen.h"
char array[m];
void main()
{
int z=0,flag=0;
Huiwen p;
stackstru *s=(stackstru *)malloc(sizeof(stackstru));
queuestru *q=(queuestru *)malloc(sizeof(queuestru));
p.InitStack(s);
p.InitQueue(q);
cout<<"打开文件:"<<endl;
FILE *fp;
fp = fopen("E:\\D_S代码\\huiwen.txt","r");
if(!fp)
{
cout<<"Open file error!"<<endl;
exit(0);
}
for(int i = 0;i < m;i ++)
{
fscanf(fp,"%c",&array[i]);
if(array[i] != '\0')
cout<<array[i];
}
fclose (fp);
cout<<endl<<"判断单词是否是回文单词:"<<endl;
for(i = 0;i < m;i ++)
{
char n = array[i];
if(n >= 'A'&&n <='Z'||n >= 'a'&&n <= 'z')
{
putchar(n); //输出输入的字符
p.Push(s,n); //字符进栈
p.EnQueue(q,n); //字符进队列
}
if(n == ' '||n==','||n=='.'||n=='?'||n=='!')
{
goto xia;
xia:
{
while(p.StackEmpty(s))
{
if(p.Pop(s)==p.DeQueue(q))
{
flag = 1;
continue;
}
else
{
flag = 0;
break;
}
}
if(flag == 1)
{
z++;
cout<<"是回文单词!"<<endl;
}
else
cout<<"不是回文单词!"<<endl;
p.ClearStack (s);
p.ClearQueue (q);
}
}
while (n == '\0')
{
if (p.StackEmpty(s))
goto xia;
break;
}
}
cout<<"该文档中共有"<<z<<"个回文单词!"<<endl<<endl;
}
[解决办法]
弱弱地问下。。。 回文单词什么意思, 是不是像 abcba 就是?
[解决办法]
lz也太不小心了,头文件都给打错了。
#include"HuiWen.h"
[解决办法]
最好的办法是下断, 然后F5运行起来, 然后F10单步运行程序, 看程序崩溃在了哪句, 再找原因.
[解决办法]
编译出错,问题不大。有个i变量没有声明
[解决办法]
单步调试和设断点调试是程序员必须掌握的技能之一。
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。