c++类的动态数组怎么建立?
以下是我写的实现栈的代码,我想让stack类中的entry数组实现动态,即,我输入一个值,使他等于maxstack,这样来定义entry数组的大小。而不是现在的,给max...
以下是我写的实现栈的代码,我想让stack类中的entry数组实现动态,即,我输入一个值,使他等于maxstack,这样来定义entry数组的大小。而不是现在的,给maxstack赋予100;请问怎么才能够实现,谢谢!!
#include <iostream>
using namespace std;
int count=0;
const int maxstack=100;
class stack
{
private:
char item;
char entry[maxstack];
public:
void clear();
void pop();
char top(char item);
void push(char item);
bool empty();
};
void stack ::push( char item)
{
if(count>=maxstack)
{
cout<<"overflow";
}
else
{
entry[count++]=item;
}
}
char stack ::top(char item)
{
if (count==0)
{
cout<<"underflow";
}
else
{
item=entry[count-1];
}
return item;
}
void stack ::pop()
{
if(count==0)
{
cout<<"underflow";
}
else
{
count--;
}
}
bool stack :: empty()
{
if(count==0)
{
return true;
}
else
{
return false;
}
}
void stack ::clear()
{
count=0;
}
int main ()
{
char ch;
stack cha;
cout<<"please enter a single line of text"<<endl;
cout<<"then it will write out the characters in the line in reverse order !"<<endl;
while((ch=cin.get())!='\n')
{
cha.push(ch);
}
while(!cha.empty())
{
ch = cha.top(ch);
cha.pop();
cout<<ch;
}
cout<<endl;
return 0;
} 展开
#include <iostream>
using namespace std;
int count=0;
const int maxstack=100;
class stack
{
private:
char item;
char entry[maxstack];
public:
void clear();
void pop();
char top(char item);
void push(char item);
bool empty();
};
void stack ::push( char item)
{
if(count>=maxstack)
{
cout<<"overflow";
}
else
{
entry[count++]=item;
}
}
char stack ::top(char item)
{
if (count==0)
{
cout<<"underflow";
}
else
{
item=entry[count-1];
}
return item;
}
void stack ::pop()
{
if(count==0)
{
cout<<"underflow";
}
else
{
count--;
}
}
bool stack :: empty()
{
if(count==0)
{
return true;
}
else
{
return false;
}
}
void stack ::clear()
{
count=0;
}
int main ()
{
char ch;
stack cha;
cout<<"please enter a single line of text"<<endl;
cout<<"then it will write out the characters in the line in reverse order !"<<endl;
while((ch=cin.get())!='\n')
{
cha.push(ch);
}
while(!cha.empty())
{
ch = cha.top(ch);
cha.pop();
cout<<ch;
}
cout<<endl;
return 0;
} 展开
2个回答
推荐于2016-04-16
展开全部
修改如下:
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
int count=0;
//注意这里
class stack
{
private:
char item;
char *entry;//注意这里
int maxstack ; //注意这里
public:
stack(void);//注意这里
void clear();
void pop();
char top(char item);
void push(char item);
bool empty();
~stack(); //注意这里
};
stack::stack(void) //注意这里
{
cout<<"Maxstack=";
cin>>maxstack;
entry=new char[maxstack];
cin.ignore(255,'\n') ;
}
stack::~stack() //注意这里
{
delete []entry;
}
void stack ::push( char item)
{
if(count>=maxstack)
{
cout<<"overflow";
}
else
{
entry[count++]=item;
}
}
char stack ::top(char item)
{
if (count==0)
{
cout<<"underflow";
}
else
{
item=entry[count-1];
}
return item;
}
void stack ::pop()
{
if(count==0)
{
cout<<"underflow";
}
else
{
count--;
}
}
bool stack :: empty()
{
if(count==0)
{
return true;
}
else
{
return false;
}
}
void stack ::clear()
{
count=0;
}
int main ()
{
char ch;
stack cha;
cout<<"please enter a single line of text"<<endl;
cout<<"then it will write out the characters in the line in reverse order !"<<endl;
while((ch=cin.get())!='\n')
{
cha.push(ch);
}
while(!cha.empty())
{
ch = cha.top(ch);
cha.pop();
cout<<ch;
}
cout<<endl;
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
int count=0;
//注意这里
class stack
{
private:
char item;
char *entry;//注意这里
int maxstack ; //注意这里
public:
stack(void);//注意这里
void clear();
void pop();
char top(char item);
void push(char item);
bool empty();
~stack(); //注意这里
};
stack::stack(void) //注意这里
{
cout<<"Maxstack=";
cin>>maxstack;
entry=new char[maxstack];
cin.ignore(255,'\n') ;
}
stack::~stack() //注意这里
{
delete []entry;
}
void stack ::push( char item)
{
if(count>=maxstack)
{
cout<<"overflow";
}
else
{
entry[count++]=item;
}
}
char stack ::top(char item)
{
if (count==0)
{
cout<<"underflow";
}
else
{
item=entry[count-1];
}
return item;
}
void stack ::pop()
{
if(count==0)
{
cout<<"underflow";
}
else
{
count--;
}
}
bool stack :: empty()
{
if(count==0)
{
return true;
}
else
{
return false;
}
}
void stack ::clear()
{
count=0;
}
int main ()
{
char ch;
stack cha;
cout<<"please enter a single line of text"<<endl;
cout<<"then it will write out the characters in the line in reverse order !"<<endl;
while((ch=cin.get())!='\n')
{
cha.push(ch);
}
while(!cha.empty())
{
ch = cha.top(ch);
cha.pop();
cout<<ch;
}
cout<<endl;
return 0;
}
//---------------------------------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询