通過棧進行進制轉換


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 10
typedef char ElemType;
typedef int Status;
typedef struct{
    ElemType *top;
    ElemType *base;
    int stackSize;
}SqStack;
//創建一個空棧 
Status InitStack(SqStack *S){
    S->base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
    if(!(S->base)) return ERROR;
    S->top=S->base;
    S->stackSize=MAXSIZE;//棧的最大容量 
}
//入棧
Status Push(SqStack *S,ElemType e){
    if(S->top-S->base>=S->stackSize){//c和c++中兩個地址相減的結果是地址減完的差除以元素所占字節(sizeof(ElemType)) 
        S->base=(ElemType*)realloc(S->base,(MAXSIZE+S->stackSize)*sizeof(ElemType));
        if(!(S->base)) return ERROR;
        S->top=S->base+S->stackSize;
        S->stackSize=S->stackSize+MAXSIZE;
    }
    *(S->top)=e;
    S->top++;
    return OK;
} 
void Pop(SqStack *S,ElemType *e){
    if(S->top==S->base) return;
    *e=*--(S->top);
}
//銷毀棧 
void DestroyStack(SqStack *S){
    int i;
    int len=S->stackSize;
    for(i=0;i<len;i++){
        free(S->base);
        S->base++;
    }
    S->top=S->base=NULL;
    S->stackSize=0;
} 
//格式化棧
void ClearStack(SqStack *S){
    S->top=S->base;
} 
//計算棧當前容量
int StackLen(SqStack S){
    return S.top-S.base;
} 
//二轉十 
//int main(){
//    SqStack S;
//    int i;
//    char c;
//    InitStack(&S);
//    printf("請輸入二進制數,以#結束\n");
//    scanf("%c",&c);
//    while(c!='#'){
//        Push(&S,c);
//        scanf("%c",&c);
//    } 
//    getchar();//清除鍵盤緩沖區的回車
//    char e;
//    int sum=0,len=StackLen(S);
//    for(i=0;i<len;i++){//二進制比較簡單,從棧頭開始出棧到棧尾,將字符轉換成整數的和
//        Pop(&S,&e);
//        sum=sum+(e-'0')*pow(2,i);
//    }
//    printf("%d",sum);
//    DestroyStack(&S);
//}
//二轉八:要構建兩個棧,棧一入棧完畢后以3個字符為一組轉為數字求和,然后將和轉換為字符型后入棧二;最后再將棧二從頭開始出棧; 
//int main(){
//    SqStack S,S1;
//    int i;
//    char c;
//    InitStack(&S);
//    InitStack(&S1);
//    printf("請輸入二進制數,以#結束\n");
//    scanf("%c",&c);
//    while(c!='#'){
//        Push(&S,c);
//        scanf("%c",&c);
//    } 
//    getchar();
//    char e,sum;
//    int len=StackLen(S);
//    while(len){
//        sum=0;
//        for(i=0;i<3;i++){
//            Pop(&S,&e);
//            sum=sum+(e-'0')*pow(2,i);
//            len--;
//            if(S.top==S.base) break;
//        }
      e=sum+'0';
// Push(&S1,e); // } // len=StackLen(S1); // for(i=0;i<len;i++){ // Pop(&S1,&e); // printf("%c",e); // } //} //二轉十六:和轉八一樣,要考慮一下sum超過9的情況 int main(){ SqStack S,S1; int i; char c; InitStack(&S); InitStack(&S1); printf("請輸入二進制數,以#結束\n"); scanf("%c",&c); while(c!='#'){ Push(&S,c); scanf("%c",&c); } getchar(); char e; int sum,len=StackLen(S); while(len){ sum=0; for(i=0;i<4;i++){ Pop(&S,&e); sum=sum+(e-'0')*pow(2,i); len--; if(S.top==S.base) break; } if(sum>=10){ e=(char)(sum-10+'A'); Push(&S1,e); } else{ e=sum+'0'; Push(&S1,e); } } len=StackLen(S1); for(i=0;i<len;i++){ Pop(&S1,&e); printf("%c",e); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM