這是一個利用棧的知識,通過C,不是C++,C#進行進制轉換的題,代碼如下,結果“要轉換的進制數”部分總顯示不對QAQ求解,本人查錯不太會QAQ
我的代碼:
#include?<stdio.h>
#include?<stdlib.h>
#include?<math.h>
#define?S_SIZE?100?//棧所占空間
#define?STACKINCREAMENT?10?//擴充空間時一次擴充10個字節
typedef?struct?SqStack
{
int*base;//棧底
int*top;//棧頂
int?stacksize;//棧當前的存儲空間
}SqStack;
//主函數聲明
SqStack?*?lnitStack();//初始化空棧
int?StackEmpty(SqStack?*S);//???void?GetTop(SqStack?*S?,int?*e);//獲得棧頂元素
void?push(SqStack?*S,int?*e);//進棧
void?pop(SqStack?*S,int?*e);//出棧
void?convert(SqStack?*S,int?N,int?n?);//十進制轉換N進制
void?main()
{
int??n,N;
SqStack?*s;
s=lnitStack();//初始化空棧
printf("輸入要轉換的十進制數和要轉換的進制數:\n");
scanf("%d,%d",&N,&n);
printf("%d轉換為%d進制后為:\n",N,n);
convert(s,N,n);
}
SqStack?*?lnitStack()
{???
SqStack?*S;
????S=(SqStack?*)malloc(sizeof(SqStack?));
S->base=(int*)malloc(S_SIZE*sizeof(int));
S->stacksize=S_SIZE;
S->top=S->base;//初始化空棧
return?S;
}
int?StackEmpty(SqStack?*S)
{
if(S->base==S->top)
return?1;
else?
return?0;
}
void?GetTop(SqStack?*S?,int?*e)
{
*e=*(S->top-1);
}
void?push(SqStack?*S,int?*e)
{
if(S->top-S->base>=S->stacksize)
{
S->base?=?(int?*)realloc(S->base,(S->stacksize+STACKINCREAMENT)*sizeof(int));???
S->base?+=?S->stacksize;
S->top?=?S->base;???
S->stacksize?+=?STACKINCREAMENT;??
}??
*(S->top)=*e;
S->top++;
}
void?pop(SqStack?*S,int?*e)?
{???//出棧??
if(S->base?!=?S->top)??
{???
S->top--;
*e=*S->top;??
}?
}??
void?convert(SqStack?*S,int?N,int?n)?
{
int?a;
lnitStack(S);??
do??
{??
a=N%n;
push(S,&a);???
N?=?N/n;??
}
while(N?!=?0);??
?
while(!StackEmpty(S))??
{???
pop(S,&a);???
if(a?>?9)//十六進制時輸出字母
{
????a?=?a?+?55;??
printf("%c",a);???
}???
else????
printf("%d",a);??
}??printf("\n");?
}
小白求大神幫忙找錯QAQ,要吐血了QAQ
慕粉1462487243
2016-10-21 17:01:25