天下奇才 - 2006-9-17 20:33:00
不知道是我编码出了小错误,还是编译器有问题,反正输出结果就是n个%c(和输出数据位数相同)。所有模块单独测试都是可以的,但就是出问题,怎么也找不到错误。
这是一个十进制转十六进制代码,用winTC编译
#include "stdio.h"
#define N 16
typedef struct
{
int *top;
int *bottom;
int size;
}stack;
int push(stack *S,int data)
{
/*
*Function of push a new element into a stack
*/
if(S==NULL) return 0;
if(S->size==100) return 0;/*Not enough space*/
*(S->top)=data;
S->top++;
S->size++;
return 1;
}
int pop(stack *S,int *data)
{
/*
*Function of deleting and getting the top element
*/
if(S==NULL) return 0;
if(S->top==S->bottom) return 0;/*The case that the stack is empty*/
S->top--;
*data=*(S->top);
S->size--;
return 1;
}
char hex(int n)
{
char e='0';
if(n<10) e+=n;
else if(n>=10&&n<16) e='A'+n-10;
return e;
}
int convertion(stack *S,int n)
{
/*
*Function of transform a decimal number to a octal one
*/
int *e;
if(S==NULL) return 0;
while(n)
{
push(S,n%N);
n=n/N;
}
printf("\n");
while(S->size!=0)
{
pop(S,e);
printf("%c",hex(*e));
}
}
main()
{
int n;
char a[100]={0};/*Get a space of sequence*/
stack *S;
S->top=S->bottom=a;
S->size=0;
printf("\nPlease input the decimal number:");
scanf("%d",&n);
convertion(S,n);
getch();
}
© 2000 - 2024 Rising Corp. Ltd.