进制之间的转换 要用栈来实现

hillhero789 2005-03-03 04:31:05
输入一个十进制数,将它转化成2,8,16进制,用栈弹出的时候要按位从小到大来实现

请问这个程序怎么编呢?
谢谢
...全文
745 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
winstonch 2005-03-04
  • 打赏
  • 举报
回复
To 磊:我是在网上搜的,你搜一下就行了

To 楼主:不知道你学过数据结构没有,这个问题和数据结构中的用栈实现表达式计算是一样的,只不过这个的表达式的操作符是确定的,你参考那个就行了
pcboyxhy 2005-03-03
  • 打赏
  • 举报
回复
to sankt(黄景天)

在你的栈溢出之前,
int 已经到底了,
也就是说不可能产生溢出。

其实一个数组 p[]

插入时 p[i++]=??;就可以了,
只要有栈的本质,
形式是无所谓的,
拘泥于书上的形式没什么意义。
sankt 2005-03-03
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>


#define stacksize 100 //
typedef int datatype;

typedef struct
{
datatype data[stacksize];
int top;
}seqstack;

void Initial(seqstack *s)
{
s->top=-1; //s->top<0 表示空栈,下溢
}

int Isempty(seqstack *s)
{
return s->top==-1;
}

int Isfull(seqstack *s)
{
return s->top==stacksize-1; //注意这里是==而不是=
}

void push(seqstack *s,datatype x)
{
if(Isfull(s))
{
printf("栈上溢");
exit(1);
}

s->data[++s->top]=x;
}

datatype pop(seqstack *s)
{
if(Isempty(s))
{
printf("栈为空");
exit(1);
}

return s->data[s->top--];
}

//进制转换函数

void conversion(int i,int j )
{
int k;
seqstack s;
Initial(&s);
while(i)
{
push(&s,i%j);
i=i/j;
}
while(!Isempty(&s))
{
k=pop(&s);
printf("%3d",k);
}
printf("\n");

}

datatype top(seqstack *s)
{
if(Isempty(s))
{

printf("栈为空");
exit(1);
}

return s->data[s->top];
}


void main()
{
conversion(32767,8);
}
我的是十进制转八进制,其它的一样
up
yuchengliu 2005-03-03
  • 打赏
  • 举报
回复
还是找本数据结构看看吧!
hillhero789 2005-03-03
  • 打赏
  • 举报
回复
怎么看你们的方法跟我的有点象呢?
那我的方法应该对了吧?
hillhero789 2005-03-03
  • 打赏
  • 举报
回复
R=2:
2 |12
----
2 |6 ----> 0
----
2 |3 ----> 0
----
2 |1 ----> 1
----
|0 ----> 1
----

其他情况一样,用这个方法编程序很容易,但是用堆栈就没有听说过
你给出来的程序好像没有用到堆栈吧,你能亲自写一个让我学习学习吗?
languagec 2005-03-03
  • 打赏
  • 举报
回复
#include <iostream.h>
int stack[100],top=0;
void Change(int n,int dex)
{
if(n)
{
Change(n/dex,dex);
stack[top++]=n%dex;
//cout<<n%dex;
}
}
void main()
{
int n,dex;
while(cin>>n>>dex)
{
Change(n,dex);
for(int i=0;i<top;i++)
cout<<stack[i];
cout<<endl;

top=0;
}
}


input example
8 3
output example
22
pcboyxhy 2005-03-03
  • 打赏
  • 举报
回复
有那么复杂吗?


#include <iostream.h>

int main(int argc, char *argv[])
{
int n, q=0, i=0, w;
char p[64], hexn[]="0123456789ABCDEF";
cin>>n; w=n;

cout<<endl<<"8进制: "<<0;
while(n>0){p[i++]=n&0x7; n>>=3;}
while(--i>=0) cout<<int(p[i]);

i=0; n=w;
cout<<endl<<endl<<"16进制: 0x";
while(n>0){p[i++]=n&0xf; n>>=4;}
while(--i>=0) cout<<hexn[int(p[i])];

i=0; n=w;
cout<<endl<<endl<<"2进制: ";
while(n>0){p[i++]=n&0x1; n>>=1;}
while(--i>=0) cout<<int(p[i]);

cout<<endl;
system("PAUSE");
return 0;
}
languagec 2005-03-03
  • 打赏
  • 举报
回复
#include <iostream.h>
void Change(int n,int dex)
{
if(n)
{
Change(n/dex,dex);
cout<<n%dex;
}
}
void main()
{
int n,dex;
while(cin>>n>>dex)
{
Change(n,dex);
cout<<endl;
}
}
xuelong_zl 2005-03-03
  • 打赏
  • 举报
回复
我想问一下 winstonch() ( )兄弟,那个代码你是在哪看到的?

可以不以告诉,我也想多看看源代码

呵呵,谢谢先
zhousqy 2005-03-03
  • 打赏
  • 举报
回复
up
winstonch 2005-03-03
  • 打赏
  • 举报
回复
你的算法肯定的对,你把进制转换的公式帖上来我看看
winstonch 2005-03-03
  • 打赏
  • 举报
回复
我给你找了一个,不知道是不是用栈实现的,你看看吧
/*------------------------mini_tool小工具系列 numconv.cc-----------------------
Name: numconv.cyx
描述: 数值转换小工具, 用于将标准输入的 "十六进制/十进制/二进制" 数值互为转换.
方便其他程序设计中的 "位状态" 考虑.
作者: 任逍 |2002.04.21.
版权: GNU General Public License (GPL)
------------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <locale.h>

char bin_string[sizeof(long int) * 8 + 1];

void get_help(char* program_name);
void get_version(char* program_name);
char* puts_bin_string(char* bin_string, const long int value);
bool test_chars(char* parameter, int num_system);

int main(int argc, char** argv)
{
// 初步检测命令行参数.
if (argc == 1)
{
printf("Please try -h or --help for help!\n");
return 0;
}
if ( (! strcmp(argv[1], "-h")) || (! strcmp(argv[1], "--help")) )
{
get_help(argv[0]);
return 0;
}
if ( (! strcmp(argv[1], "-V")) || (! strcmp(argv[1], "--version")) )
{
get_version(argv[0]);
return 0;
}

// 检测初始化环境变量 "NUMCONV_IN" 和 "NUMCONV_OUT".
char* penv_in = NULL;
char* penv_out = NULL;

penv_in = getenv("NUMCONV_IN");
if ( (penv_in == NULL ) || (!strcmp(penv_in, "")) )
{
printf(" Please export NUNCONV_IN to environment!\n OR try -h/--help for help.\n");
return -1;
}

penv_out = getenv("NUMCONV_OUT");
if ( (penv_out == NULL ) || (!strcmp(penv_out, "")) )
{
printf(" Please export NUNCONV_OUT to environment!\n OR try -h/--help for help.\n");
return -1;
}

// 查看程序环境变量.
if ( (!strcmp(argv[1], "-s")) )
{
printf("NUMCONV_IN=\t%s\n", getenv("NUMCONV_IN"));
printf("NUMCONV_OUT=\t%s\n", getenv("NUMCONV_OUT"));
return 0;
}

// 按进制转换数据并显示(标准输出).
long int conv_value;
char* end_chars = NULL;
int conv_in = atoi(getenv("NUMCONV_IN"));
int conv_out = atoi(getenv("NUMCONV_OUT"));

for (int iB = 1; iB < argc; iB ++)
{
if (! test_chars(argv[iB], conv_in))
{
fprintf(stderr, "Invalid chars!\t-> %s\n", argv[iB]);
printf("xxx\n"); // 待转换字符串首字符即不合法. 打印 "xxx" 作标示.
continue;
}
if ( ((conv_value = strtol(argv[iB], &end_chars, conv_in) ) == LONG_MAX)
|| ((conv_value = strtol(argv[iB], &end_chars, conv_in) ) == LONG_MIN) )
{ // 数值字符串超出long类型范围, 打印 "???" 作标示.
fprintf(stderr, "Overflow or Underflow of:\t%s\n", argv[iB]);
printf("???\n");
continue;
}
if ( (end_chars != NULL) && (strcmp(end_chars, "")) )
{ // 在标准错误输出设备指出不能转换的非法字符.
fprintf(stderr, "Spare chars of string end!\t-> %s\n", end_chars);
end_chars = NULL;
}
switch (conv_out)
{ // 格式化显示转换后的数据.
case 0:
case 10:
printf("%d\n", conv_value);
break;
case 8:
printf("%#o\n", conv_value);
break;
case 16:
printf("%#x\n", conv_value);
break;
case 2:
printf("%s\n", puts_bin_string(bin_string, conv_value));
break;
default:
printf("Please set NUMCONV_OUT for correct.\n");
printf("Please tyt -h or --help for help!\n");
return -1;
}
}
return 0; // 数值转换结束.
}

// 程序使用帮助函数.
void get_help(char* pname)
{
printf("-----------------------------------------------------------------------------\n");
printf("Usage: %s [option] [number_chars ...]\n", pname);
printf("(用法: %s [选项] [待处理数据字符串...]\n", pname);
printf("\n描 述:\n");
printf(" 通过设定数据进制环境变量 \"NUMCONV_IN\" 和 \"NUMCONV_OUT\"转换从命令行传\n");
printf(" 入的数据字符串. 支持 \"二进制/八进制/十进制/十六进制\" 的任意互转.\n");
printf(" 转换后的数据送到标准输出.\n");

printf("\n选 项:\n");
printf(" -h|--help 显示此帮助列表.\n");
printf(" -V|--version 显示程序版本及相关说明.\n");
printf("\n注 意!\n");
printf(" 1. 使用前请先设定环境变量! 如\"export NUMCONV_IN=10\" ->设定输入数据为十\n");
printf(" 进制方式, \"export NUMCONV_OUT=16\" ->设定输出为十六进制.\n");
printf(" 2. 待处理数据字符串字符应是符合相应进制规定的字符, 如十六进制的a-f/A-F.\n");
printf(" 3. 程序可处理以空格/TAB分隔的多个数据字串(转换数据时无选项字符)\n");
printf(" 4. 在转换中碰到不能转换的非法字符, 程序打印已转换的数据, 并在标准错误输\n");
printf(" 出打印剩下的非法字串.\n");
printf(" 5. 若转换一开始就碰到非法字符, 程序打印 'xxxx' 字串以作标示, 并在标准错\n");
printf(" 误输出打印该字串.\n");
printf(" 6. 若碰到超出long数据类型范围的字串, 程序将其打印为 '???'.\n");

printf("\n错误反馈:\n");
printf(" 非常欢迎您的错误信息/报告反馈! 请将出现错误的前后情形描述得尽量详细些, \n");
printf(" 以方便本人的分析和判断, 解决您的问题(当然更是程序本身的问题!).\n");
printf(" 信息反馈地址: E-Mail: cyhouser@163.com 谢谢!\n");
printf("-----------------------------------------------------------------------------\n");
}

// 二进制字符串显示函数.
char* puts_bin_string(char* bin_string, const long int conv_value)
{ // 采用 "位检测" 循环赋值字符 '1' 或 '0'.
int tmp_length = (sizeof(long int) * 8);
long int tmp_value = conv_value;
for (int i3 = tmp_length - 1; i3 >= 0; i3 --)
{
if (tmp_value & 1)
bin_string[i3] = '1';
else
bin_string[i3] = '0';
tmp_value = tmp_value >> 1;
}
bin_string[tmp_length] = '\0';
return bin_string;
}

// 显示程序版本号及其版权等说明.
void get_version(char* name)
{
printf(" Name: %s\n", name);
printf(" Version: 0.1-0\n");
printf(" License: GNU General Public License (GPL)\n");
printf(" Author: ZhongHui-Huang of Chinese.\n");
printf(" TimeFrom: 2002.04.27.\n");
}

// 检测待转换字符串首字符(除'-'号外)是否合法. (包括 '0x?' 式的十六进制值)
bool test_chars(char* param, int num_system)
{
int test_char[3];
int choice = num_system;

if (!param || param[0] == '\0')
return false;

if (param[0] == '-')
{
if ( (test_char[0] = param[1]) != 0 )
if ( (test_char[1] = param[2]) != 0 )
if ( (test_char[2] = param[3]) != 0 )
;
}
else
{
if ( (test_char[0] = param[0]) != 0 )
if ( (test_char[1] = param[1]) != 0 )
if ( (test_char[2] = param[2]) != 0 )
;
}

switch (choice)
{
case 0:
if ( (isdigit(test_char[0]) && test_char[1] != 'x')
|| ( (test_char[0] == '0') && (test_char[1] == 'x')
&& isxdigit(test_char[2]) ) )
return true;
break;
case 2:
if ( (test_char[0] == '0') || (test_char[0] == '1') )
return true;
break;
case 8:
if ( isdigit(test_char[0]) && ( test_char[0] < '8' ) )
return true;
break;
case 10:
if ( isdigit(test_char[0]) )
return true;
break;
case 16:
if ( isxdigit(test_char[0]) )
return true;
break;
default:
break;
}
return false;
}


hillhero789 2005-03-03
  • 打赏
  • 举报
回复
下边这个是我写的,没有测试 (刚刚写的)
我觉得这里堆栈的意义不大, 所以我觉得肯定是错了
希望大侠能指点一二
/*
输入一个十进制数,将它转化成2,8,16进制,用栈弹出的时候要按位从小到大来实现
*/

#define MAX_SIZE 100
#include<stdio.h>
int stack_mem[MAX_SIZE];
int currentSize = 0;

/*入栈函数*/
void push(int num)
{
if(currentSize++=MAX_SIZE)
{
printf("overflow!");
return;
}
else
{
stack_mem[currentSize] = num;
}

}

/*出栈函数*/
int pop()
{
if(!currentSize)
{
printf("the stack is empty!");
}
else
{
return stack_mem[currentSize--];
}
}

/*检查堆栈是否为空的函数*/
int isEmpty()
{
return currentSize==0?1:0;
}

/*用堆栈进行进制转换的函数*/
void calculate(int num) /*num是进制数*/
{
do
{
push(dec%num); /*这里用了入栈*/
dec/=2;
}while( dec!=0 )
}

void printResult()
{
int tmp;
while(!isEmpty())
{
tmp = pop(); /*这里用了出栈*/
if(tmp<10)
printf("%d",tmp);
else
printf("%c",'A'+tmp-10);
}
}

main()
{
int d_num;
printf("please input a positive decimal number:");
scanf("%d",&d_num); /*输入10进制数*/

calculate(2); /*10进制转化成2进制*/
calculate(8); /*10进制转化成8进制*/
calculate(16); /*10进制转化成16进制*/

printResult();
}
hillhero789 2005-03-03
  • 打赏
  • 举报
回复
我看不太懂你的意思
我知道以前学过进制转换是用 "除R取余的方法"

但是用堆栈进行转换我就不太懂了,能不能给个完整的程序我参考一下?
谢谢这位大侠
winstonch 2005-03-03
  • 打赏
  • 举报
回复
这不是数据类型转换.你应该知道,数在计算机中是以2进制补码形式存储的.

你是想实现那个进制转换的公式吧?
那你要把输入的数从高位到低位依次入栈,然后再出栈,同时进行计算,一定要从低位开始算,这有进位的问题.不会很难吧
hillhero789 2005-03-03
  • 打赏
  • 举报
回复
为什么是数字就不用转换?
我就是不太明白怎么通过堆栈来进行数据类型转换,请指教,谢谢
winstonch 2005-03-03
  • 打赏
  • 举报
回复
是数字就不用转了.
要是字符串可以先用atoi变成整数就行了
然后用sprintf来做就行了
hillhero789 2005-03-03
  • 打赏
  • 举报
回复
应该都可以,字符串或者数字都可以吧
winstonch 2005-03-03
  • 打赏
  • 举报
回复
输入是个字符串吗?

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧