一道有关字符串处理和队列的代码出错。。求助

叶子君 2014-04-17 09:40:29
Project:所谓双端队列(double-ended queue,deque),就是在列表的两端都可以插入和删除数据。
因此它允许的操作有Create、IsEmpty、IsFull、Left、Right、AddLeft、AddRight、DeleteLeft、DeleteRight。
使用循环数组方式实现双端队列,要求实现上述操作,并实现一个Print输出操作,能将队列由左至右的次序输出于一行,元素间用空格间隔。
队列元素类型设为整型。
Input:input.txt,给出一个操作序列,可能是Create、Print之外的任何操作,需要的情况下,会给出参数。最后以关键字“End”结束,例如:
AddLeft 1
AddLeft 2
DeleteRight
IsFull
DeleteLeft
IsEmpty
AddRight 3
AddLeft 2
AddRight 1
End

Output:程序开始执行时,队列设置为空,按输入顺序执行操作,每个操作执行完后,将结果输出于一行。对于错误命令,输出“WRONG”。
对IsEmpty和IsFull命令,试情况输出“Yes”或“No”。
对Left和Right命令,若队列空,输出“EMPTY”,否则输出对应队列元素。
对Add命令,若队列满,输出“FULL”,否则调用Print,输出队列所有元素。
对Del命令,若队列空,输出“EMPTY”,否则输出所有元素。元素间用空格间隔,最后一个元素后不能有空格。最后输出一个回车。

输出示例:
―――――――――
1
2 1
2
No

Yes
3
2 3
2 3 1

――――――――――――――――
问题:编译不通过,主函数部分strcmp报错,不知道肿么改。。;
不确定我这么写这道题对不对,希望有人帮我改下,谢谢

[code=c#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class deque/**************类的声明******************/
{
public:
void Create(int MaxQSize);
void Print(deque &x);

bool IsEmpty(deque &x);
bool IsFull(deque &x);

void Left(deque &x);
void Right(deque &x);

void AddLeft(deque &x,int d);
void AddRight(deque &x,int d);

void DeleteLeft(deque &x);
void DeleteRight(deque &x);

int *a;
int MaxSize;
int front;
int rear;
};

void deque::Create(int MaxQSize)/****************生成函数*******************/
{
MaxSize=MaxQSize+1;
a=new int[MaxSize];
front=rear=0;
}

void deque:: Print(deque &x)/***********************打印*****************************/
{
for (int i=x.front+1;i<=x.rear;i++)
cout<<x.a[i]<<" ";
cout<<endl;
}

bool deque::IsEmpty(deque &x)/**********************判断是否为空*****************************/
{
if (x.front==x.rear)
{
cout<<"Yes"<<endl;
return true;
}
else
{
cout<<"No"<<endl;
return false;
}
}

bool deque::IsFull(deque &x)/********************判断是否已满********************************/
{
if ((x.rear+1)%x.MaxSize==x.front)
{
cout<<"Yes"<<endl;
return true;
}
else
{
cout<<"No"<<endl;
return false;
}
}

void deque::Left(deque &x)
{
if (IsEmpty(x))
{
cout<<"EMPTY"<<endl;
}
else
{
x.Print(x);
}
}

void deque::Right(deque &x)
{
if (IsEmpty(x))
{
cout<<"EMPTY"<<endl;
}
else
{
x.Print(x);
}
}

void deque::AddLeft(deque &x,int d)//对Add命令,若队列满,输出“FULL”,否则调用Print,输出队列所有元素。!!!!!!!!
{
if (IsFull(x))
{
cout<<"FULL"<<endl;
exit (1);
}
else
{
x.rear=(x.rear+1)%x.MaxSize;
for (int i=x.rear;i>front+1;i--)
{
a[i]=a[i-1];
}
a[x.front+1]=d;
x.Print(x);
}
}

void deque::AddRight(deque &x,int d)
{
if (IsFull(x))
{
cout<<"FULL"<<endl;
exit (1);
}
else
{
x.rear=(x.rear+1)%x.MaxSize;
a[x.rear]=d;
x.Print(x);
}
}

void deque::DeleteLeft(deque &x)//对Del命令,若队列空,输出“EMPTY”,否则输出所有元素.
{
if (IsEmpty(x))
{
cout<<"EMPTY"<<endl;
exit (1);
}
else
{
x.front=(x.front+1)%MaxSize;
x.Print(x);
}
}

void deque::DeleteRight(deque &x)//!!!!!!!!!!!!!!!!!!!!1
{
if (IsEmpty(x))
{
cout<<"EMPTY"<<endl;
exit (1);
}
else
{
x.rear=(x.rear-1)%MaxSize;
x.Print(x);
}

}

int main()/*****************主函数******************/
{
fstream fin("input.txt");

string str;
string str1="AddLeft";//1
string str2="AddRight";//2
string str3="IsFull";//3
string str4="IsEmpty";//4
string str5="DeleteRight";//5
string str6="DeleteLeft";//6
string str7="End";//7

deque ts;
ts.Create(6);
int a;
while (!fin.eof())
{
getline(fin,str);
if (! strcmp(str,str1))
{
fin>>a;
ts.AddLeft(ts,a);
}
if (! strcmp(str,&str2))//!!!!!!!!!!!! &
{
fin>>a;
ts.AddRight(ts,a);
}
if (! strcmp(str,&str3))
{
ts.IsFull(ts);
}
if (! strcmp(str,&str4))
{
ts.IsEmpty(ts);
}
if (! strcmp(str,&str5))
{
ts.DeleteRight(ts);
}
if (! strcmp(str,&str6))
{
ts.DeleteLeft(ts);
}
if (! strcmp(str,&str7))
{
break;
}

}

fin.close();
return 0;
}][/code]
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
MBSHENG 2014-04-18
  • 打赏
  • 举报
回复
在编辑内容的这里有个"代码"这个图标。用这个再重新发一次吧。 比如这样:
printf("%s\n", "Hey, beauty!")
叶子君 2014-04-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include<math.h> 
#include<fstream>
using namespace std; 

class TNode//节点类
{
public: 
	char oper;//元素
	TNode *left; //左孩子
	TNode *right; //右孩子
	TNode() 
	{ 
		left=right=NULL;

		oper=0; 
	} 
	TNode(char op) 
	{ 
		left=right=NULL;

		oper=op;
	}
}; 

bool isOper(char op)//判断是否为运算符
{ 
	char oper[]={'(',')','+','-','*','/'}; 
	for(int i=0;i<sizeof(oper);i++)
	{
		if(op==oper[i])	{ return true;}
	} 
	return false;
}

int getOperOrder(char op)//返回运算符 op 所对应的优先级
{
	switch(op) 
{
	case '(':	
		return 1; 
	case '+': 
	case '-': 
		return 2;
	case '*':
	case '/': 
		return 3; 
	default: //定义在栈中的右括号和栈底字符的优先级最低
		return 0; 
	}
}

void freeTree(TNode *&p)//释放树 
{
	if(p->left!=NULL) 
		freeTree(p->left);
	if(p->right!=NULL)
		freeTree(p->right);
	delete(p);
	 //cout<<"Memory free ";
}

叶子君 2014-04-18
  • 打赏
  • 举报
回复
[code=c#include <iostream> #include <stack> #include <queue> #include <string> #include<math.h> #include<fstream> using namespace std; class TNode//节点类 { public: char oper;//元素 TNode *left; //左孩子 TNode *right; //右孩子 TNode() { left=right=NULL; oper=0; } TNode(char op) { left=right=NULL; oper=op; } }; bool isOper(char op)//判断是否为运算符 { char oper[]={'(',')','+','-','*','/'}; for(int i=0;i<sizeof(oper);i++) { if(op==oper[i]) { return true;} } return false; } int getOperOrder(char op)//返回运算符 op 所对应的优先级 { switch(op) { case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; default: //定义在栈中的右括号和栈底字符的优先级最低 return 0; } } void freeTree(TNode *&p)//释放树 { if(p->left!=NULL) freeTree(p->left); if(p->right!=NULL) freeTree(p->right); delete(p); //cout<<"Memory free "; } void inOrder(TNode *p)//中序遍历 { if(p) { if(p->left) inOrder(p->left); if(p->right) inOrder(p->right); } } void ExpTree1(TNode *&p,string str)//后缀表达式生成二叉树 { stack<TNode*>nodeStack; char temp; int i=0; temp=str[i++]; //将得到的二叉树转化为字符串 while(temp!='\0') { if(temp!='\0'&&!isOper(temp))//不是运算符,则进栈 { p=new TNode(temp);//以 temp 为结点值构造二叉树结点 nodeStack.push(p); temp=str[i++]; } else { p=new TNode(temp); if(nodeStack.size()) //The member function returns the length of the controlled sequence.一直执行到非空 { p->right=nodeStack.top();//若非空则弹栈并设为结点的右孩子 nodeStack.pop();//删除当前栈顶元素 } if(nodeStack.size()) { p->left=nodeStack.top();//若非空则弹栈并设为结点的左孩子 nodeStack.pop(); } nodeStack.push(p); temp=str[i++]; } } } void ExpTree2(TNode *&p, string str)//中缀表达式转换成后缀表达式生成二叉树 { stack<char> a; char temp; string biaodashi=""; int i=0; temp=str[i++]; while(temp!='\0') { if(!isOper(temp)) { biaodashi+=temp; temp=str[i++]; } else if(temp=='(')//进栈 { a.push(temp); temp=str[i++]; } else if(temp==')') { while(a.top()!='(')//脱括号 { biaodashi+=a.top(); a.pop();//在碰到开括号和栈为空前反复弹出栈中元素 } a.pop(); temp=str[i++]; } else if(temp=='+'||temp=='-'||temp=='*'||temp=='/')//出栈 { while(!a.empty()&&a.top()!='('&&getOperOrder(a.top())>=getOperOrder(temp))//若栈非空栈顶不是左括号且栈顶元素优先级不低于输入运算符时, //将栈顶元素弹出到后缀表达式中,并且 str 下标加 1 { biaodashi+=a.top(); a.pop(); } a.push(temp); temp=str[i++]; } } while(!a.empty()) { biaodashi+=a.top(); a.pop(); } ExpTree1(p,biaodashi); } void outputTree(TNode *p,int totalSpaces) { //树的打印 if(p) { outputTree(p->right,totalSpaces+12); //使用递归方法,树的每层按纵向输出 for(int i=1;i<=totalSpaces;i++) { cout<<"\t "; } cout<<p->oper<<endl; outputTree(p->left,totalSpaces+10); //列与列之间间隔10个空格 } } int main() 主函数 { string expression; TNode *tree; char a[100]=" "; ifstream input("input.txt"); for(int i=0;!input.eof();i++) { input>>a[i]; } expression=a; cout<<expression; input.close(); //读取命令参数至完毕 ExpTree2(tree,expression); cout<<endl; inOrder(tree); cout<<endl; outputTree(tree,0); //paint(tree);//横向输出函数 freeTree(tree); cout<<endl; //system("pause"); return 0; }][/code]
叶子君 2014-04-18
  • 打赏
  • 举报
回复
if (! strcmp(str,&str6))
{
ts.DeleteLeft(ts);
}
叶子君 2014-04-17
  • 打赏
  • 举报
回复
上面一楼想把格式弄得好看一点。。。失败了。。。请无视。。
叶子君 2014-04-17
  • 打赏
  • 举报
回复
[code=c#include <iostream> #include <fstream> #include <string> using namespace std; class deque/**************类的声明******************/ { public: void Create(int MaxQSize); void Print(deque &x); bool IsEmpty(deque &x); bool IsFull(deque &x); void Left(deque &x); void Right(deque &x); void AddLeft(deque &x,int d); void AddRight(deque &x,int d); void DeleteLeft(deque &x); void DeleteRight(deque &x); int *a; int MaxSize; int front; int rear; }; void deque::Create(int MaxQSize)/****************生成函数*******************/ { MaxSize=MaxQSize+1; a=new int[MaxSize]; front=rear=0; } void deque:: Print(deque &x)/***********************打印*****************************/ { for (int i=x.front+1;i<=x.rear;i++) cout<<x.a[i]<<" "; cout<<endl; } bool deque::IsEmpty(deque &x)/**********************判断是否为空*****************************/ { if (x.front==x.rear) { cout<<"Yes"<<endl; return true; } else { cout<<"No"<<endl; return false; } } bool deque::IsFull(deque &x)/********************判断是否已满********************************/ { if ((x.rear+1)%x.MaxSize==x.front) { cout<<"Yes"<<endl; return true; } else { cout<<"No"<<endl; return false; } } void deque::Left(deque &x) { if (IsEmpty(x)) { cout<<"EMPTY"<<endl; } else { x.Print(x); } } void deque::Right(deque &x) { if (IsEmpty(x)) { cout<<"EMPTY"<<endl; } else { x.Print(x); } } void deque::AddLeft(deque &x,int d)//对Add命令,若队列满,输出“FULL”,否则调用Print,输出队列所有元素。!!!!!!!! { if (IsFull(x)) { cout<<"FULL"<<endl; exit (1); } else { x.rear=(x.rear+1)%x.MaxSize; for (int i=x.rear;i>front+1;i--) { a[i]=a[i-1]; } a[x.front+1]=d; x.Print(x); } } void deque::AddRight(deque &x,int d) { if (IsFull(x)) { cout<<"FULL"<<endl; exit (1); } else { x.rear=(x.rear+1)%x.MaxSize; a[x.rear]=d; x.Print(x); } } void deque::DeleteLeft(deque &x)//对Del命令,若队列空,输出“EMPTY”,否则输出所有元素. { if (IsEmpty(x)) { cout<<"EMPTY"<<endl; exit (1); } else { x.front=(x.front+1)%MaxSize; x.Print(x); } } void deque::DeleteRight(deque &x)//!!!!!!!!!!!!!!!!!!!!1 { if (IsEmpty(x)) { cout<<"EMPTY"<<endl; exit (1); } else { x.rear=(x.rear-1)%MaxSize; x.Print(x); } } int main()/*****************主函数******************/ { fstream fin("input.txt"); string str; string str1="AddLeft";//1 string str2="AddRight";//2 string str3="IsFull";//3 string str4="IsEmpty";//4 string str5="DeleteRight";//5 string str6="DeleteLeft";//6 string str7="End";//7 deque ts; ts.Create(6); int a; while (!fin.eof()) { getline(fin,str); if (! strcmp(str,str1)) { fin>>a; ts.AddLeft(ts,a); } if (! strcmp(str,&str2))//!!!!!!!!!!!! & { fin>>a; ts.AddRight(ts,a); } if (! strcmp(str,&str3)) { ts.IsFull(ts); } if (! strcmp(str,&str4)) { ts.IsEmpty(ts); } if (! strcmp(str,&str5)) { ts.DeleteRight(ts); } if (! strcmp(str,&str6)) { ts.DeleteLeft(ts); } if (! strcmp(str,&str7)) { break; } } fin.close(); return 0; }][/code]

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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