Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I'll post the code and the errors, thank you for helping me.

In main.cpp:
C++
// 6Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include "SeqStack.h"

#define maxsize_seq 50
#define end_tail 10000


int main(int argc, char* argv[])
{
//	printf("Hello World!\n");
	SeqStack<int> SeqStack1(maxsize_seq);
	int Push_Numbers_Int, Pop_Size, Pop_Number, Stack_Size;
	cout<<"Push the Stack! Stopped by 10000!"<<endl;
	while (1)
	{
		cin>>Push_Numbers_Int;
		if (Push_Numbers_Int!=end_tail)
		{
			SeqStack1.Push(Push_Numbers_Int);
		}
		else 
			break;
	}
	cout<<"Pop the Stack! Input the number of Poping out!"<<endl;
	cin>>Pop_Size;
	for (int i=0;i<Pop_Size;i++)
	{
		if (SeqStack1.Pop(Pop_Number))
		{
			cout<<Pop_Number<<endl;
		}
		else
			break;
	}

	cout<<"Head of the Stack:"<<endl;
	if (SeqStack1.Get_Top(Pop_Number))
	{
		cout<<Pop_Number<<endl;
	}
	else
		cout<<"Error!"<<endl;


	cout<<"The size of the Stack is:"<<endl;
	Stack_Size=SeqStack1.Get_Size();
	cout<<Stack_Size;
	return 0;
}


In SeqStack.cpp:
C++
<pre lang="vb">// SeqStack.cpp: implementation of the SeqStack class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SeqStack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////






In Stack.cpp:
C++
<pre lang="vb">// Stack.cpp: implementation of the Stack class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Stack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////





In SeqStack.h
C++
<pre lang="xml">// SeqStack.h: interface for the SeqStack class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)
#define AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <assert.h>
#include "Stack.h"
#define Stackincreament 20;
template<class T>
class SeqStack : public Stack<T>
{
public:
    SeqStack(int len);
    void Push(const T &x);
    bool Pop(T &x);
    bool Get_Top(T &x);
    bool Is_Empty();
    bool Is_Full();
    int Get_Size();
    virtual ~SeqStack();
private:
    T *elements;
    int top;
    int maxsize;
    void Overflow_Process();
};
template<class T>
class SeqStack<T>::SeqStack(int len)
{
    top=-1;
    maxsize=len;
    T *elements=new T[maxsize];
    assert(elements!=null);
}
template<class T>
void class SeqStack<>::Overflow_Process()
{
    T *NewArray=new T[maxsize+Stackincreament];
    if (NewArray==NULL)
    {
        cout<<"Error!!!"<<endl;
        exit(1);
    }
    maxsize=maxsize+Stackincreament;
    delete []elements;
    elements=NewArray;
}
template<class T>
void class SeqStack<T>::Push(const T &x)
{
    if (Is_Full()==true)
    {
        Overflow_Process();
    }
    elements[++top]=x;
}
template<class T>
bool class SeqStack<T>::Pop(T &x)
{
    if (Is_Empty()==true)
    {
        return false;
    }
    x=elements[top--];
    return true;
}
template<class T>
bool class SeqStack<T>::Get_Top(T &x)
{
    if (Is_Empty()==true)
    {
        cerr<<"Error!!!"<<endl;
        return false;
    }
    x=elements[top];
    return true;
}
template<class T>
int class SeqStack<T>::Get_Size()
{
    int Size_SeqStack;
    Size_SeqStack=top+1;
    return Size_SeqStack;
}
template<class T>
bool class SeqStack<T>::Is_Full()
{
    if (top==maxsize-1)
    {
        return true;
    }
    else
        return false;
}
template<class T>
bool class SeqStack<T>::Is_Empty()
{
    if (top!=maxsize-1)
    {
        return true;
    }
    else
        return false;
}
template<class T>
class SeqStack<T>::~SeqStack()
{
    delete []elements;
}
#endif // !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)





In Stack.h
C++
// Stack.h: interface for the Stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)
#define AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template<class T>
class Stack  
{
public:
	Stack();
        // pure virtual functions, cannot be used if not implemented in derived classes,
        // must be implemented in derived classes
	virtual void Push(const T &x)=0;
        virtual bool Pop(T &x)=0;
        
        // constant methods: cannot modify data members of this class:
	virtual bool Get_Top(T &x) const=0;
	virtual bool Is_Empty() const=0;
	virtual bool Is_Full() const=0;
	virtual int Get_Size() const=0;
	virtual ~Stack();


};

#endif // !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)


Error:

--------------------Configuration: 6Stack - Win32 Debug--------------------
Compiling...
6Stack.cpp
C:\Allfiles\Tony\Learning\Data_Structure\small_program\data_structure_operation\6Stack\6stack.cpp(56) : fatal error C1004: unexpected end of file found
SeqStack.cpp
C:\Allfiles\Tony\Learning\Data_Structure\small_program\data_structure_operation\6Stack\SeqStack.cpp(12) : fatal error C1004: unexpected end of file found
Generating Code...
Error executing cl.exe.

6Stack.exe - 2 error(s), 0 warning(s)
Posted
Updated 6-Mar-11 15:47pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Mar-11 21:52pm    
I translated from Chinese (because I understand the meaning of comments, not because I know Chinese; I know some 10 words :-)

Please, listen to a good friendly advice: use English-only in code, especially file names. It will help you even if you plan to leave in China and never leave. Chinese belongs in Resources. Needless to say, exposing any code using Chinese comments in English-language resource is very confusing.

I never use my native language in programming, it only helps me to write code working with my and other languages.
Sergey Alexandrovich Kryukov 6-Mar-11 21:54pm    
Now please mark line 56 and line 12 in your code to make you error messages completely clear.
--SA
zhaoyilong1 7-Mar-11 2:55am    
Thank you very much, I will do it myself next time. But can you help me with solving this problem?
Sergey Alexandrovich Kryukov 7-Mar-11 4:30am    
Needs time... and... did you mark those two line with errors in the code? too much bothering with putting your code in VS...
--SA

include "stdafx.h" in SeqStack.h and Stack.h
 
Share this answer
 
Comments
zhaoyilong1 7-Mar-11 2:54am    
I tried, but errors remained.
There A LOT of errors in your code:

1- change
#include <iostream.h>
into
#include <iostream>

2- in the main cpp file, after the includes, add
using namespace std;

3- change null into NULL
4- remove the constructor and destructor from the Stack class
5- in the implementation of Overflow_Process method, a <T> is missing
6- remove the class keyword from every method implementation
7- add virtual and const keywords in the SeqStack declaration to match
the Stack class prototypes.
8- add the const to the SeqStack methods implementation to match the previous declarations
9- change
#define Stackincreament 20;
into
#define Stackincreament 20


After doing all these changes, it should compile. :)
 
Share this answer
 
v3
Comments
zhaoyilong1 10-Mar-11 18:32pm    
Thank you! I changed it with your advice, but this error still remains...
Olivier Levrey 11-Mar-11 3:25am    
I copied the code on my computer, changed everything as described, and made it compile. What is the error message this time? If you don't post it, I can't help you.
Olivier Levrey 17-Mar-11 4:53am    
I sent the files but please edit your comment and remove your email address because robots can find it and use it to send you spams...
zhaoyilong1 17-Mar-11 19:49pm    
Thank you very much !
zhaoyilong1 21-Mar-11 11:11am    
Well, would you please send the file to me again, I was out days ago and reply you by phone. However, I came back to find I haven't got the email, could you please send it to me gain? Thank you!
Here is the complete corrected code:

stdafx.h
C++
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here

main.cpp
C++
// 6Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "SeqStack.h"

#define maxsize_seq 50
#define end_tail 10000

using namespace std;

int main(int argc, char* argv[])
{
//  printf("Hello World!\n");
    SeqStack<int> SeqStack1(maxsize_seq);
    int Push_Numbers_Int, Pop_Size, Pop_Number, Stack_Size;
    cout<<"Push the Stack! Stopped by 10000!"<<endl;
    while (1)
    {
        cin>>Push_Numbers_Int;
        if (Push_Numbers_Int!=end_tail)
        {
            SeqStack1.Push(Push_Numbers_Int);
        }
        else
            break;
    }
    cout<<"Pop the Stack! Input the number of Poping out!"<<endl;
    cin>>Pop_Size;
    for (int i=0;i<Pop_Size;i++)
    {
        if (SeqStack1.Pop(Pop_Number))
        {
            cout<<Pop_Number<<endl;
        }
        else
            break;
    }

    cout<<"Head of the Stack:"<<endl;
    if (SeqStack1.Get_Top(Pop_Number))
    {
        cout<<Pop_Number<<endl;
    }
    else
        cout<<"Error!"<<endl;


    cout<<"The size of the Stack is:"<<endl;
    Stack_Size=SeqStack1.Get_Size();
    cout<<Stack_Size;
    return 0;
}


Stack.h
C++
// Stack.h: interface for the Stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)
#define AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template<class T>
class Stack
{
public:
        // pure virtual functions, cannot be used if not implemented in derived classes,
        // must be implemented in derived classes
    virtual void Push(const T &x)=0;
        virtual bool Pop(T &x)=0;

        // constant methods: cannot modify data members of this class:
    virtual bool Get_Top(T &x) const=0;
    virtual bool Is_Empty() const=0;
    virtual bool Is_Full() const=0;
    virtual int Get_Size() const=0;
};

#endif // !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)


Stack.cpp
C++
// Stack.cpp: implementation of the Stack class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Stack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


SeqStack.h
C++
// SeqStack.h: interface for the SeqStack class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)
#define AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <assert.h>
#include "Stack.h"
#define Stackincreament 20
template<class T>
class SeqStack : public Stack<T>
{
public:
    SeqStack(int len);
    virtual void Push(const T &x);
    virtual bool Pop(T &x);
    virtual bool Get_Top(T &x) const;
    virtual bool Is_Empty() const;
    virtual bool Is_Full() const;
    virtual int Get_Size() const;
    virtual ~SeqStack();
private:
    T *elements;
    int top;
    int maxsize;
    void Overflow_Process();
};
template<class T>
SeqStack<T>::SeqStack(int len)
{
    top=-1;
    maxsize=len;
    T *elements=new T[maxsize];
    assert(elements!=NULL);
}
template<class T>
void SeqStack<T>::Overflow_Process()
{
    T *NewArray=new T[maxsize+Stackincreament];
    if (NewArray==NULL)
    {
        cout<<"Error!!!"<<endl;
        exit(1);
    }
    maxsize=maxsize+Stackincreament;
    delete []elements;
    elements=NewArray;
}
template<class T>
void SeqStack<T>::Push(const T &x)
{
    if (Is_Full()==true)
    {
        Overflow_Process();
    }
    elements[++top]=x;
}
template<class T>
bool SeqStack<T>::Pop(T &x)
{
    if (Is_Empty()==true)
    {
        return false;
    }
    x=elements[top--];
    return true;
}
template<class T>
bool SeqStack<T>::Get_Top(T &x) const
{
    if (Is_Empty()==true)
    {
        cerr<<"Error!!!"<<endl;
        return false;
    }
    x=elements[top];
    return true;
}
template<class T>
int SeqStack<T>::Get_Size() const
{
    int Size_SeqStack;
    Size_SeqStack=top+1;
    return Size_SeqStack;
}
template<class T>
bool SeqStack<T>::Is_Full() const
{
    if (top==maxsize-1)
    {
        return true;
    }
    else
        return false;
}
template<class T>
bool SeqStack<T>::Is_Empty() const
{
    if (top!=maxsize-1)
    {
        return true;
    }
    else
        return false;
}
template<class T>
SeqStack<T>::~SeqStack()
{
    delete []elements;
}
#endif // !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)


SeqStack.cpp
C++
// SeqStack.cpp: implementation of the SeqStack class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SeqStack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
Share this answer
 
v2
Comments
zhaoyilong1 21-Mar-11 19:25pm    
Thank you !
zhaoyilong1 21-Mar-11 21:25pm    
I cannot find the difference, but mine is not working, I paste it as a solution too, can you figure out the difference between mine and the correct ansewer for me please?
Olivier Levrey 22-Mar-11 4:46am    
All the differences are described in my previous answer. To see them easily, use a merge tool (it shows differences between 2 files) like winmerge or windiff from microsoft. For example: http://winmerge.org/
main.cpp:

C++
// 6 栈.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "SeqStack.h"

#define maxsize_seq 50
#define end_tail 10000

using namespace std;

int main(int argc, char* argv[])
{
//	printf("Hello World!\n");
	SeqStack<int> SeqStack1(maxsize_seq);
	int Push_Numbers_Int, Pop_Size, Pop_Number, Stack_Size;
	cout<<"Push the Stack! Stopped by 10000!"<<endl;
	while (1)
	{
		cin>>Push_Numbers_Int;
		if (Push_Numbers_Int!=end_tail)
		{
			SeqStack1.Push(Push_Numbers_Int);
		}
		else 
			break;
	}
	cout<<"Pop the Stack! Input the number of Poping out!"<<endl;
	cin>>Pop_Size;
	for (int i=0;i<Pop_Size;i++)
	{
		if (SeqStack1.Pop(Pop_Number))
		{
			cout<<Pop_Number<<endl;
		}
		else
			break;
	}

	cout<<"Head of the Stack:"<<endl;
	if (SeqStack1.Get_Top(Pop_Number))
	{
		cout<<Pop_Number<<endl;
	}
	else
		cout<<"Error!"<<endl;


	cout<<"The size of the Stack is:"<<endl;
	Stack_Size=SeqStack1.Get_Size();
	cout<<Stack_Size; 
	return 0;
}


stack.h:

C++
// Stack.h: interface for the Stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)
#define AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template<class t="">
class Stack  
{
public:
	virtual void Push(const T &x)=0;//=0表示纯虚函数,不能实现,子类必须实现!
	virtual bool Pop(T &x)=0;
	virtual bool Get_Top(T &x) const=0;//可以操作常量成员,只能调用const函数,不能修改类数据成员。
	virtual bool Is_Empty() const=0;
	virtual bool Is_Full() const=0;
	virtual int Get_Size() const=0;
	

};

#endif // !defined(AFX_STACK_H__A023084D_646A_4916_BC79_6C1E85CD4940__INCLUDED_)

</class>


stack.cpp:

C++
// Stack.cpp: implementation of the Stack class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Stack.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


SeqStack.h:
// SeqStack.h: interface for the SeqStack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)
#define AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <assert.h>
#include "Stack.h"

#define Stackincreament 20

template<class t="">
class SeqStack : public Stack<t>  
{
public:
	SeqStack(int len);
	virtual void Push(const T &x);
	virtual bool Pop(T &x);
	virtual bool Get_Top(T &x) const;
	virtual bool Is_Empty() const;
	virtual bool Is_Full() const;
	virtual int Get_Size() const;
	virtual ~SeqStack();
private:
	T *elements;
	int top;
	int maxsize;
	void Overflow_Process();
};

template<class t="">
SeqStack<t>::SeqStack(int len)
{
	top=-1;
	maxsize=len;
	T *elements=new T[maxsize];
	assert(elements!=NULL);
}

template<class t="">
void SeqStack<t>::Overflow_Process()
{
	T *NewArray=new T[maxsize+Stackincreament];
	if (NewArray==NULL)
	{
		cout<<"Error!!!"<<endl;
		exit(1);
	}
	maxsize=maxsize+Stackincreament;
	delete []elements;
	elements=NewArray;
}

template<class t="">
void SeqStack<t>::Push(const T &x)
{
	if (Is_Full()==true)
	{
		Overflow_Process();
	}
	elements[++top]=x;
}

template<class t="">
bool SeqStack<t>::Pop(T &x)
{
	if (Is_Empty()==true)
	{
		return false;
	}
	x=elements[top--];
	return true;
}

template<class t="">
bool SeqStack<t>::Get_Top (T &x) const
{
	if (Is_Empty()==true)
	{
		cerr<<"Error!!!"<<endl;
		return false;
	}
	x=elements[top];
	return true;
}

template<class t="">
int SeqStack<t>::Get_Size ()const
{
	int Size_SeqStack;
	Size_SeqStack=top+1;
	return Size_SeqStack;
}

template<class t="">
virtual bool SeqStack<t>::Is_Full ()const
{
	if (top==maxsize-1)
	{
		return true;
	}
	else 
		return false;
}

template<class t="">
virtual bool SeqStack<t>::Is_Empty ()const
{
	if (top!=maxsize-1)
	{
		return true;
	}
	else 
		return false;
}

template<class t="">
class SeqStack<t>::~SeqStack()
{
	delete []elements;
}
#endif // !defined(AFX_SEQSTACK_H__94EF3A76_EB42_4B3D_BE22_1B9F8E253D2D__INCLUDED_)

</t></class></t></class></t></class></t></class></t></class></t></class></t></class></t></class></t></class></t></class></assert.h>


SeqStack.cpp:
C++
// SeqStack.cpp: implementation of the SeqStack class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SeqStack.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


stdafx.h:
C++
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__10471F60_C09E_4D72_92E3_A9B3BF30CEAA__INCLUDED_)
#define AFX_STDAFX_H__10471F60_C09E_4D72_92E3_A9B3BF30CEAA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <stdio.h>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__10471F60_C09E_4D72_92E3_A9B3BF30CEAA__INCLUDED_)

</stdio.h>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900