急求大神帮我运行一下,看看那里出问题了,关于栈和队列的应用
// Huiwen.h: interface for the Huiwen class.
#include <iostream.h>
#define m 100
typedef struct stackstru // 定义栈
{
char stack[m];
int *base;
int *top;
int stacksize;
}stackstru;
typedef struct queuestru //定义队列
{
int *base;
int front;
int rear;
}queuestru ;
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: implementation of the Huiwen class.
#include "Huiwen.h"
#include "iostream.h"
#include "stdlib.h"
#define INCREASE 10
#define INITSIZE 100
#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[INITSIZE];
if(!s.base)
exit(OVERFLOW);
s.top = s.base ;
s.stacksize =INITSIZE;
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 ; //取出栈顶元素
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 ;
//s=(stackstru *)malloc(sizeof(stackstru));
queuestru q ;
//q=(queuestru *)malloc(sizeof(queuestru));
p.InitStack(s);
p.InitQueue(q);
cout<<"打开文件:"<<endl;
FILE *fp;
fp = fopen("../Stack_Queue/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;
}
#include <iostream>
#include <stdlib.h>
#define m 1000
using namespace std;
typedef struct stackstru // 定义栈
{
char *base;
int top;
int stacksize;
} stackstru;
typedef struct queuestru //定义队列
{
char *base;
int front;
int rear;
} queuestru;
class Huiwen
{
public:
Huiwen();
virtual ~Huiwen();
int InitStack(stackstru &s); //初始化栈
void ClearStack(stackstru &s); //清空栈
void DestroyStack(stackstru &s); //销毁栈
int StackEmpty(stackstru &s); //判断栈是否空
int Push(stackstru &s,char x); //入栈操作
char Pop(stackstru &s); //出栈操作
void PrintStack(stackstru s); //输出栈所有元素
int InitQueue(queuestru &q); //初始化为一个空的队列
void ClearQueue(queuestru &q); //清空队列
void DestroyQueue(queuestru &q); //销毁队列
int QueueEmpty(queuestru &q); //判断队列是否为空
int EnQueue(queuestru &q,char e); //入队操作
char DeQueue(queuestru &q); //出队操作
void PrintQueue(queuestru q); //输出队列所有元素
};
// Huiwen.cpp: implementation of the Huiwen class.
#define INCREASE 10
#define INITSIZE 100
#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 = (char*)new char[INITSIZE];
if(!s.base)
exit(OVERFLOW);
s.top = 0;
s.stacksize = INITSIZE;
return OK;
}
//清空栈
void Huiwen::ClearStack(stackstru &s)
{
memset(s.base, 0x00, INITSIZE);
s.top = 0;
}
//销毁栈
void Huiwen::DestroyStack(stackstru &s)
{
s.top = 0;
delete [] s.base;
}
//判断栈是否空
int Huiwen::StackEmpty(stackstru &s)
{
if (0 == s.top) //栈顶为空
return FALSE;
else
return TRUE;
}
//入栈操作
int Huiwen::Push(stackstru &s, char x)
{
if (s.top >= s.stacksize) //栈满
{
cout<<"该栈已满"<<endl; //输出提示信息
return 0;
}
s.base[s.top] = x;
s.top++;
return OK;
}
//出栈操作
char Huiwen::Pop(stackstru &s)
{
char ch;
if (0 == s.top)
{
cout<<"The Stack is Empty!"<<endl;
return ERROR;
}
ch = s.base[--s.top];
return ch;//取出栈顶元素
}
void Huiwen::PrintStack(stackstru s)
{
for (int i=0; i<s.top; i++)
{
printf("%c", s.base[i]);
}
printf("\n");
}
//初始化为一个空的队列
int Huiwen::InitQueue(queuestru &q)
{
q.base = (char*)malloc(INITSIZE*sizeof(char));
if(!q.base) exit(OVERFLOW);
q.front = q.rear = 0;
return OK;
}
//清空队列
void Huiwen::ClearQueue(queuestru &q)
{
memset(q.base, 0x00, INITSIZE);
q.front = 0;
q.rear = 0;
}
//销毁队列
void Huiwen::DestroyQueue(queuestru &q)
{
q.front = 0;
q.rear = 0;
delete [] q.base;
}
//判断队列是否为空
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)%INITSIZE==q.front) //循环队列已满
{
cout<<"The Queue is Full!"<<endl;
return ERROR;
}
else
{
q.base[q.rear] = e; //入队操作
q.rear = (q.rear+1)%INITSIZE; //移动队尾指针
return OK;
}
}
//出队操作
char Huiwen::DeQueue(queuestru &q)
{
char f;
if (q.rear == q.front) //队列为空
{
cout<<"The Queue is Empty!"<<endl;
return ERROR;
}
else
{
f = q.base[q.front]; //取出队首元素
q.front=(q.front+1)%INITSIZE; //移动对头指针
return f;
}
}
void Huiwen::PrintQueue(queuestru q)
{
for (int i=q.front; i<q.rear; i++)
{
printf("%c", q.base[i]);
}
printf("\n");
}
//main.cpp
//#include "Huiwen.h"
int isAlpha(char chAlpha)
{
if ((chAlpha >= 'A' && chAlpha <= 'Z')
[解决办法]
(chAlpha >= 'a' && chAlpha <= 'z'))
{
return 1;
}
return 0;
}
int isSplit(char chSplit)
{
if ( chSplit==' '
[解决办法]
chSplit==','
[解决办法]
chSplit=='.'
[解决办法]
chSplit=='?'
[解决办法]
chSplit=='!'
[解决办法]
chSplit=='\n' )
{
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
int z = 0,flag = 0;
int i;
char n;
char array[m+1] = {0};
Huiwen p;
stackstru s;
queuestru q;
FILE *fp;
cout<<"打开文件:"<<endl;
fp = fopen("huiwen.txt","r");
if(!fp)
{
cout<<"Open file error!"<<endl;
exit(0);
}
for(i = 0; i < m; i ++)
{
fscanf(fp, "%c", &(array[i]));
}
fclose(fp);
printf("%s\n", array);
printf("判断单词是否是回文单词:\n");
p.InitStack(s);
p.InitQueue(q);
n = array[0];
i = 0;
while(n != '\0')
{
n = array[i++];
if(isAlpha(n) && !isSplit(n))
{
p.Push(s, n); //字符进栈
p.EnQueue(q, n); //字符进队列
continue;
}
// p.PrintStack(s);
// p.PrintQueue(q);
if(isSplit(n))
{
while(p.StackEmpty(s))
{
if(p.Pop(s) == p.DeQueue(q))
{
flag = 1;
continue;
}
else
{
flag = 0;
break;
}
}
if(1 == flag)
{
z++;
}
}
p.ClearStack(s);
p.ClearQueue(q);
}
p.DestroyStack(s);
p.DestroyQueue(q);
printf("该文档中共有%d个回文单词!\n", z);
return 0;
}