我loveC 发表于 2008-3-29 14:09

数制转换(错在那)

#include<malloc.h>
#include<stdio.h>
#define STACK_INIT_SIZE 100
#define STACKINCERMENT 10
#define OK 1
#define OVERFLOW -1
#define ERROR 0
typedef struct{
    int *base;
    int *top;
    int stacksize;}sqstack;
  int initstack(sqstack *s)
{  s->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
    return OK;}
  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;}
void pop(spstack *s,int *e)
{ if(s->top==s->base)
   return ERROR;
   *e=*(--s->top);}
main()
{int n,m,e;
  initstack(&s);
clrscr();
scanf("%d%d",&n,&m);
while(n){ push(&s,n%m);
n=n/m;}
  while(!(s.top==s.base))
{pop(&s,&e);
  if(e<=q)
  printf("%d",e);
  else
  printf("%c",e-10+'A');
}
}

随心 发表于 2008-3-30 09:25

我晕,你的风格让我难受

Ethip 发表于 2008-3-30 09:28

回复 1# 的帖子

数据结构课本上有类似的算法哦!

江湖五痕 发表于 2008-3-31 16:58

风格我也不喜欢!这个太紧凑了!!

succubus 发表于 2008-3-31 19:42

编程风格确实too horrible

diaoxue 发表于 2008-4-5 15:18

我很忙,我花时间帮你改,希望你以后注意编程风格,多看论坛上高手的代码。
你的程序里有好多拼写错误,命名有点也不太好,一定要注意!!!
#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
//#include <conio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define OVERFLOW -1
#define ERROR 0

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

int initstack(sqstack &s)
{  
        s.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
    s.top=s.base;
    s.stacksize=STACK_INIT_SIZE;
    return OK;
}

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(1);
                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 1;
}
int main()
{
        int n,m,e;
        sqstack s;
        initstack(s);
//        clrscr();
        scanf("%d%d",&n,&m);
        while(n)
        {
                push(s,n%m);
                n=n/m;
        }
        while(!(s.top==s.base))
        {
                pop(s,e);
                if(e<=9)
                        printf("%d",e);
                else
                        printf("%c",e-10+'A');
        }
        return 0;
}

diaoxue 发表于 2008-4-5 15:19

我以前写的,差不多
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100//初始分配量
#define STACKINCREMENT 10//分配增量

typedef struct ///////定义结构
{
    int *base;
    int *top;
    int stacksize;
}SqStack;

void InitStack(SqStack &S)///初始化栈
{
    S.base=(int *)malloc(STACK_INIT_SIZE *sizeof(int));
    if(!S.base)exit(1);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
}

int GetTop(SqStack &S,int &e)///获得栈顶元素
{
    if(S.top==S.base)return 0;
    e=*(S.top-1);
    return e;
}

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(1);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
}

int Pop(SqStack &S,int &e)///出栈
{
    if(S.top==S.base)exit(1);
    e=* --S.top;
    return e;
}
bool StackEmpty(SqStack &S)//判空
{
    if(S.base==S.top)return true;
    return false;
}

void Conversion()//进制转换
{
    int N,e;
    SqStack S;
    InitStack(S);
    printf("input the number which you want to conver:");
    scanf("%d",&N);
    while(N)
    {
        Push(S,N%2);
        N=N/2;
    }
    while(!StackEmpty(S))
    {
        Pop(S,e);
        printf("%d",e);
    }
}

int main()
{
    Conversion();
    system("pause");
    return 0;
}

张信哲 发表于 2008-4-8 15:05

我喜欢你的格式,哈

张信哲 发表于 2008-4-9 20:46

//我也贴下,嘿嘿

#include<stdio.h>   
#include<malloc.h>
#define Maxsize 30   

#define stack_init_size 100
#define stackincrement 20

typedef struct      
{
        int s[Maxsize];
        int *top;
        int *base;
}stack;

void initstack(stack *st)   
{
        (*st).base=(int *)malloc(stack_init_size*sizeof(int));
        (*st).top=(*st).base;
}

int push(stack *st,int x)  
{
                *st->top++=x;
       
                return 1;

}

int stackempty(stack st)   
{
        return(st.top==st.base);
}

int pop(stack *st,int *x)  
{
                *x=*--(*st).top;
                return 1;

}

void main()
{
        stack st;
        st.base=st.top=0;   
        initstack(&st);  
        int n;     
        scanf("%d",&n);
        while(n)
        {
                push(&st,n%2);
                n=n/2;
        }
        while(!stackempty(st))
        {
                int e;
                pop(&st,&e);
                printf("%d",e);
        }
        printf("\n");
}

页: [1]

编程论坛