小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C|簡單實(shí)現(xiàn)棧、及利用遞歸將十進(jìn)制數(shù)用其他進(jìn)制來輸出

 thchen0103 2019-07-10

1 位的數(shù)據(jù)表示與應(yīng)用

C|簡單實(shí)現(xiàn)棧、及利用遞歸將十進(jìn)制數(shù)用其他進(jìn)制來輸出

2 十進(jìn)制轉(zhuǎn)二進(jìn)制

可以利用循環(huán)除2及求余來計(jì)算,最先計(jì)算出的余數(shù)應(yīng)該是放在數(shù)字序列的最右端,最后算出的余數(shù)應(yīng)該放在數(shù)字序列的最左端,而數(shù)字序列的書寫是從左至右的,所以是一個(gè)倒序的操作,可以利用棧的后進(jìn)先出來模擬操作,類似的,也可以利用遞歸來進(jìn)行輸出。

C|簡單實(shí)現(xiàn)棧、及利用遞歸將十進(jìn)制數(shù)用其他進(jìn)制來輸出

3 實(shí)現(xiàn)棧和十進(jìn)制輸出其它進(jìn)制

#include<iostream>
using namespace std;
#define MAX_STACK_SIZE 100 /* 棧向量大小 */
typedef int ElemType;
typedef struct sqstack
{
ElemType stack_array[MAX_STACK_SIZE];
int top;
int bottom;
}SqStack;
SqStack Init_Stack(void)
{
SqStack S;
S.bottom=S.top=0;
return(S);
}
int push(SqStack &S, ElemType e) /* 使數(shù)據(jù)元素e進(jìn)棧成為新的棧頂 */
{
if(S.top==MAX_STACK_SIZE-1)
return -1; /* 棧滿,返回錯(cuò)誤標(biāo)志 */
S.top++; /* 棧頂指針加1 */
S.stack_array[S.top]=e; /* e成為新的棧頂 */
return 0; /* 壓棧成功 */
}
int pop(SqStack &S, ElemType *e) /*彈出棧頂元素*/
{
if(S.top==0 )
return -1; /* 棧空,返回錯(cuò)誤標(biāo)志 */
*e=S.stack_array[S.top];
S.top--;
return 0;
}
void conversion(int n, int d) /*將十進(jìn)制整數(shù)N轉(zhuǎn)換為d(2或8)進(jìn)制數(shù)*/
{
SqStack S;
ElemType e;
int k;
S=Init_Stack();
/* 求出所有的余數(shù),進(jìn)棧 */
while(n>0)
{
k=n%d;
push(S,k);
n=n/d;
}
while(S.top!=0) /* 棧不空時(shí)出棧,輸出 */
{
pop(S,&e);
printf("%1d",e);
}
}
void main()
{
conversion(54,2);
cout<<endl;
conversion(54,8);
cout<<endl;
conversion(54,16);
system("pause");
}
/*
110110
66
36
*/

4 利用遞歸來實(shí)現(xiàn)十進(jìn)制輸出二進(jìn)制

#if(0)
#include<stdio.h>
#include<stdlib.h>
void decToBin(int n, int base) {
if (n>0) {
decToBin(n/base,base);
printf("%d", n%base);
}

}
void main()
{
decToBin(13,2);
system("pause");
}
#endif
//1101
/*
f(13)
f(6)
f(3)
f(1)
1%2
3%2
6%2
13%2
*/
#if(1)
#include<stdio.h>
#include<stdlib.h>
void dec2(unsigned long n,unsigned long base)
{
int r;
r=n%base; //最后一位
if(n>=base)
dec2(n/base,base);//遞歸后進(jìn)先出的特點(diǎn)很適合倒序計(jì)算;
printf("%d",r);
}
int main()
{
unsigned int i=0;
dec2(13,2);
system("pause");
return 0;
}
#endif
/*
//1101

int r=1
n=11
dec2(5)
int r=1
n=5
dec2(2)
int r=0
n=2
dec2(2)
int r=1
n=1
*/

-End-

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約