main.cpp
#include <iostream>
#include <cstdlib>
#include "implementation.cpp"
using namespace std;
class Fishing
{
public:
void initialize();
void start();
private:
void input(Queue<int> &player);
void drawCard(Queue<int> &player);
Queue<int> Aq, Bq;
Stack<int> desk;
int n;
};
void Fishing::initialize()
{
cout << "请输入两人起始手牌的数量:";
cin >> n;
cout << "请输入A手中初始牌的号码:";
input(Aq);
cout << "请输入B手中初始牌的号码:";
input(Bq);
}
void Fishing::input(Queue<int> &player)
{
int number, count = 0;
while (count < n)
{
if (!(cin >> number))
{
cout << "您的输入有误,请重新输入:";
cin.clear();
cin.ignore(100, '\n');
}
else
{
player.append(number);
count++;
}
}
}
void Fishing::drawCard(Queue<int> &player)
{
int card, temp;
card = player.serve();
if (desk.exist(card))
{
player.append(card);
while ((temp = desk.pop()) != card)
player.append(temp);
player.append(temp);
}
else
desk.push(card);
}
void Fishing::start()
{
while (!(Aq.empty() || Bq.empty()))
{
drawCard(Aq);
drawCard(Bq);
cout << "the cards on the desk:";
desk.printAll();
cout << endl << "the cards in A's hand:";
Aq.printAll();
cout << endl << "the cards in B's hand:";
Bq.printAll();
cout << endl;
cout << "************************" << endl;
}
if (Aq.empty())
cout << "B wins!" << endl;
else
cout << "A wins!" << endl;
}
int main()
{
Fishing fishing;
fishing.initialize();
fishing.start();
return 0;
}
implementation.cpp
#include "statement.h"
template <class T>
List<T>::List()
{
head = tail = NULL;
}
template <class T>
List<T>::~List()
{
Node<T> *temp;
while (temp = head)
{
head = temp -> next;
delete temp;
}
}
template <class T>
bool List<T>::empty()
{
//cout << head << "haha";
return head == NULL;
}
template <class T>
void Queue<T>::append(T data)
{
Node<T> *temp = new Node<T>;
temp -> data = data;
temp -> next = NULL;
if (!head)
head = temp;
else
tail -> next = temp;
tail = temp;
}
template <class T>
T Queue<T>::serve()
{
T returnedData;
Node<T> *temp;
if (head)
{
returnedData = head -> data;
temp = head;
head = head -> next;
delete temp;
return returnedData;
}
else
{
cerr << "列表是空的!" << endl;
abort();
}
}
template <class T>
bool List<T>::exist(T data)
{
Node<T> *temp = head;
while (temp && temp -> data != data)
temp = temp -> next;
return temp;
}
template <class T>
void Stack<T>::push(T data)
{
Node<T> *temp = new Node<T>;
temp -> data = data;
temp -> next = head;
head = temp;
}
template <class T>
T Stack<T>::pop()
{
T returnedData;
Node<T> *temp;
if (head)
{
returnedData = head -> data;
temp = head;
head = head -> next;
delete temp;
return returnedData;
}
else
{
cerr << "栈是空的!" << endl;
abort();
}
}
template <class T>
void Queue<T>::printAll()
{
Node<T> *temp = head;
while (temp)
{
cout << temp -> data << " ";
temp = temp -> next;
}
}
template <class T>
void Stack<T>::printAll(Node<T> *position = NULL)
{
if (!head) return;
if (!position) position = head;
Node<T> *temp = position -> next;
if (temp)
printAll(temp);
cout << position -> data << " ";
}
statement.h
template <class T>
struct Node
{
T data;
Node *next;
};
template <class T>
class List
{
public:
List();
~List();
bool exist(T data);
bool empty();
protected:
Node<T> *head, *tail;
};
template <class T>
class Queue : public List<T>
{
public:
void append(T);
T serve();
void printAll();
};
template <class T>
class Stack : public List<T>
{
public:
void push(T);
T pop();
void printAll(Node<T> *position = NULL);
};