回 帖 发 新 帖 刷新版面

主题:C++实现 数据结构遇到的问题`HELP ME!

在栈中 10进制 转换为8进遇到的问题` 本人菜`HELP ME`
#include<iostream.h>
#include<malloc.h>
#define MAXLEN 100;
#define STACKINCREMENT  10;
 
 struct sqstack
{
  int *base;
  int *top;
  int length;
};

sqstack initstack(sqstack &s)
{
  
  s.base=(int *)malloc(MAXLEN * sizeof(int));
  if(!s.base)
  {
   cout<<"ERROR"<<endl;
  }
  s.top=s.base;
  s.length=MAXLEN;
  return s;
}
sqstack push(sqstack &s,int a){
    if(s.top-s.base>=s.length){
     s.base=(int *)malloc((MAXLEN+STACKINCREMENT)*(sizeof(int)));
    
    if(!s.base){
    cout<<"ERROR";
    }
    s.top=s.base+s.length;
    s.length+=STACKINCREMENT;
    
    }
    *s.top++=a;
}
sqstack pop(sqstack &s,int &a){
 if(s.top=s.base) cout<<"ERROR";
 a=*s.top--;
}
Empty(sqstack s)
{
 int empty=1;
 if(s.top==0) empty=0;
 return empty;

}
void conversion(sqstack s,int a){
    
 while(a){
 s=push(s,a%8);
      a=a/8;
 }
 while(Empty(s)){
 pop(s,a);
     cout<<a;
 }
}



void main(){
int a;
sqstack s;
sqstack initstack(sqstack &s);
cout<<"input a number";
 cin>>a;
 void conversion(sqstack s,int a);
}

回复列表 (共3个回复)

沙发

while(Empty(s)){
 pop(s,a);
     cout<<a;


判断条件不对吧

板凳

#include<stack>
#include<iostream>

using namespace std;

int main()
{
    stack<int> a;
    int in,in1,in2;
    int temp;
    cout<<"请输入一个数 : "<<endl;
    cin>>in;
    in2 = in;
    do
    {
    cout<<"请输入.目标进制数的基数 : "<<endl;
    cin>>in1;
    }while(in1<0 || in1>9);
    while(in>=in1)
    {
        temp = in%in1;
        in = (in - temp) / in1;
        a.push(temp);
    }
    a.push(in);
    cout<<"你输入的数为 : "<<in2<<'\t'<<"转换为"<<in1<<"进制为 : ";
        while(!a.empty())
        {
            int x;
            x = a.top();
            a.pop();
            cout<<x;
        }
        cout<<endl;
        return 0;
}

3 楼

能将10进制转换成 2-9进制..
  如果超过10进制..!  
    则需要修改一下程序.. 呵呵..!

我来回复

您尚未登录,请登录后再回复。点此登录或注册