Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215693
  • 博文数量: 35
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 14:27
文章分类

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-03-23 09:50:41

/**设堆栈中的元素具有Comple类型,设计一个菜单方式的测试驱动程序,并使用此程序测试堆栈类seqstack
/**以下程序在vc++ 2005里编译通过                                                    */

// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
using namespace std;
enum ResultCode{UnderFlow,OverFlow};
template
class Complex;
template
Complex operator+(const Complex& a,const Complex& b);
template
Complex operator-(const Complex& a,const Complex& b);
template
Complex operator*(const Complex& a,const Complex& b);
template
Complex operator/(const Complex& a,const Complex& b);
template
ostream& operator<<(ostream& out,const Complex &b);
template
istream& operator>>(istream& in,Complex &b);
template
class SeqStack;
template
ostream &operator<<(ostream & out,const SeqStack & s);
template
class Stack
{
public:
 virtual void Push(const T &x)=0;
 virtual void Pop()=0;
 virtual T Top() const=0;
 virtual bool IsEmpty() const=0;
 virtual bool IsFull() const=0; 
};
template
class SeqStack:public Stack
{
public:
 SeqStack(int mSize);
 ~SeqStack(){delete[] s;}
 bool IsEmpty() const {return (top==-1);}
 bool IsFull() const {return (top==maxSize-1);}
 void Push(const T &x);
 void Pop();
 T Top() const;
private:
 void Output(ostream & out)const;
 T *s;
 int maxSize;
 int top;
 friend ostream &operator<< <> (ostream & out,SeqStack & s);
 
};
template
class Complex
{
public:
    Complex(const T& solid=T(),const T& weak=T()):x(solid),y(weak){}
    Complex Add(const Complex &a);
    Complex Sub(const Complex &a);
    Complex Mul(const Complex &a);
    Complex Div(const Complex &a);
    Complex& operator=(const Complex& a);
private:
    T x;
    T y;
    friend Complex operator+<>(const Complex& a,const Complex& b);
    friend Complex operator-<>(const Complex& a,const Complex& b);
    friend Complex operator*<>(const Complex& a,const Complex& b);
    friend Complex operator/<>(const Complex& a,const Complex& b);
    friend ostream& operator<< <>(ostream& out,const Complex &b);
    friend istream& operator>> <>(istream& in, Complex &b);
};
template
Complex Complex::Add(const Complex& a)
{
    x=x+a.x;
    y=y+a.y;
}
template
Complex Complex::Sub(const Complex &a)
{
    x=x-a.x;
    y=y-a.y;
}
template
Complex Complex::Mul(const Complex &a)
{
    x=x*a.x;
    y=y*a.y;
}
template
Complex Complex::Div(const Complex &a)
{
    x=x/a.x;
    y=y/a.y;
}
template
Complex operator+(const Complex& a,const Complex& b)
{
return Complex(a.x+b.x,a.y+b.y);
}
template
Complex operator-(const Complex& a,const Complex& b)
{
return Complex(a.x-b.x,a.y-b.y);
}
template
Complex operator*(const Complex& a,const Complex& b)
{
return Complex(a.x*b.x,a.y*b.y);
}
template
Complex operator/(const Complex& a,const Complex& b)
{
return Complex(a.x/b.x,a.y/b.y);
}
template
ostream& operator<<(ostream& out,const Complex& b)
{
out<<"("<return out;
}
template
istream& operator>>(istream& in, Complex& b)
{
in>>b.x>>b.y;
return in;
}
template
Complex& Complex::operator=(const Complex& a)
{
    x=a.x;
    y=a.y;
    return *this;
}
template
SeqStack::SeqStack(int mSize)
{
 maxSize=mSize;
 s=new T[maxSize];
 top=-1;
}
template
void SeqStack::Pop()
{
 if(IsEmpty()) throw UnderFlow;
  top--;
}
template
void SeqStack::Push(const T &x)
{
 if(IsFull()) throw OverFlow;
 s[++top]=x;
}
template
T SeqStack::Top() const
{
 if(IsEmpty()) throw UnderFlow;
 return s[top];
}
template
void SeqStack::Output(ostream& out) const
{
out<<"the stack contains:"<for(int i=top;i>-1;i--)
out<}
template
ostream& operator<<(ostream& out,SeqStack& s)
{
 s.Output(out);
 return out;
}
void introduction()
{
      cout<<"this is a test programme"<     cout<<"you can input only one command each time"<}
void help()
{
     cout<<"the valid command is as follows"<     cout<<"input P for Push a element in queue"<     cout<<"input O for Pop a element in queue"<     cout<<"input R for Print the element in queue"<     cout<<"input E for check whether the queue is empty"<     cout<<"input F for check whether the queue is full"<     cout<<"input Q for quit the programme"<     cout<<"input T for Retrieve and print the top element"<     cout<<"input H for show the help screen"<    
 }
char getcommmand()
 {
    char command;
    bool waiting=true;
    while(waiting)
    {
    help();
    cout<<"please input the commmand "<    cin>>command;
    command=tolower(command);
    if
    (command=='p'||command=='o'||command=='r'
    ||command=='e'||command=='f'||
    command=='q'||command=='t'||command=='h'
    )
    waiting=false;
    else
    {
     help();
     cout<<"please input a valid command:"<    }
 
  }
    return command;
}
template
bool docommand(char c,SeqStack &s)
  {
       T x;
       switch(c){
                case'p': cout<<"please input an element"<                cin>>x;
                s.Push(x);
                cout<                break;
                case'o':
                s.Pop();
                cout<<"the front element is poped"<                break;
                case'r':          
                cout<                cout<                break;
                case'e':
                if(s.IsEmpty())
                cout<                else
                cout<                break;
                case'f':
                if(s.IsFull())
                cout<                else
                cout<                break;
                case'q': cout<                return false;
                case't':
                cout<                break;
                case'h':
                help();
                break;
                }  
                return true;  
   }
 
int _tmain(int argc, _TCHAR* argv[])
{
 
SeqStack< Complex > s(20);
introduction();
cout<try
{
while(docommand(getcommmand(),s));   
}  
catch(ResultCode err){
switch(err)
       {
   case OverFlow:cout<<"OverFlow!"<   case UnderFlow:cout<<"UnderFlow"<       }
}
system("pause");
 return 0;
}
 
阅读(1714) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~