#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OvERFLOW -2
#define STACK_INIT_SIZE 100 //栈当前可使用的最大容量
#define STACKINCREMENT 10 //存储空间分配增量
typedef int Status; //函数结果状态类型
typedef int SElemType; //数据类型
typedef struct
{
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
//定义InitStack函数,创建一个空栈
Status InitStack(SqStack &s)
{
s.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s.base) exit(OvERFLOW);
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return OK;
}
//定义Push函数
Status Push(SqStack &s,SElemType &e)
{
if(s.top - s.base >= s.stacksize)
{//栈满,追加存储空间
s.base = (SElemType *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s.base) exit(OVERFLOW);
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top = e;
s.top++;
return OK;
}
//定义Pop函数
Status Pop(SqStack &s,SElemType &e)
{
if(s.top == s.base) return ERROR;
--s.top;
e = *s.top;
return e;
}
//定义StackLength(SqStck &s)
Status StackLength(SqStack &s)
{
//int len;
//len = s.top - s.base;
//return len;
return (s.top - s.base);
}
//定义conversion函数,实现十进制转换换为二进制
//(其中十进制需输入一个整数,目前带小数的十进制数转换为二进制的程序还没有写出来)
void conversionTenToTwo()
{
SqStack s;
int n,x,e,i,len;
//调用InitStack(SqStack &s)函数,创建一个空栈
InitStack(s);
printf("栈已创建好!\n");
printf("请输入一个十进制的整数:");
scanf("%d",&n);
printf("十进制数%d的二进制等于:",n);
while(n)
{
x = n%2;
Push(s,x);
n = n/2;
}
len = StackLength(s);
for(i=len;i>0;i--)
{
printf("%d",Pop(s,e));
}
}
void conversionTenToEight()
{
SqStack s;
int n,x,e,i,len;
//调用InitStack(SqStack &s)函数,创建一个空栈
InitStack(s);
printf("栈已创建好!\n");
printf("\n");
printf("请输入一个十进制的整数:");
scanf("%d",&n);
printf("十进制数%d的八进制等于:",n);
while(n)
{
x = n%8;
Push(s,x);
n = n/8;
}
len = StackLength(s);
for(i=len;i>0;i--)
{
printf("%d",Pop(s,e));
}
}
/*
void conversionTenToSixteen()
{
//由于现在还没有考虑到怎样吧A,B,C,D,E,F这六个字母进行转换,所以暂时不能实现//
//此程序有问题
SqStack s;
int n,x,e,i,len;
//调用InitStack(SqStack &s)函数,创建一个空栈
InitStack(s);
printf("栈已创建好!\n");
printf("请输入一个十进制的整数:");
scanf("%d",&n);
while(n)
{
x = n%16;
if(x<=9)
{
Push(s,x);
}
else if(x == 10)
{
x = ‘A‘;
Push(s,x);
}
else if(x == 11)
{
x = ‘B‘;
Push(s,x);
}
else if(x == 12)
{
x = ‘C‘;
Push(s,x);
}
else if(x == 13)
{
x = ‘D‘;
Push(s,x);
}
else if(x == ‘E‘)
{
x = ‘E‘;
Push(s,x);
}
else
{
x = ‘F‘;
Push(s,x);
}
n = n/16;
}
printf("\n");
len = StackLength(s);
for(i=len;i>0;i--)
{
if(*s.base<=9)
printf("%d",Pop(s,e));
else
printf("%c",Pop(s,e));
s.base++;
}
}
*/
//定义主函数
int main()
{
void conversionTenToTwo();
void conversionTenToEight();
void conversionTenToSixteen();
printf("\t===================*进制转换*===================\n");
printf("\t\t1->十进制转换为二进制\n");
printf("\t\t2->十进制转换为八进制\n");
//printf("\t\t3->十进制装换为十六进制\n");
printf("\t================================================\n");
char ch;
printf("请输入你的选择:");
scanf("%c",&ch);
switch(ch)
{
case ‘1‘:conversionTenToTwo();break;
case ‘2‘:conversionTenToEight();break;
//case ‘3‘:conversionTenToSixteen();break;
default:printf("enter data error!\n");
}
//conversionTenToTwo();
printf("\n");
return 0;
}