From fa8cb8a459d1411a73156341e0b49209e9235f3b Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 22 Oct 2021 19:25:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=A0=86=E4=BF=AE=E4=B8=8D=E5=A5=BD?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex5/ex5.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++++++++-- ex5/list.cpp | 82 ++++++++++++++++++++++++++++++++-- ex5/list.h | 39 +++++----------- ex5/vec.cpp | 6 +++ ex5/vec.h | 11 ++++- 5 files changed, 224 insertions(+), 38 deletions(-) diff --git a/ex5/ex5.cpp b/ex5/ex5.cpp index b29bc82..7eae7b3 100644 --- a/ex5/ex5.cpp +++ b/ex5/ex5.cpp @@ -2,23 +2,139 @@ #include "list.cpp" #include #include - +std::string PU[54] = { + "1A", "1B", "1C", "1D", + "2A", "2B", "2C", "2D", + "3A", "3B", "3C", "3D", + "4A", "4B", "4C", "4D", + "5A", "5B", "5C", "5D", + "6A", "6B", "6C", "6D", + "7A", "7B", "7C", "7D", + "8A", "8B", "8C", "8D", + "9A", "9B", "9C", "9D", + "10A", "10B", "10C", "10D", + "JA", "JB", "JC", "JD", + "QA", "QB", "QC", "QD", + "KA", "KB", "KC", "KD", + "BlackJoker", "RadJoker"}; int main() { + + int xipai_flag = 0; + printf("生成随机扑克序列"); srand(time(NULL)); - Vec v(54,2); + Vec v(54, 2); v.printall(); List l; printf("开始建立链表\n"); - for(int i=0;i<=53;i++) + for (int i = 0; i <= 53; i++) { l.firstInsert(v.get(i)); - printf("%d\n",l[0]); + std::cout << PU[l[0]]; + printf("\n"); } +Begin: + printf("是否重新洗牌?(0/1)"); + scanf("%d", &xipai_flag); + + if (xipai_flag == 1) + { + v.reorder(); + + l.clear(); + + printf("开始建立链表\n"); + + for (int i = 0; i <= 53; i++) + { + l.firstInsert(v.get(i)); + std::cout << PU[l[0]]; + printf("\n"); + } + xipai_flag == 0; + goto Begin; + } + printf("开始发牌\n"); + List p1, p2, p3; + for (int i = 0; i <= 53; i++) + { + switch (i % 3) + { + case 0: + p1.firstInsert(l[i]); + break; + case 1: + p2.firstInsert(l[i]); + break; + case 2: + p3.firstInsert(l[i]); + break; + } + } + l.clear(); + // p1.sort(); + // p2.sort(); + // p3.sort(); + int player = 0, opt = 0; + List *pai; +select: + printf("选择玩家(1/2/3)"); + scanf("%d", &player); + switch (player) + { + case 1: + pai = &p1; + break; + case 2: + pai = &p2; + break; + case 3: + pai = &p3; + break; + default: + printf("失败\n"); + goto select; + } + while (1) + { + printf("first:%d",(*pai).first()->data); + printf("1看牌2出牌3查看牌堆4结束\n"); + scanf("%d", &opt); + switch (opt) + { + case 1: + for (int i = 0; i < (*pai).getSize(); i++) + { + printf("%d\n",(*pai).get(i)); + } + break; + case 2: + printf("出哪张\n"); + scanf("%d", &opt); + // l.firstInsert((*pai)[opt]); + (*pai).remove(opt); + printf("first6:%d",(*pai).first()->data); + printf("剩余%d \n",pai->getSize()); + printf("%d",pai->get(0)); + break; + case 3: + for (int i = 0; i pred = header; } +List::~List() +{ + clear(); + delete trailer; + delete header; +} + +void List::clear() +{ + while (0 < _size) + remove(0); +} + Node List::predInsert(Node Old, int New) { Node T = new node; @@ -93,14 +106,75 @@ Node List::lastInsertN(Node New) } Node List::find(int i) { - Node ans = first(); - for (int i = 0; i < _size; i++) + Node p = first(); + while (0 < i--) { - ans = ans->succ; + p = p->succ; } - return ans; + + return p; } int List::get(int i) { return find(i)->data; +} + +int List::remove(int i) +{ + + Node T = find(i); + int ans = T->data; + removeN(T); + return ans; +} + +void List::removeN(Node i) +{ + printf("first3:%d\n",first()->data); + printf("last%d\n",last()->data); + (i->pred)->succ = i->succ; + printf("first3.5:%d\n",first()->data); + printf("last%d\n",last()->data); + (i->succ)->pred = i->pred; + printf("first4:%d\n",first()->data); + printf("last%d\n",last()->data); + delete i; + printf("first5:%d\n",first()->data); + _size--; +} + +void List::sort() +{ + // int sorted_size=0; + // int unsorted_size=_size-1; + // for(int i=0;idata=T->data; + // T->data=max; + + // sorted_size++; + // unsorted_size--; + // } + // Node maxN; + // Node p=trailer->pred; + // int max; + // for(int r=0;r<_size;r++) + // { + // for(int j=0;j<=r;j++) + // { + // printf("j%d\n",j); + // Node T =find(j); + // if(max>T->data) + // { + // max=T->data; + // maxN=T; + // } + // } + // predInsert(find(),) + // } } \ No newline at end of file diff --git a/ex5/list.h b/ex5/list.h index 8b36283..513bb3c 100644 --- a/ex5/list.h +++ b/ex5/list.h @@ -17,10 +17,15 @@ private: int _size; protected: - int clear(); public: + int getSize() + { + return _size; + } List(); + ~List(); + void clear(); Node first() { return header->succ; } Node last() { return trailer->pred; } void firstInsert(int New); @@ -33,6 +38,10 @@ public: Node predInsertN(Node old, Node ne); int get(int i); Node find(int i); + int remove(int i); + void removeN(Node i); + + void sort(); int operator[](int i) { @@ -42,31 +51,3 @@ public: return p->data; } }; - -class PUKE -{ -private: - std::string PU[54] = { - "1A", "1B", "1C", "1D", - "2A", "2B", "2C", "2D", - "3A", "3B", "3C", "3D", - "4A", "4B", "4C", "4D", - "5A", "5B", "5C", "5D", - "6A", "6B", "6C", "6D", - "7A", "7B", "7C", "7D", - "8A", "8B", "8C", "8D", - "9A", "9B", "9C", "9D", - "10A", "10B", "10C", "10D", - "JA", "JB", "JC", "JD", - "QA", "QB", "QC", "QD", - "KA", "KB" - "KC", - "KD", - "BlackJoker", "RadJoker"}; - -public: - std::string p(int a) - { - return PU[a]; - } -}; diff --git a/ex5/vec.cpp b/ex5/vec.cpp index 4689d25..1826962 100644 --- a/ex5/vec.cpp +++ b/ex5/vec.cpp @@ -238,3 +238,9 @@ void Vec::merge(int lo, int mi, int hi) _v[i] = temp[k]; delete[] temp; } + +void Vec::reorder() +{ + for (int i = 0; i <= _used; i++) + swap(i, rand() % ((_used + 1))); +} \ No newline at end of file diff --git a/ex5/vec.h b/ex5/vec.h index a03dde2..04a25c8 100644 --- a/ex5/vec.h +++ b/ex5/vec.h @@ -21,7 +21,14 @@ private: void shrink(); public: - Vec(int _len,int mode); + ~Vec() + { + delete[] _v; + delete &_len; + delete &_used; + delete &_sorted; + } + Vec(int _len, int mode); int get(int a); @@ -51,6 +58,8 @@ public: void mergeSort(int lo, int hi); + void reorder(); + private: void merge(int lo, int mi, int hi); };