|
|
#2
pauljames2012-05-27 15:58
#include <stdio.h>
#include <stdlib.h> #include <malloc.h> #define MAX 100 typedef struct { int data[MAX]; int top; }seqstack, *pseqstack; pseqstack s; init_seqstack() { s = (pseqstack)malloc(sizeof(seqstack)); if(s) s ->top = -1; return; } int empty_seqstack() { if(s ->top == -1) return 1; else return 0; } input_seqstack(int x) { if(s ->top ==MAX - 1) return;//栈满不能入栈 else { s ->top ++; s ->data[s ->top] = x; return; } } int out_seqstack() { int x; if (empty_seqstack(s)) //栈空不能出栈 return 0; else { x = s ->data[s ->top]; s ->top--; return x; } } int gettop_seqstack(pseqstack s) { int x; if(empty_seqstack(s)) return 0; else { x = s->data[s ->top]; return 1; } } void change(int x,int n) { int temp; while(x != 0) { temp = x % n; input_seqstack(temp); x = x / n; } while(s->top>-1) { temp=out_seqstack(); printf("%d",temp); } return; } int main(void) { int i,j; init_seqstack(); printf("输入要转化的进制整数:\n"); scanf("%d",&i); printf("选择你要转换成的进制数:\n"); scanf("%d",&j); change(i,j); return 0; } 效果 输入要转化的进制整数: 12 选择你要转换成的进制数: 2 1100 paul@ubuntu:~/prog$ ./a.out 输入要转化的进制整数: 15 选择你要转换成的进制数: 8 17 paul@ubuntu:~/prog$ [ 本帖最后由 pauljames 于 2012-5-27 16:00 编辑 ] |
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef struct
{
int data[MAX];
int top;
}seqstack, *pseqstack;
pseqstack s;
//s = (pseqstack)malloc(sizeof(seqstack));
pseqstack init_seqstack()
{
pseqstack s;
s = (pseqstack)malloc(sizeof(seqstack));
if(s)
s ->top = -1;
return s;
}
int empty_seqstack(pseqstack s)
{
if(s ->top == -1)
return 1;
else
return 0;
}
int input_seqstack(pseqstack s,int x)
{
if(s ->top ==MAX - 1)
return 0;//栈满不能入栈
else
{
s ->top ++;
s ->data[s ->top] = x;
return 1;
}
}
int out_seqstack(pseqstack s)
{
int x;
if (empty_seqstack(s)) //栈空不能出栈
return 0;
else
{
x = s ->data[s ->top];
s ->top--;
printf("%d",x);
return 1;
}
}
int gettop_seqstack(pseqstack s)
{
int x;
if(empty_seqstack(s))
return 0;
else
{
x = s->data[s ->top];
return (1);
}
}
int change(int x)
{
int n;
pseqstack s;
s = (pseqstack)malloc(sizeof(seqstack));
s->top=-1;
while(x != 0)
{
n = x % n;
input_seqstack(s,n);
x = x / n;
}
while(s->top>-1)
{
out_seqstack(s);//
}
putchar('\n');
return 1;
}
int main()
{
int n;
pseqstack s;
s = (pseqstack)malloc(sizeof(seqstack));
printf("输入要转化的进制整数:\n");
scanf("%d",&n);
//printf("选择你要转换成的进制数:\n");
change(n);
return 0;
}