这个顺序栈进制转换实现不成功的原因是什么呀求指点(图一是参考的算法,图二是参考图一写的
#include<iostream>
#include<malloc.h>
#include <string>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack * &s);
bool StackEmpty(SqStack * s) ;
bool Push(SqStack * &s,ElemType e);
int StackLength(SqStack * s);
void DisplayStack(SqStack *s);
bool GetTop(SqStack*s,ElemType &e);
bool Pop(SqStack * &s,ElemType &e);
void DestroyStack(SqStack * &s);
string dec2base(int num,int base);
int main() {
int num,base,numStr;
cout<<"输入十进制数:";
cin>>num;
cout<<"要将其转换为几进制:";
cin>>base;
cout<<"转换后为:";
dec2base(num,base);
return 0;
}
void InitStack(SqStack * &s) {
s = (SqStack*) malloc(sizeof(SqStack));
s->top=-1;
}
bool StackEmpty(SqStack * s) {
return (s->top==-1);
}
bool Push(SqStack * &s,ElemType e) {
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
int StackLength(SqStack * s) {
return(s->top+1);
}
void DisplayStack(SqStack *s) {
for(int i=s->top; i>=0; i--) {
cout<<s->data[i]<<" ";
}
cout<<endl;
}
bool GetTop(SqStack*s,ElemType &e) {
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
bool Pop(SqStack * &s,ElemType &e) {
if(s->top== -1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
void DestroyStack(SqStack * &s) {
free(s);
}
string dec2base(int num,int base) {
string digitChar = "0123456789ABCDEF";
string numStr = "";
SqStack * s;//
InitStack(s);//
ElemType e;//
do {
Push(s, digitChar[num%base]); //将余数入栈
num/=base;
} while(num != 0);
char temp;
while(!StackEmpty(s)) {
temp = s->top; //取栈顶
Pop(s,e); //出栈
printf("%d",e);
numStr += temp; //将余数出栈并附到numStr字符串中
}
return numStr;
}
emm,最好还是把代码也贴出来,这样子即使看不出来是什么问题,也可能有人会拿代码去运行,帮你debug 出来
#include<iostream>
using namespace std;
int oneSort(int a[],int x,int y); //一次划分函数
void qSort(int a[],int,int); //快速排序函数
int main(){
int a[]={3,6,5,9,7,1,8,2,4};
//int a[]={1,2,3,4,5,6,7,8,9};
int n=sizeof(a)/sizeof(int);
qSort(a,0,n-1);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
int oneSort(int a[],int x,int y){
int i=x; //i从左往右走
int j=y; //j从右往左走
int tmp=a[x]; //把第一个数当轴值,保存到tmp中
while(i<j){ //当i<j时,循环
while(tmp<a[j]&&i<j) //如果右边的值比轴值大就循环
j--;
if(i<j){
a[i]=a[j]; //此时tmp>a[j],将a[j]的值赋值给a[i],然后i++
i++;
}
while(a[i]<tmp&&i<j) //如果左边的值比轴值小就循环
i++;
if(i<j){
a[j]=a[i]; //此时tmp<a[i],将a[i]的值赋值给
//a[j],然后j--
j--;
}
}
a[i]=tmp; //最后i,j的位置的值就是轴值所在的值tmp,此时,
//i左边的值都比tmp小,i右边的值都比tmp大,
//完成一次划分
return i; //返回i的位置,然后对i左边和右边递归进行划分
}
void qSort(int a[],int x,int y){
if(x<y){
int k=oneSort(a,x,y); //取得一次划分的轴值,再对轴
//值左右边进行递归划分
qSort(a,x,k-1);
qSort(a,k+1,y);
}
}
我可以帮你指点一下你的问题所在。 你的代码中需要进行以下几个调整:
对于入栈操作,栈顶指针应该先增加,再给数组元素赋值。
出栈操作前应该判断栈是否为空,如果为空需要提示并返回错误信息。
在进制转换过程中,应该不断使用除法和取余操作,将余数逆序入栈,直到商为0。
以下是修改后的代码:
#include<iostream>
using namespace std;
const int MAXSIZE = 100;
class Stack
{
private:
int top;
int data[MAXSIZE];
public:
Stack();
~Stack();
bool push(int);
bool pop(int&);
bool isEmpty();
};
Stack::Stack()
{
top = -1;
}
Stack::~Stack()
{
}
bool Stack::push(int x)
{
if(top == MAXSIZE-1)
return false;
top++;
data[top] = x;
return true;
}
bool Stack::pop(int& x)
{
if(top == -1)
return false;
x = data[top];
top--;
return true;
}
bool Stack::isEmpty()
{
return top == -1;
}
int main()
{
int num, base;
Stack s;
cout << "Please input a number and a base:" << endl;
cin >> num >> base;
if(base < 2 || base > 10)
{
cout << "Base must be between 2 and 10." << endl;
return 0;
}
if(num == 0)
{
cout << 0 << endl;
return 0;
}
while(num)
{
int remainder = num % base;
s.push(remainder);
num /= base;
}
while(!s.isEmpty())
{
int x;
if(s.pop(x))
{
cout << x;
}
else
{
cout << "Unexpected error." << endl;
}
}
cout << endl;
return 0;
}
希望能对你有所帮助。