栈的基本应用-数制转换-数据结构
2015-09-16 11:00
429 查看
栈的基本操作-数制转换,其实就是余数部分先存起来,倒序输出
为了简单,就不考虑16之类的需要转换ABCDEF的,就简单的比如十进制转二进制之类的转化
简单例代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define InitStackSize 1000
typedef struct SqStack
{
int *base,*top;
int stacksize;
}SqStack;
void InitSqSatck(SqStack &s)
{
s.base=s.top=(int*)malloc(InitStackSize*sizeof(int));
s.stacksize=InitStackSize;
}
void PushSqStack(SqStack &s,int e)
{
*s.top=e;
s.top++;
}
void Conversion(SqStack &s,int n,int b)
{
while(n)
{
PushSqStack(s,n%b);
n=n/b;
}
}
void DisplaySqStack(SqStack s)
{
while(s.top!=s.base)
{
s.top--;
printf("%d",*s.top);
}
printf("\n");
}
int main()
{
SqStack stack;
int n,b;
printf("Print the num you want to convert:\n");
while(scanf("%d",&n)!=EOF)
{
InitSqSatck(stack);
printf("Input the conversion num b:");
scanf("%d",&b);
Conversion(stack,n,b);
printf("After convert :\n");
DisplaySqStack(stack);
}
return 0;
}
为了简单,就不考虑16之类的需要转换ABCDEF的,就简单的比如十进制转二进制之类的转化
简单例代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define InitStackSize 1000
typedef struct SqStack
{
int *base,*top;
int stacksize;
}SqStack;
void InitSqSatck(SqStack &s)
{
s.base=s.top=(int*)malloc(InitStackSize*sizeof(int));
s.stacksize=InitStackSize;
}
void PushSqStack(SqStack &s,int e)
{
*s.top=e;
s.top++;
}
void Conversion(SqStack &s,int n,int b)
{
while(n)
{
PushSqStack(s,n%b);
n=n/b;
}
}
void DisplaySqStack(SqStack s)
{
while(s.top!=s.base)
{
s.top--;
printf("%d",*s.top);
}
printf("\n");
}
int main()
{
SqStack stack;
int n,b;
printf("Print the num you want to convert:\n");
while(scanf("%d",&n)!=EOF)
{
InitSqSatck(stack);
printf("Input the conversion num b:");
scanf("%d",&b);
Conversion(stack,n,b);
printf("After convert :\n");
DisplaySqStack(stack);
}
return 0;
}
相关文章推荐
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 数据结构之Treap详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)
- java数据结构和算法学习之汉诺塔示例
- Java数据结构及算法实例:三角数字
- Java数据结构之简单链表的定义与实现方法示例
- 数据结构之AVL树详解
- qqwry.dat的数据结构图文解释第1/2页
- JavaScript中数据结构与算法(五):经典KMP算法