栈的应用—数制转换

来源:互联网 发布:mysql update 自关联 编辑:程序博客网 时间:2024/05/17 09:01
十进制转化为任意进制的数:
#include<iostream>#include<stdlib.h>#include<stdio.h>using namespace std;typedef int datatype;#define MAXSIZE 100typedef struct sqstack{datatype ss[MAXSIZE];int top;};void conversion(datatype N,datatype d)//十进制数N,转化为d进制数{datatype s[MAXSIZE],e,top=-1;while(N!=0){s[++top]=N%d;N=N/d;}while(top!=-1){e=s[top--];cout<<e;}}int main(){int num=100;int d=8;conversion(num,d);}

任意进制的数转化为十进制:

#include<iostream>#include<stdlib.h>#include<stdio.h>using namespace std;typedef int datatype;#define MAXSIZE 100typedef struct sqstack{datatype ss[MAXSIZE];int top;};void conversion(datatype X,datatype d)//任意进制数转化为十进制,X表示这个数d表示数是几进制{datatype data=X;datatype s[MAXSIZE]={0},e,top=-1;while(X!=0){s[++top]=X%10;X=X/10;}datatype a=0;while(top!=-1){e=s[top];a=a+e*pow(d*1.0,top);top--;}cout<<d<<"进制"<<data<<"转化为十进制是"<<a<<endl;}int main(){int num=100;int d=7;conversion(num,d);}


0 0
原创粉丝点击