怎么会不对呢?求大神......
#include<iostream>
#include<math.h>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define OK 1
#define OVERFLOW -1
//栈:
typedef struct {
int *base;
int *top;
int stacksize;
}sqstack;
//构建一个空栈
int initstack(sqstack *s){
s->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s->base)
exit(OVERFLOW);
//存储分配失败(健壮性)
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
//进栈
void Push(sqstack *s,int e){
if(s->top-s->base>=s->stacksize){ //栈满,追加存储空间
s->base=(int *)realloc(s->base,(s->stacksize+STACKINCREMENT) * sizeof(int));
if(!s->base)
exit(OVERFLOW);
//存储分配失败(健壮性)
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top++)=e;
}
//出栈
int Pop(sqstack *s,int *e){
//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
if(s->top==s->base)
return ERROR;
*e=* (--s->top);
return OK;
}
//判断已存栈是否为空
int StackEmpty(sqstack *s){
return (s->top==s->base);
}
//数制转换
void conversion(int n, int m){
sqstack s;
int e;
initstack(&s);
while(n){
Push(&s,n%m);
n=n/m;
}
while(!StackEmpty(&s)){
Pop(&s,&e);
cout<<e;
}
}
//队列:
typedef struct qnode{
int data;
struct qnode * next;
}qnode,*queueptr;
typedef struct{
queueptr *front;
queueptr *rear;
}linkqueue;
//构建队列
int initqueue(linkqueue *q){
(q->front) = (q->rear)= (queueptr) malloc(sizeof(qnode));
if(!q->front)exit(OVERFLOW);
(q->front)->next=NULL;
return OK;
}
//插入元素
int enqueue(linkqueue *q,int f){
qnode *p;
p=(queueptr)malloc(sizeof(qnode));
if(!p)exit(OVERFLOW);
(p->data)=f;
(p->next)=NULL;
(q->rear)->next= p;
(q->rear)=p;
return OK;
}
//删除元素
int dequeue(linkqueue *q,int &f){
if(q->front==q.rear)renturn ERROR;
P=q->front->next;
f=p->data;
q->front->next=p->next;
if(q->rear==p)q->rear=q->front;
free(p);
return OK;
}
//小数部分的转换
void conversion2(float n,int m){
linkqueue q;
float n;
linkqueue(&q);
while(n){
r=n*m;
int floor(float r); //向下取整
enqueue(&q,r);
n=n*m-r;
}
while(!q->front=q->rear){
dequeue(&q,&f);
cout<<f;
}
}
//主函数
void main(){
int N,m;
float M;
cout<<" 请输入一个非负的十进制数: ";
cout<<"整数部分: ";
cin>>N;
cout<<"小数部分(0.--形式): ";
cin>>M;
cout<<" 请键入需要转换的进制数: "<<endl;
cout<<" 1.转换成二进制数"<<endl;
cout<<" 2.转换成八进制数"<<endl;
cout<<" 3.转换成十六进制数"<<endl;
cout<<" 0.退出程序"<<endl;
int t=1;
while(t){
cin>>m;
switch(m){
case 1:cout<<" 转换成的二进制数为:";
conversion(N,2);
cout<<".";
conversion2(M,2);
break;
case 2:cout<<" 转换成的八进制数为:";
conversion(N,8);
cout<<".";
conversion2(M,8);
break;
case 3:cout<<" 转换成的十六进制数为:";
conversion(N,16);
cout<<".";
conversion2(M,16);
break;
case 0:cout<<" 退出程序!";
break;
default:cout<<" 输入有误,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}