|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//本次作业目的:将十进制的数转化为二进制,八进制或十六进制
#include <stdio.h>
typedef struct
{
int A[10];
int top;
}stack;
void iniStack(stack *S);//初始化栈
int Push(stack *S, int x);//压栈
int Pop(stack *S, int *x);//出栈
int EmptyStack(stack *S);//判栈空
void DecToOthers(int n, int b);//进行进制间的转换
int main()
{
int i ,j;
printf("请输入一个十进制的数字:");
scanf("%d", &i);
DecToOthers(i, 2);
return 0;
}
void iniStack(stack *S)//初始化栈
{
S->top = -1;
}
int Push(stack *S, int x)//压栈
{
if (S->top = 9)
{
printf("栈中的数据已经满了\n");
return 0;
}
S->top++;
S->A[S->top] = x;
return 1;
}
int Pop(stack *S, int *x)//出栈
{
if (EmptyStack(S))
{
printf("栈是空的");
return 0;
}
else
{
x = S->A[S->top];
S->top--;
return 1;
}
}
int EmptyStack(stack *S)//判栈空
{
return(S->top == -1);
}
void DecToOthers(int n, int b)//进行进制间的转换
{
char M[] = "abcdefg";
stack Z;
int x ,i;
iniStack(&Z);
while (n)
{
Push(&Z, n%b);
if (n > 9)
{
i = n - 9;
n = M[i - 1];
}
n = n/b;
}
while (EmptyStack(&Z))
{
Pop(&Z, &x);
printf("%d", x);
}
}
算了,帮你改了一下
原因是Push那里==写成=了
- //本次作业目的:将十进制的数转化为二进制,八进制或十六进制
- #include <stdio.h>
- typedef struct
- {
- int A[10];
- int top;
- }stack;
- void iniStack(stack *S);//初始化栈
- int Push(stack *S, int x);//压栈
- int Pop(stack *S, int *x);//出栈
- int EmptyStack(stack *S);//判栈空
- void DecToOthers(int n, int b);//进行进制间的转换
- int main()
- {
- int i ,j;
- printf("请输入一个十进制的数字:");
- scanf("%d", &i);
- DecToOthers(i, 2);
- return 0;
- }
- void iniStack(stack *S)//初始化栈
- {
- S->top = -1;
- }
- int Push(stack *S, int x)//压栈
- {
- if (S->top == 9)
- {
- printf("栈中的数据已经满了\n");
- return 0;
- }
- S->top++;
- S->A[S->top] = x;
- return 1;
- }
- int Pop(stack *S, int *x)//出栈
- {
- if (EmptyStack(S))
- {
- printf("栈是空的");
- return 0;
- }
- else
- {
- x = S->A[S->top];
- S->top--;
- return 1;
- }
- }
- int EmptyStack(stack *S)//判栈空
- {
- return(S->top == -1);
- }
- void DecToOthers(int n, int b)//进行进制间的转换
- {
- char M[] = "abcdefg";
- stack Z;
- int x ,i;
- iniStack(&Z);
- while (n)
- {
- Push(&Z, n%b);
- if (n > 9)
- {
- i = n - 9;
- n = M[i - 1];
- }
- n = n/b;
- }
- while (EmptyStack(&Z))
- {
- Pop(&Z, &x);
- printf("%d", x);
- }
- }
复制代码
还有问题,我继续改改
|
|