首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

用栈实现数制转换解决思路

2012-03-09 
用栈实现数制转换#include cstdlib#include iostream#include stackusing namespace stdstackint

用栈实现数制转换
#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;} 

热点排行