问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.h
#ifndef STACK_H
#define STACK_H
template <class T>
class Stack
{
private:
T* element;
int maxSize;
int top;
public:
Stack(int w);
bool isempty();
bool isfull();
bool push(const T& x);
bool pop(T x);
~Stack(){delete[] element;}
};
template<class T>
Stack<T>::Stack(int w)
{
top = -1;
maxSize = w;
element = new T(w);
}
template <class T>
bool Stack<T>::isempty()
{
return top == -1;
}
template <class T>
bool Stack<T>::isfull()
{
return top+1 == maxSize;
}
template <class T>
bool Stack<T>::push(const T& x)
{
if(isfull())
return false;
else
{
element[top + 1] = x;
top++;
return true;
}
}
template <class T>
bool Stack<T>::pop(T x)
{
if(isempty())
return false;
else
{
x = element[top];
top--;
return true;
}
}
#endif
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.cpp
#include <iostream>
#include "stack.h"
using namespace std;
void conversion(int n,int base);
int main()
{
int n,base;
cout << "请输入想转换的数字和进制:" << endl;
cin >> n >> base;
conversion(n,base);
cout << endl;
return 0;
}
void conversion(int n,int base)
{
Stack<int> s(100);
int y;
y = n;
int x;
while(y)
{
s.push(y%base);
y = y / base;
}
while(!s.isempty())
{
s.pop(x);
cout << x;
}
}
运行结果及报错内容
请输入想转换的数字和进制:
100 2
32766327663276632766327663276632766