大家看看我写的程序那里错了(关于用栈实现数制转换的程序)?

Sesshomaru00 2008-04-02 10:08:14
Zhan.cpp的内容:
#include <iostream>
using namespace std;
enum Status {
stOk = 1,
stError = 0,
};
template <class T> class Zhan
{
public:
static const int INIT_SIZE = 100;
static const int INCREMENT = 10;

Zhan(int nCapacity = INIT_SIZE);
~Zhan();
bool Isempty(){return top!=base;}
Status Push(T e);
Status Pop(T &e);
protected:
T* base;
T* top;
int stacksize;
};
template <class T> Zhan<T>::Zhan(int nCapacity=INIT_SIZE)
{
base=top=(T *)new(nCapacity*sizeof(T));
try{if(!base)throw stError;}
catch(...)
{
cerr<<"error"<<endl;
exit(1);
}
}
template <class T> Zhan<T>::~Zhan(void)
{
while(base!=top)
{
delete top;
top=top-sizeof(T);
}
}
template <class T> Status Zhan<T>::Push(T e)
{
*top++=e;
return stOk;
}
template <class T> Status Zhan<T>::Pop(T &e)
{
try{if(base==top)throw stError;}
catch(...)
{
cerr<<"error"<<endl;
exit(1);
}
e=*--top;
return stOk;
}





main.cpp的内容:
#include <iostream>
#include "Zhan.cpp"
using namespace std;
void main()
{
int a,e;
Zhan<int>z;
cout<<"数制转换:请输入一个十进制数:"<<endl;
cin>>a;
while(a){
z.Push(a%8);
a=a/8;
}
while(z.Isempty()){
z.Pop(e);
cout<<e;
}
}

...全文
146 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sesshomaru00 2008-04-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jieao111 的回复:]
~但是建数组的话用栈就没什么意义了吧~我就是想弄一个连续空间来存放栈~就是好象我的new命令有点问题,,

什么意思?
[/Quote]
数组的话直接用下标不就完了~没必要用那些函数吧~
jieao111 2008-04-06
  • 打赏
  • 举报
回复
数组不是连续的 吗。。还有new不对。。好像是opreator new,,具体忘了。。
jieao111 2008-04-06
  • 打赏
  • 举报
回复
~但是建数组的话用栈就没什么意义了吧~我就是想弄一个连续空间来存放栈~就是好象我的new命令有点问题,,

什么意思?
Sesshomaru00 2008-04-03
  • 打赏
  • 举报
回复
static const int INIT_SIZE = 100; //の..我的可以通过啊~
static const int INCREMENT = 10;
Sesshomaru00 2008-04-03
  • 打赏
  • 举报
回复
base=top=(T *)new(nCapacity*sizeof(T));
base=top=new T[nCapacity];
2楼的大大,你用new建了一个数组吧~但是建数组的话用栈就没什么意义了吧~我就是想弄一个连续空间来存放栈~就是好象我的new命令有点问题~不知道怎么用~麻烦说一下吧~
恩~6楼的同学~昨天我这也熄灯了没看到~
#include<stack>//这是C++里本来就有的吗?老师要我们写栈的一个类啊~这是我怕麻烦,就只写了几个函数...能不能看看
base=top=(T *)new(nCapacity*sizeof(T)); //是哪里错了啊??
谢谢了~
Sesshomaru00 2008-04-02
  • 打赏
  • 举报
回复
谢谢2楼~
向你致以最崇高的敬意!!!
jieao111 2008-04-02
  • 打赏
  • 举报
回复
我发错帖了,,不好意思。。我们快熄灯了,,先给你个写好的,自己看看。。不行明天在给你看
//关于整形数的进制计算
#include<iostream>
#include<stack>
using namespace std;
void main()
{
stack<char>s;
int n,a,m;
cout<<"输入一个数";
cin>>n;
cout<<"输入进制";
cin>>m;
cout<<n<<"=";
if(m<n)
{
while(n)
{
a=n%m;
n/=m;
if(a>9)
a=65+a-10-48;// 凑成英文字母的ASC值
s.push(a+48);//加上48后变成ASC的数字
}
}
else
{
while(n)
{
a=n%m;
n/=m;
if(a>9)
a=65+a-10-48;// 凑成英文字母的ASC值
s.push(a+48);//加上48后变成ASC的数字
}
s.push(48);//多输入一个0即可
}
while(!s.empty())
{cout<<s.top();
s.pop();
}
cout<<endl;
}

effective_person 2008-04-02
  • 打赏
  • 举报
回复
我已经帮你改好了啊!
你贴到对应的文件中就可以了啊!
Sesshomaru00 2008-04-02
  • 打赏
  • 举报
回复
2,3楼的大哥啊~怎么改啊???
jieao111 2008-04-02
  • 打赏
  • 举报
回复
非静态成员函数的非法调用
effective_person 2008-04-02
  • 打赏
  • 举报
回复

base=top=(T *)new(nCapacity*sizeof(T)); //是c还是c++??


static const int INIT_SIZE = 100; //我的编译器是通不过的?不晓得你的可以不?
static const int INCREMENT = 10;


就这些吧!
运行你的程序结果对了
effective_person 2008-04-02
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
enum Status
{
stOk = 1,
stError = 0,
};
template <class T> class Zhan
{
public:
Zhan(int nCapacity = INIT_SIZE);
~Zhan();
bool Isempty(){return top!=base;}
Status Push(T e);
Status Pop(T &e);
protected:
T* base;
T* top;
int stacksize;
enum{ INIT_SIZE = 100, INCREMENT = 10};
};
template <class T>
Zhan <T>::Zhan(int nCapacity=INIT_SIZE)
{
base=top=new T[nCapacity];
try
{
if(!base)
throw stError;
}
catch(...)
{
cerr <<"error" <<endl;
exit(1);
}
}
template <class T> Zhan <T>::~Zhan(void)
{
while(base!=top)
{
delete top;
top=top-sizeof(T);
}
}
template <class T> Status Zhan <T>::Push(T e)
{
*top++=e;
return stOk;
}
template <class T> Status Zhan <T>::Pop(T &e)
{
try
{
if(base==top)
throw stError;
}
catch(...)
{
cerr <<"error" <<endl;
exit(1);
}
e=*--top;
return stOk;
}
void main()
{
int a,e;
Zhan <int>z;
cout <<"数制转换:请输入一个十进制数:" <<endl;
cin>>a;
while(a)
{
z.Push(a%8);
a=a/8;
}
while(z.Isempty())
{
z.Pop(e);
cout <<e;
}
}

64,654

社区成员

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

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