链栈实现十进制向八进制转换
来源:互联网 发布:西工大网络继续教育 编辑:程序博客网 时间:2024/06/14 15:02
用链栈实现十进制向八进制转换。例如输入123,输出0173.
// :decimal2octal.c
//--converse a decimal number to an octal number applying linked-list stack
#include <stdio.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
/*** structure ***/
struct node{
int data;
struct node *next;
};
/*** check the stack is empty or not ***/
bool stack_empty(struct node *top)
{
if(top == NULL)
return true; // is empty
return false; // not empty
}
/*** push ***/
struct node * push(struct node *top, int N)
{
struct node *new_top;
while((new_top=(struct node *)malloc(sizeof(struct node))) == NULL);
new_top->data = N;
new_top->next = top;
return new_top;
}
/*** pop ***/
struct node * pop(struct node *top, int *x)
{
if(stack_empty(top))
return NULL;
struct node *p;
*x = top->data;
p = top;
top = top->next;
free(p);
return top;
}
int main(int argc, char **argv)
{
struct node *top=NULL;
int N, M;
printf("Pls input the number you wanna change: ");
if(!scanf("%d", &N)){
printf("we need a integer, sorry./n");
return -1;
}
M=N;
if(N<0)
N *= -1;
while(N != 0){
top = push(top, N%8); // push the lowest changed number into the stack
N /= 8;
}
if(M<0)
printf("(%d)10 = (-0", M);
else
printf("(%d)10 = (0", M);
int x;
while(!stack_empty(top)){
top = pop(top, &x);
printf("%d", x);
}
printf(")8/n");
return 0;
}
- 链栈实现十进制向八进制转换
- 二进制,八进制,十六进制向十进制转换(一)
- 二进制,八进制,十六进制向十进制转换(二)
- 十进制向二进制,八进制,十六进制随意转换
- C++ 用栈实现十进制转换成八进制
- 顺序栈实现十进制和八进制之间的转换
- C语言:十进制到八进制转换(用栈实现)
- 进制转换 十进制转化为八进制 通过栈实现
- 用栈的思想实现数制转换(十进制->八进制)
- 十进制转换为二进制 八进制 ——用栈实现
- 栈实现十进制转八进制
- 十进制转换八进制转换器
- 八进制转换为十进制
- 八进制转换为十进制
- 十进制转换为八进制
- 线性表_使用栈实现二进制转换到八进制/十进制/十六进制
- 快速实现二进制/八进制/十六进制/十进制数之间的转换
- C语言实现数值转换(十进制转八进制)
- 从“成龙之死”看恶作剧新闻的传播力与预防之道
- Ogre渲染优化心得(五) -- 用硬件蒙皮代替软件蒙皮,用GPU代替CPU
- js select cloneNode name value 一串名词连起来的意思,你懂得
- ascent的网络模块简析
- Android关机重启分析
- 链栈实现十进制向八进制转换
- String srt = new String("abc");
- Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- 网站分析工具Piwik 与joomla整合。
- 常用的JS
- 顺序栈和链栈实现汉洛塔
- vim remap F5 CTRL-F7 F7 F12
- 写AS别忘了加这句代码
- 控件操作