栈的基本操作以及整数数制转换的测试
2011-05-26 13:38
134 查看
#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int StackElemType;
typedef struct
{
StackElemType *base;
StackElemType *top;
int stacksize;
}SqStack;
void InitStack(SqStack *S)
{
S->base=(StackElemType *)malloc( Stack_Init_Size *sizeof(StackElemType));
if(!S->base)exit(0);
S->top=S->base;
S->stacksize=Stack_Init_Size;
}
void Push(SqStack *S,StackElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(StackElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(StackElemType));
if(!S->base)exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
}
int Pop(SqStack *S,StackElemType e)
{
if(S->top==S->base)return 0;
e=*--S->top;
return e;
}
int GetTop(SqStack *S,StackElemType e)
{
if(S->top==S->base) return 0;
e=*(S->top-1);
return e;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return 1;
else
return 0;
}
void conversion(SqStack *S,int n,int r)
{
printf("十进制正整数%d转换为%d进制为:/n",n,r);
while(n)
{
Push(S,n%r);
n=n/r;
}
while(!StackEmpty(S))
{
StackElemType e=Pop(S,e);
switch(e)
{
case 10: printf("A");
break;
case 11: printf("B");
break;
case 12: printf("C");
break;
case 13: printf("D");
break;
case 14: printf("E");
break;
case 15: printf("F");
break;
default: printf("%d",e);
}
}
printf("/n");
}
void main()
{
int n,r;
SqStack S;
InitStack(&S);
printf("Input the Positive Integer:/n");
scanf("%d",&n);
printf("Input the coversion type:/n");
scanf("%d",&r);
conversion(&S,n,r);
}
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int StackElemType;
typedef struct
{
StackElemType *base;
StackElemType *top;
int stacksize;
}SqStack;
void InitStack(SqStack *S)
{
S->base=(StackElemType *)malloc( Stack_Init_Size *sizeof(StackElemType));
if(!S->base)exit(0);
S->top=S->base;
S->stacksize=Stack_Init_Size;
}
void Push(SqStack *S,StackElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(StackElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(StackElemType));
if(!S->base)exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
}
int Pop(SqStack *S,StackElemType e)
{
if(S->top==S->base)return 0;
e=*--S->top;
return e;
}
int GetTop(SqStack *S,StackElemType e)
{
if(S->top==S->base) return 0;
e=*(S->top-1);
return e;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return 1;
else
return 0;
}
void conversion(SqStack *S,int n,int r)
{
printf("十进制正整数%d转换为%d进制为:/n",n,r);
while(n)
{
Push(S,n%r);
n=n/r;
}
while(!StackEmpty(S))
{
StackElemType e=Pop(S,e);
switch(e)
{
case 10: printf("A");
break;
case 11: printf("B");
break;
case 12: printf("C");
break;
case 13: printf("D");
break;
case 14: printf("E");
break;
case 15: printf("F");
break;
default: printf("%d",e);
}
}
printf("/n");
}
void main()
{
int n,r;
SqStack S;
InitStack(&S);
printf("Input the Positive Integer:/n");
scanf("%d",&n);
printf("Input the coversion type:/n");
scanf("%d",&r);
conversion(&S,n,r);
}
相关文章推荐
- 变量和数制进制之间的相互转换以及八大基本数据类型
- Java中的移位操作以及基本数据类型转换成字节数组【收集】
- 数据库行列转换以及c#数据库基本操作
- 测试整数类型:byte,short,int,long。以及进制之间的转换问题
- string字符串操作以及整数和字符转换的总结
- Java中的移位操作以及基本数据类型转换成字节数组【收集】
- mysql数据库相关介绍以及一些基本操作
- java基本类型的最大最小值以及溢出测试
- MySQL学习总结(二)数据库以及表的基本操作
- unity学习笔记01--基础视图以及基本操作
- Silverlight RIA Service 对数据进行操作--基本测试
- 如何在PB中进行位操作以及二进制与十进制转换(摘录)
- VS输入输出基本操作以及数据类型和类型转换
- 数据库基本操作小摘(代码由mysql-essential-5.1.50-win32测试通过)
- 测试基本数据类型以及复合类型的长度
- 转:SQl大数据量操作效率以及日志测试
- 关于kali开机自动启动ssh服务的方法以及配置kalissh的一些基本操作
- 二叉树转换为双向链表,以及二叉树相关操作---纪念考研的时光
- 【转】hibernate学习系列-----(3)Session 缓存和持久化生命周期以及Session 基本操作
- mysql 的基本操作以及常用命令