用栈实现数制转换
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
stack<int> s(200);
int N,k,r;
int main(int argc, char *argv[])
{
cout<<"请输入数字N:"; cin>>N;
cout<<"请输入转化后的进制k(2--10):"; cin>>k;
while ( N ){
s.push(N%k);
N /= k;
}
int x;
while ( s.pop(x) ) cout<<x;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
这个代码有错吗?为什么编译不过????
[解决办法]
先贴上代码我再来解释
- C/C++ code
#include <cstdlib>#include <iostream>#include <stack>using namespace std;stack<int> s;int N = 0;int k = 0;int r =0;void main(int argc, char *argv[]){ cout<<"请输入数字N:"; cin>>N; cout<<"请输入转化后的进制k(2--10):"; cin>>k; while ( N ){ s.push(N%k); N /= k; } int x = 0; while ( !s.empty() ) { x=s.top();//取最上面的 s.pop();//取完后删除最上面的 cout<<x; } cout<<endl;}