C语言:关于进制转化的问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 03:44:08
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct Lsnode
{
ElemType data;
struct Lsnode *next;
}node;
void initStack(node *top)
{
top->next=NULL;
}
void push(node *top,int x)
{
node *p;
p=(node *)malloc(sizeof(node));
p->data=x;
p->next=top->next;
top->next=p;
}
int pop(node *top)
{
node *p;
int x;
if(top->next!=NULL)
{
p=top->next;
top->next=p->next;
x=p->data;
free(p);
}
else
{
printf("Stack null!\n");
return '#';
}
}
void convert(int x,int n)
{
node *s;
int y;
s=(node *)malloc(sizeof(node));
while(x>0)
{
y=x%n;
x=x/n;

首先感觉思路有点不对。你输入的是10进制数,输出的还是10进制啊。而且,转换的时候,都是按类似“除二取余倒着读”的老方法,有点问题。

另外,你目前的错误主要在输出。不可以这样输出的哦。看看链表是怎么输出的吧。

/* *********************************
* Filename:changedate.c
* Author:hufeng Date:2007/11/11
* Version:v1.0
* *********************************/

/* header file */
#include <stdio.h>
#include <stdlib.h>

/* creat a struct */
typedef struct linkstack
{
int date;
struct linkstack *next;
}ls;

/* change date */
void change_date(ls *top, int num)
{

ls *p = NULL;

while (num != 0)
{
p = (ls*)malloc(sizeof(ls));
p->date = num % 2;

p->next = top->next;
top->next = p;

num = num / 2;
}
}

/* pop_linkstack */
void pop_linkstack(ls *top)
{
ls *p = NULL ;

w