|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么就是显示不出来,一直找不出哪里出问题了
#include <iostream>
#include <string>
#include <math.h>
#define STACK_INIT_SIZE 32
using namespace std;
typedef struct sqStack
{
char *base;
char *top;
int stacksize;
};
typedef struct interStack //中间堆栈
{
char *base;
char *top;
int stacksize;
};
void InitStack(sqStack *s)
{
s->base = new char [STACK_INIT_SIZE];
if(!s->base)
{
exit(0);
}
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
}
void Push(sqStack *s, char e)
{
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, char *e)
{
if(s->top == s->base)
{
return;
}
*e = *--(s->top);
}
void InitStack(interStack *i)
{
i->base = new char [STACK_INIT_SIZE];
if(!i->base)
{
exit(0);
}
i->top = i->base;
i->stacksize = STACK_INIT_SIZE;
}
void Push(interStack *i, char e)
{
*(i->top) = e;
i->top++;
}
void Pop(interStack *i, char *e)
{
if(i->top == i->base)
{
return;
}
*e = *--(i->top);
}
int StackLen(sqStack s)
{
return (s.top-s.base);
}
int StackLen(interStack i)
{
return (i.top-i.base);
}
int main()
{
char c, it, isum = 0;
sqStack s;
interStack ins; //中间堆栈
int choose = 0, len, ilen, i, j, k, sum = 0;
InitStack(&s);
InitStack(&ins);
cout << "请输入二进制数,输入#符号结束:";
cin >> c;
while(c != '#')
{
Push(&s, c);
cin >> c;
}
len = StackLen(s);
ilen = StackLen(ins);
cout << endl << "栈的当前容量是:" << len << endl << endl;
cout << "请问转换为几进制数【1表示十,2表示八,3表示十六,0表示退出】:" ;
cin >> choose;
switch(choose)
{
case 1:
{
for(i = 0; i < len; i++)
{
Pop(&s, &c);
sum = sum+(c-48)*pow(2,i);
}
cout << "转换后的十进制数是:" << sum << endl;
break;
}
case 2:
{
if(len%3 == 0)
k = 0;
k = 1;
for(i = 0; i < len/3+k; i++)
{
for(j = 0; j < 3 && s.top != s.base; j++)
{
Pop(&s, &c);
sum = sum+(c-48)*pow(2,j);
}
isum = sum+48;
Push(&ins, isum);
}
cout << "转换后的八进制数是:";
for(i = 0; i < ilen; i++)
{
Pop(&ins, &it);
cout << it;
}
break;
}
case 3:
{
if(len%4 == 0)
k = 0;
k = 1;
for(i = 0; i < len/4+k; i++)
{
for(j = 0; j < 4 && s.top != s.base; j++)
{
Pop(&s, &c);
sum = sum+(c-48)*pow(2,j);
}
if(sum > 9)
isum = 64+sum-9;
isum = sum+48;
Push(&ins, isum);
}
cout << "转换后的十六进制数是:";
for(i = 0; i < ilen; i++)
{
Pop(&ins, &it);
cout << it;
}
break;
}
default:
break;
}
cout << endl << endl;
return 0;
}
|
|