+ -
当前位置:首页 → 问答吧 → 输出有问题,请高手看看是哪里的问题

输出有问题,请高手看看是哪里的问题

时间:2011-12-07

来源:互联网

#include<iostream>
using namespace std;
const int MAXSIZE=5;
struct datatype
{
char name[10],author[10],publish[20];
float price;
};
class Stack
{
private:
datatype data[MAXSIZE];
int top;
public:
Stack();
int Push_SeqStack(datatype x);
int Pop_SeqStack();
void Print_SeqStack();
};
Stack::Stack()
{
top=0;
}
int Stack::Push_SeqStack(datatype x)
{
strcpy(data[top].name,x.name);
strcpy(data[top].author,x.author);
strcpy(data[top].publish,x.publish);
  data[top].price=x.price;
top++;
return top;
}
int Stack::Pop_SeqStack()
{

if(top==0)
{
cout<<"Stack is empty.";
return 0;
}
top--;
return top;
}
void Stack::Print_SeqStack()
{
for(int i=0;i<top;i++)
cout<<data[top].name<<endl<<data[top].author<<endl<<data[top].publish<<endl<<data[top].price<<endl;
}
int main()
{
Stack s;
struct datatype x;
strcpy(x.name,"数据结构");
strcpy(x.author,"严蔚敏");
strcpy(x.publish,"清华大学出版社");
x.price=30.00;
s.Push_SeqStack(x);
s.Print_SeqStack();
}

作者: a18451175   发布时间: 2011-12-07

C/C++ code
#include<iostream>
using namespace std;
const int MAXSIZE=5;
struct datatype
{
char name[10],author[10],publish[20];
float price;
};
class Stack
{
private:
datatype data[MAXSIZE];
int top;
public:
Stack();
void Push_SeqStack(datatype x);
int Pop_SeqStack();
void Print_SeqStack();
};
Stack::Stack()
{
top=0;
}
void Stack::Push_SeqStack(datatype x)
{
    strcpy(data[top].name,x.name);
    strcpy(data[top].author,x.author);
    strcpy(data[top].publish,x.publish);
    data[top].price=x.price;
//    top++;    
//    return top;  
}
int Stack::Pop_SeqStack()
{
    
    if(top==0)
    {
        cout<<"Stack is empty.";
        return 0;
    }
    top--;
    return top;
}
void Stack::Print_SeqStack()
{
    for(int i=0;i<top;i++) ;   // 老大 下标为 i  不是top
        cout<<data[i].name<<endl<<data[i].author<<endl<<data[i].publish<<endl<<data[i].price<<endl;
}
int main()
{
    Stack s;
    struct datatype x;
    strcpy(x.name,"数据结构");
    strcpy(x.author,"严蔚敏");
    strcpy(x.publish,"清华大学出版社");
    x.price=30.00;
    s.Push_SeqStack(x);
    s.Print_SeqStack();
}

作者: yuegeman   发布时间: 2011-12-07