用栈实现进制转换

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char st[33],top;
 4 int change_ten(int d)
 5 {
 6     int x=0;
 7     for(int i=1;i<=top;i++)
 8     {
 9      if(st[i]>='0'&&st[i]<='9')
10      {
11          x+=pow(d,i-1)*(st[i]-'0');
12      }
13      else
14      {
15          x+=pow(d,i-1)*(st[i]-'A'+10);
16      }
17     }
18     return x;
19 }
20 int main()
21 {
22     int n=9413,d=8;//10进制转d进制 
23     while(n)
24     {
25         st[++top]=(n%d>9)?(64+n%d-9):(n%d+'0');
26         //cout<<n%d<<endl;
27         n/=d;
28     }
29       cout<<change_ten(d)<<endl;;
30     for(int i=top;i;i--)cout<<st[i]<<" ";
31     
32     return 0;
33 }
View Code

 

posted @ 2021-12-08 21:33  matt-11  阅读(18)  评论(0)    收藏  举报