| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2150 人关注过本帖
标题:我写的c++程序 关于十进制转换成二进制 有错误想请大家指点
取消只看楼主 加入收藏
与寻
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-11
收藏
 问题点数:0 回复次数:2 
我写的c++程序 关于十进制转换成二进制 有错误想请大家指点

我写的程序,十进制转换成二进制,用c++编的。找了好久也没发现错误 ,调试说是在push 函数中错了 可不知道怎么改!
别的地方也有不合适的,在这想请教一下高手,帮忙看看 谢谢!
struct SXZ
{
int MAX;
int t;
int *s;
};
typedef struct SXZ * PSXZ;
#include <stdio.h>
#include <stdlib.h>
#include "10_2.h"
PSXZ creat_SXZ(int i);
int isNULLSXZ(PSXZ m);
void push(PSXZ sxz,int x);
int qzd(PSXZ sxz);
//以上是头文件

PSXZ creat_SXZ(int i) //创建一个空栈
{
PSXZ sxz;
sxz=(PSXZ)malloc(sizeof(struct SXZ));
if(sxz!=NULL)
{
sxz->s=(int*)malloc(sizeof(int)*i);
if(sxz->s)
{sxz->MAX=i;
sxz->t=-1;
return (sxz);
}
}
else
printf("Out of space!\n");
return NULL;
}
int isNULLSXZ(PSXZ sxz) //判断是否栈为空
{
return(sxz->t==0);
}
void push(PSXZ sxz,int x) // 将得到的值放进栈中
{
if(sxz->t==MAX-1)
printf("Overflow\n");
else
{
sxz->t=sxz->t+1;
sxz->s[sxz->t]; //调试就说这里不对
}
}
int qzd(PSXZ sxz) //取栈顶元素
{
if(sxz->t==-1)
printf("Underflow!\n");
else
return(sxz->s[sxz->t]);

}
//下面是主函数
#include <stdio.h>
#include "10_2.h"
int main(void)
{
int k;
PSXZ z;
z=creat_SXZ(20);
printf("输入一个十进制数:");
scanf("%d",&k);
while(k)
{
push(z,k%2);
k=k/2;
}
while(!isNULLSXZ(z))
{
printf("%d",qzd(z));
}
printf("\n");
return 0;
}

搜索更多相关主题的帖子: 二进制 int 十进制 PSXZ 
2006-11-22 20:20
与寻
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-11
收藏
得分:0 

不好意思,这两天我宿舍老是网络限制。

下面是我改过的程序:

struct SXZ
{
int MAX;
int t;
int *s;
};
typedef struct SXZ * PSXZ;
PSXZ creat_SXZ(int i);
int isNULLSXZ(PSXZ m);
void push(PSXZ sxz,int x);
int qzd(PSXZ sxz);

#include <stdio.h>
#include <stdlib.h>
#include "shi_er.h"
PSXZ creat_SXZ(int i)
{
PSXZ sxz;
sxz=(PSXZ)malloc(sizeof(struct SXZ));
if(sxz!=NULL)
{
sxz->s=(int*)malloc(sizeof(int)*i);
if(sxz->s!=NULL) //我觉得是错在这, 原来的是if(sxz-s)。因为这个判断只能是0或1,而sxz-s不能取这两个值。
{
sxz->MAX=i;
sxz->t=-1;
return (sxz);
}
}
else
printf("Out of space!\n");
return NULL;
}
int isNULLSXZ(PSXZ sxz)
{
return(sxz->t==-1); //这地方原来是return(sxz->t==0); 也不对
}
void push(PSXZ sxz,int x)
{
if(sxz->t==sxz->MAX-1)
printf("Overflow\n");
else
{
sxz->t=sxz->t+1;
sxz->s[sxz->t]=x;
}
}
int qzd(PSXZ sxz)
{
if(sxz->t==-1)
printf("Underflow!\n");
else
return(sxz->s[sxz->t--]);
}

#include <stdio.h>
#include "shi_er.h"
int main(void)
{
int k;
PSXZ z;
z=creat_SXZ(60);
printf("输入一个十进制数:");
scanf("%d",&k);
while(k)
{
push(z,k%2);
k=k/2;
}
while(!isNULLSXZ(z))
{
printf("%d",qzd(z));
}
printf("\n");
return 0;
}





这样解释对吗?是否还存在概念理解的错误,或者别的错误,请指点,谢谢!



2006-11-24 19:54
与寻
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-11
收藏
得分:0 

这么说的话
struct stack
{
int a[N];//栈的存储空间
int top; //栈顶下标.
}
我那个n就是N,t就是top,只是把它们分开了,这样看起来不是很明显。我会注意!
谢谢侬!

2006-11-24 22:19
快速回复:我写的c++程序 关于十进制转换成二进制 有错误想请大家指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011459 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved