注册 登录
编程论坛 数据结构与算法

数据结构-栈

wbajieng 发布于 2010-04-24 00:28, 755 次点击
本人刚学习栈,但是用C语言还没写出来那个高手能帮忙写个将十进制转换成八进制的代码
要用上栈
谢了哈!!
5 回复
#2
寒风中的细雨2010-04-24 07:58
#include <stdio.h>
#include <stdlib.h>

#define MAXS 8//
#define ADD  8//

 typedef struct
{
    int *base;
    int *top;
    int stacksize;
}Sqstack;

void InitStack( Sqstack &s)
{
    s.base = (int *) malloc (MAXS*sizeof(int));
    if( !s.base )
        exit(0);
    s.top = s.base;
    s.stacksize = MAXS;
}

void Push( Sqstack &s, int temp )
{
    if( s.top-s.base>=MAXS )
    {
        s.base =(int *) realloc (s.base, (MAXS+ADD)*sizeof(int));
        if( !s.base )
            exit(0);
        s.top = s.base + s.stacksize;
        s.stacksize += ADD;
    }
    *s.top++ = temp;
}

void Pop( Sqstack &s, int &temp )
{
    if( s.top==s.base )
        return;
    temp = *--s.top;
}

int main()
{
    Sqstack s;
    InitStack(s);
    int n;
    int temp;

    printf("input the number:");
    scanf("%d", &n);
    while( n )
    {
        temp= n%8;
        Push( s, temp );
        n = n/8;
    }
    while( s.base != s.top )
    {
        Pop( s, temp );
        printf("%d",temp);
    }
    printf("\n");

    return 0;
}
#3
MyStar2010-04-24 11:33
#4
2010-04-28 18:52
#include<iostream>
using namespace std;
#include<stack>;
void Decimaltor(int n,int r)
{
    int k;
    stack<int> s;
    while(n!=0)
    {
        k=n%r;
        s.push(k);
        n=n/r;
    }
    while(s.top!=-1)
    {
        cout<<s.top();
        s.pop();
    }
}
void main()
{
    int x,y;
    cout<<"请输入要转换的数:"<<endl;
    cin>>x;
    cout<<"请输入要转化成的进制数:"<<endl;
    cin>>y;
    Decimaltor(x,y);
}
#5
2010-04-28 19:50
程序代码:
#include<stdio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

#define OVERFLOW -2
#define  OK 1
#define  TURE 1
#define  Error 0

typedef struct
{  int *top;
    int *base;
    int stacksize;
}Sqstack;

void push(Sqstack *S,int e);
void initstack(Sqstack *S)
{ S->base=(int)malloc(STACK_INIT_SIZE*sizeof (int));
    if(!S->base)exit(OVERFLOW);
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;

}



int StackEmpty(Sqstack *S)
{
    if(S->top==S->base)
    return Error;
}

void conversion()
{    Sqstack *S;
     int n,e,m,k;
   
    initstack(S);
    scanf("%d",&n);
    printf("enter the number you want to change:");
    scanf("%d",&k);

    while(n)
    {push(S,n%k);

    n=n/k;
    }
    while(StackEmpty(S)!=0)
    {
    m=Pop(S,e);
    if(m<10)
    printf("%d",m);
    else
    {
      switch(m)
      {
         case 10:printf("A");break;
         case 11:printf("B");break;
         case 12:printf("C");break;
         case 13:printf("D");break;
         case 14:printf("E");break;
         case 15:printf("F");break;

      }
    }
    }
}


void push(Sqstack *S,int e)
{
    if(S->top-S->base>=S->stacksize)
    {
      S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
        if(!S->base)exit(OVERFLOW);
        S->top=S->base+S->stacksize;
        S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;

}

int Pop(Sqstack *S,int e)
{
    if(S->top==S->base)
        return Error;
    e=*--S->top;
    return e;
}

void main()
{
    Sqstack *S;
    printf("enter the number:");
    conversion(S);

}



10进制向1——16之间的任意转换
#6
2010-04-29 05:55
2-9进制的转换:
#include <stdio.h>
#define MAXSIZE 100

typedef int ElemType;
typedef struct stack
{
 ElemType data[MAXSIZE];
 int top;
}SqStack;

void InitStack(SqStack *S)
{
 S->top=-1;
}

ElemType GetTop(SqStack S)
{
 if(S.top==-1) { printf("Error");exit(0); }
  else return S.data[S.top];
}

void Pop(SqStack *S)
{
 if(S->top==-1) { printf("Error");exit(0); }
  else S->top--;
}

void Push(SqStack *S,ElemType x)
{
 S->top++;
 S->data[S->top]=x;
}

void Zhuanhuan(SqStack *S,ElemType x,ElemType N)
{
 while(x!=0)
 {
  Push(S,x%N);
  x=x/N;
  }
}
 
void main()
{
 SqStack S;
 ElemType x,N;
 InitStack(&S);
 printf("Please input the data and N:");
//N表示要转换的数制,本人英语不行,不知道用英语怎么说明好,见谅。
 scanf("%d",&x);
 scanf("%d",&N);
 Zhuanhuan(&S,x,N);
 while(S.top!=-1) { printf("%d",GetTop(S));Pop(&S);}
 getch();
}
1