主题:我写的24点游戏(有错误,请指正)
这是我写的24点算法,语法没有错误。运行时产生四个随机数,输入表达式程序也转换成了后缀表达式,但是计算结果出现错误总是“空间满”。请高手指点。谢谢了。
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 4
struct stack
{
char *midstr;
int top;
int MAXSIZE;
};
void answer(struct stack *s);
void InitStack(struct stack *s)
{
s->MAXSIZE =20;
s->midstr=new char[s->MAXSIZE];
s->top=-1;
}
char Peek(struct stack *s)
{
if(s->top==-1)
cout<<"栈空"<<endl;
return s->midstr[s->top];
}
void Push(struct stack *temp,char a) //进栈
{
if(temp->top==temp->MAXSIZE)
cout<<"空间已满"<<endl;
temp->top++;
while(a=='\0')
break;
temp->midstr[temp->top]=a;
}
char Out(struct stack *temp) //出栈
{
if(temp->top==-1)
cout<<"栈空无元素可出"<<endl;
char b;
b=temp->midstr[temp->top];
temp->top--;
return b;
}
int Precedence(char op)
{
switch(op)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '(': return 0;
case '@': return 0;
default: return 0;
}
}
void Change(char *s1,char *s2)
{
struct stack R;
int i=0,j=0;
char ch;
InitStack(&R);
Push(&R,'@');
ch=s1[i];
while(ch!='\0')
{
if(ch==' ') ch=s1[++i];
else if(ch=='(')
{
Push(&R,ch);
ch=s1[++i];
}
else if(ch==')')
{
while(Peek(&R)!='(')
s2[j++]=Out(&R);
Out(&R);
ch=s1[++i];
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
char w=Peek(&R);
while(Precedence(w)>=Precedence(ch))
{
s2[j++]=w;
Out(&R);
w=Peek(&R);
}
Push(&R,ch);
ch=s1[++i];
}
else
{
while((ch>='0'&&ch<='9')||ch=='.')
{
s2[j++]=ch;
ch=s1[++i];
}
s2[j++]=' ';
}
}
ch=Out(&R);
while(ch!='@')
{
if(ch=='(')
cout<<"错误"<<endl;
else
{
s2[j++]=ch;
ch=Out(&R);
}
}
s2[j++]='\0';
}
int Calculate(char *str)
{
struct stack s;
int x;
int i=0;
InitStack(&s);
while(str[i])
{
if(str[i]==' ')
{
i++;
continue;
}
switch(str[i])
{
case '+': x=Out(&s)+Out(&s);
i++;
return x;
break;
case '-': x=Out(&s);
x=Out(&s)-x;
i++;
return x;
break;
case '*': x=Out(&s)*Out(&s);
i++;
return x;
break;
case '/': x=Out(&s);
if(x!=0)
x=Out(&s)/x;
i++;
return x;
break;
}
Push(&s,x);
}
x=Out(&s);
if(x==24)
cout<<"恭喜你!你答对了."<<endl;
else answer(&s);
return x;
}
void answer(struct stack *s)//未实现(穷举的出正确答案)
{
}
void main()
{
int adwSortArray[ARRAY_SIZE];
int wLoop;
for(wLoop = 0; wLoop < ARRAY_SIZE; wLoop++)
{
srand((unsigned)time(NULL)+wLoop);
adwSortArray[wLoop] = rand()%9+1;
cout<<adwSortArray[wLoop]<<" ";
}
char str1[8],str2[8];
cout<<"请输入表达式"<<endl;
cin>>str1;
Change(str1,str2);
cout<<str2;
Calculate(str2);
}
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 4
struct stack
{
char *midstr;
int top;
int MAXSIZE;
};
void answer(struct stack *s);
void InitStack(struct stack *s)
{
s->MAXSIZE =20;
s->midstr=new char[s->MAXSIZE];
s->top=-1;
}
char Peek(struct stack *s)
{
if(s->top==-1)
cout<<"栈空"<<endl;
return s->midstr[s->top];
}
void Push(struct stack *temp,char a) //进栈
{
if(temp->top==temp->MAXSIZE)
cout<<"空间已满"<<endl;
temp->top++;
while(a=='\0')
break;
temp->midstr[temp->top]=a;
}
char Out(struct stack *temp) //出栈
{
if(temp->top==-1)
cout<<"栈空无元素可出"<<endl;
char b;
b=temp->midstr[temp->top];
temp->top--;
return b;
}
int Precedence(char op)
{
switch(op)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '(': return 0;
case '@': return 0;
default: return 0;
}
}
void Change(char *s1,char *s2)
{
struct stack R;
int i=0,j=0;
char ch;
InitStack(&R);
Push(&R,'@');
ch=s1[i];
while(ch!='\0')
{
if(ch==' ') ch=s1[++i];
else if(ch=='(')
{
Push(&R,ch);
ch=s1[++i];
}
else if(ch==')')
{
while(Peek(&R)!='(')
s2[j++]=Out(&R);
Out(&R);
ch=s1[++i];
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
char w=Peek(&R);
while(Precedence(w)>=Precedence(ch))
{
s2[j++]=w;
Out(&R);
w=Peek(&R);
}
Push(&R,ch);
ch=s1[++i];
}
else
{
while((ch>='0'&&ch<='9')||ch=='.')
{
s2[j++]=ch;
ch=s1[++i];
}
s2[j++]=' ';
}
}
ch=Out(&R);
while(ch!='@')
{
if(ch=='(')
cout<<"错误"<<endl;
else
{
s2[j++]=ch;
ch=Out(&R);
}
}
s2[j++]='\0';
}
int Calculate(char *str)
{
struct stack s;
int x;
int i=0;
InitStack(&s);
while(str[i])
{
if(str[i]==' ')
{
i++;
continue;
}
switch(str[i])
{
case '+': x=Out(&s)+Out(&s);
i++;
return x;
break;
case '-': x=Out(&s);
x=Out(&s)-x;
i++;
return x;
break;
case '*': x=Out(&s)*Out(&s);
i++;
return x;
break;
case '/': x=Out(&s);
if(x!=0)
x=Out(&s)/x;
i++;
return x;
break;
}
Push(&s,x);
}
x=Out(&s);
if(x==24)
cout<<"恭喜你!你答对了."<<endl;
else answer(&s);
return x;
}
void answer(struct stack *s)//未实现(穷举的出正确答案)
{
}
void main()
{
int adwSortArray[ARRAY_SIZE];
int wLoop;
for(wLoop = 0; wLoop < ARRAY_SIZE; wLoop++)
{
srand((unsigned)time(NULL)+wLoop);
adwSortArray[wLoop] = rand()%9+1;
cout<<adwSortArray[wLoop]<<" ";
}
char str1[8],str2[8];
cout<<"请输入表达式"<<endl;
cin>>str1;
Change(str1,str2);
cout<<str2;
Calculate(str2);
}