2301_78364727 2023-10-31 01:04 采纳率: 90.9%
浏览 41
已结题

C++数据结构与算法

基于这段代码,按照如图所示的要求,修改代码


#include<iostream>
#include<string>
#include<time.h>
using namespace std;
typedef int Rank;
#define ListNodePosi(T) ListNode<T>*//列表节点位置
template<typename T>struct ListNode//列表节点模板类
{
​T data;
​ListNode<T>* pred;
​ListNode<T>* succ;
​ListNode() { pred = NULL; succ = NULL; }//构造
​ListNode(T e, ListNode<T>* p = NULL, ListNode<T>* s = NULL)
​​:data(e), pred(p), succ(s) {}//默认构造器
​ListNodePosi(T) insertAsPred(T const& e);//当前节点之前插入新节点
​ListNodePosi(T) insertAsSucc(T const& e);//当前节点之后插入新节点
};
 
template<typename T>class List//列表模板类
{
public:
​int _size;
​ListNode<T>* header;
​ListNode<T>* trailer;//规模,头尾哨兵void init();//列表创建时的初始化List() { init(); }//默认void insertA(ListNode<T>* p, T e);//将e当做节点p的直接后继,前驱插入void remove(ListNode<T>* p);//删除P处节点,返回其数值void clear();//清除所有节点
};
 
template<typename T>void List<T>::init()//列表初始化,在创建列表对象时统一调用
{
​header = new ListNode<T>;//创建头哨兵节点
​trailer = new ListNode<T>;//尾哨兵节点
​header->succ = trailer;
​header->pred = NULL;
​trailer->succ = NULL;
​trailer->pred = header;
​_size = 0;//记录规模
}
 
template<typename T>void List<T>::insertA(ListNode<T>* p, T e)//插入
{
​ListNode<T>* x = new ListNode<T>();//创建新节点
​x->data = e;
​x->pred = p;
​x->succ = p->succ;
​p->succ->pred = x;
​p->succ = x;
​_size++;
}
 
template<typename T>
void List<T>::remove(ListNode<T>* p)//删除节点P,返回其数值
{
​T e = p->data;//备份待删除节点的数值
​p->pred->succ = p->succ;
​p->succ->pred = p->pred;//后继,前驱
​delete p;//释放节点
​_size--;//更新规模
}
 
template<typename T>
void List<T>::clear()//清空列表
{
​header->succ = trailer;
​trailer->pred = header;
​_size = 0;
}
void distribute(List<string> L)
{
​string num[] = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" };
​string type[] = { "黑桃","红桃","梅花","方块" };
​ListNode<string>* p = L.header;
​for (int i = 0; i < 13; i++)
​{
​​for (int j = 0; j < 4; j++)
​​{
​​​string e = type[j] + num[i];
​​​L.insertA(p, e);
​​​p = p->succ;
​​}
​}
​L.insertA(p, "small joker");
​p = p->succ;
​L.insertA(p, "big joker");
}
void  display(List<string> L)
{
​ListNode<string>* n = L.header->succ;
​while (n->succ != NULL)
​{
​​cout << n->data << " ";
​​n = n->succ;
​}
​cout << endl;
}
void shuffle(List<string>L)
{
​int n = 0;
​for (int i = 1; i < 55; i++)
​{
​​srand(n);
​​int j = (rand() % 54) + 1;
​​while (i == j)
​​{
​​​j = (rand() % 54) + 1;
​​}
​​ListNode<string>* p = NULL, * q = NULL;
​​ListNode<string>* m = L.header->succ;
​​for (int count = 1; count < 55; count++)
​​{
​​​if (count == i)
​​​{
​​​​p = m;
​​​}
​​​else if (count == j)
​​​{
​​​​q = m;
​​​}
​​​if (p != NULL && q != NULL)break;
​​​m = m->succ;
​​}
​​L.insertA(p, q->data);
​​L.insertA(q, p->data);
​​L.remove(p);
​​L.remove(q);
​​n++;
​}
}
int main()
{
​List<string> list;
​distribute(list);
​cout << "原始扑克牌列表为:" << endl;
​display(list);
​cout << "洗牌请输入1,展示请输入2,发牌请输入3" << endl;
​int m;
​cin >> m;
​while (m == 1 || m == 2)
​{
​​if (m == 1)
​​{
​​​shuffle(list);
​​​cout << "已完成洗牌!" << endl;
​​}
​​else if (m == 2)
​​{
​​​cout << "当前扑克牌列表为:" << endl;
​​​display(list);
​​}
​​cout << "洗牌请输入1,展示请输入2,发牌请输入3" << endl;
​​cin >> m;
​}
​List<string> player1, player2, player3;
​ListNode<string>* p = list.header->succ;
​ListNode<string>* q1 = player1.header;
​ListNode<string>* q2 = player2.header;
​ListNode<string>* q3 = player3.header;
​while (p->succ != NULL)
​{
​​player1.insertA(q1, p->data);
​​q1 = q1->succ;
​​p = p->succ;
​​player2.insertA(q2, p->data);
​​q2 = q2->succ;
​​p = p->succ;
​​player3.insertA(q3, p->data);
​​q3 = q3->succ;
​​p = p->succ;
​}
​list.clear();
​cout << "玩家1的牌为:" << endl;
​display(player1);
​cout << "玩家2的牌为:" << endl;
​display(player2);
​cout << "玩家3的牌为:" << endl;
​display(player3);
 
​while (1) {
​​cout << "请选择一位玩家(1/2/3):";
​​int b1 = 1;
​​int b2 = 1;
​​int b3 = 1;
​​int name;
​​cin >> name;
​​switch (name)
​​{
​​case 1:
​​​cout << "玩家1的牌为:" << endl;
​​​display(player1);
 
​​​while (b1 == 1)
​​​{
​​​​int t = 0;
​​​​while (true) {
​​​​​cout << "请选择要出的牌:";
​​​​​string e1;
​​​​​cin >> e1;
​​​​​ListNode<string>* n1 = player1.header->succ;//获取第一张扑克牌
​​​​​while (n1->succ != NULL)
​​​​​{
​​​​​​if (n1->data == e1)
​​​​​​{
​​​​​​​player1.remove(n1);
​​​​​​​list.insertA(list.header, e1);//插入扑克牌列表
​​​​​​​t = 1;
​​​​​​​break;
​​​​​​}
​​​​​​n1 = n1->succ;
​​​​​}
​​​​​if (t == 0) {
​​​​​​cout << "出牌错误,请重新输入" << endl;
​​​​​}
​​​​​else {
​​​​​​break;
​​​​​}
​​​​}
​​​​cout << "继续出牌请输入1,输入其他则退出" << endl;
​​​​cin >> b1;
​​​}
​​​cout << "玩家1当前的牌为:" << endl;
​​​display(player1);
​​​cout << "弃牌堆:" << endl;
​​​display(list);
​​​break;
​​case 2:
​​​cout << "玩家2的牌为:" << endl;
​​​display(player2);
 
​​​while (b2 == 1)
​​​{
​​​​int t1 = 0;
​​​​while (true) {
​​​​​cout << "请选择要出的牌:";
​​​​​string e2;
​​​​​cin >> e2;
​​​​​ListNode<string>* n2 = player2.header->succ;
​​​​​while (n2->succ != NULL)
​​​​​{
​​​​​​if (n2->data == e2)
​​​​​​{
​​​​​​​player2.remove(n2);
​​​​​​​list.insertA(list.header, e2);
​​​​​​​t1 = 1;
​​​​​​​break;
​​​​​​}
​​​​​​n2 = n2->succ;
​​​​​}
​​​​​if (t1 == 0) {
​​​​​​cout << "出牌错误,请重新输入" << endl;
​​​​​}
​​​​​else {
​​​​​​break;
​​​​​}
​​​​}
​​​​cout << "继续出牌请输入1,输入其他则退出" << endl;
​​​​cin >> b2;
​​​}
​​​cout << "玩家2当前的牌为:" << endl;
​​​display(player2);
​​​cout << "弃牌堆:" << endl;
​​​display(list);
​​​break;
 
​​case 3:
​​​cout << "玩家3的牌为:" << endl;
​​​display(player3);
 
​​​while (b3 == 1)
​​​{
​​​​int t2 = 0;
​​​​while (true) {
​​​​​cout << "请选择要出的牌:";
​​​​​string e3;
​​​​​cin >> e3;
​​​​​ListNode<string>* n3 = player3.header->succ;
​​​​​while (n3->succ != NULL)
​​​​​{
​​​​​​if (n3->data == e3)
​​​​​​{
​​​​​​​player3.remove(n3);
​​​​​​​list.insertA(list.header, e3);
​​​​​​​t2 = 1;
​​​​​​​break;
​​​​​​}
​​​​​​n3 = n3->succ;
​​​​​}
​​​​​if (t2 == 0) {
​​​​​​cout << "出牌错误,请重新输入要输的牌" << endl;
​​​​​}
​​​​​else {
​​​​​​break;
​​​​​}
​​​​}
​​​​cout << "继续出牌请输入1,输入其他则退出" << endl;
​​​​cin >> b3;
​​​}
​​​cout << "玩家3当前的牌为:" << endl;
​​​display(player3);
​​​cout << "弃牌堆:" << endl;
​​​display(list);
​​​break;
 
​​}
​}
 
​system("pause");
 
​return 0;
}

img

  • 写回答

12条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-31 08:32
    关注

    过牌、排名机制等优化中(调试中~ 可先行参考如下代码):
    (已经调整差不多了(还没发上来,没发上来的原因是避免被人抄袭),暂时也没太多时间做到十全十美,还需要时间调试。因为你一直没有回复,我不能确定你是否有关注自己的问题,因此暂时将你的问题放下了,如还有相关需求,请评论或私信联系)

    
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    string num[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    string type[] = {"黑桃", "红桃", "梅花", "方块"};
    
    void display(const vector<string>& cards) {
        for (const string& card : cards) {
            cout << card << " ";
        }
        cout << endl;
    }
    
    void shuffle(vector<string>& cards) {
        random_shuffle(cards.begin(), cards.end());
    }
    
    int main() {
        vector<string> list;
        vector<string> player1;
        vector<string> player2;
        vector<string> player3;
        
        for (const string& t : type) {
            for (const string& n : num) {
                list.push_back(t + n);
            }
        }
        list.push_back("小王");
        list.push_back("大王");
        
        cout << "原始扑克牌列表为:" << endl;
        display(list);
        
        int m;
        cout << "洗牌请输入1,展示请输入2,发牌请输入3" << endl;
        cin >> m;
        
        while (true) {
            if (m == 1) {
                shuffle(list);
                cout << "已完成洗牌!" << endl;
            } else if (m == 2) {
                cout << "当前扑克牌列表为:" << endl;
                display(list);
            } else if (m == 3) {
                break;
            } else {
                cout << "无效的输入,请重新输入" << endl;
            }
            
            cout << "洗牌请输入1,展示请输入2,发牌请输入3" << endl;
            cin >> m;
        }
        
        player1.assign(list.begin(), list.begin() + 17);
        player2.assign(list.begin() + 17, list.begin() + 34);
        player3.assign(list.begin() + 34, list.end());
        
        list.clear();
        
        int round = 1;
        while (true) {
            cout << "第" << round << "轮出牌:" << endl;
            cout << "玩家1的牌为:" << endl;
            display(player1);
            cout << "玩家2的牌为:" << endl;
            display(player2);
            cout << "玩家3的牌为:" << endl;
            display(player3);
            
            cout << "请选择一位玩家(1/2/3):";
            int name;
            cin >> name;
            
            switch (name) {
                case 1: {
                    cout << "玩家1的牌为:" << endl;
                    display(player1);
                    
                    int index;
                    while (true) {
                        cout << "请选择要出的牌的下标:";
                        cin >> index;
                        
                        if (index >= 0 && index < player1.size()) {
                            break;
                        } else {
                            cout << "无效的下标,请重新输入" << endl;
                        }
                    }
                    
                    string card = player1[index];
                    player1.erase(player1.begin() + index);
                    list.push_back(card);
                    cout << "玩家1出牌:" << card << endl;
                    break;
                }
                case 2: {
                    cout << "玩家2的牌为:" << endl;
                    display(player2);
                    
                    string minCard = list.front();
                    for (const string& card : player2) {
                        if (card > minCard) {
                            list.push_back(card);
                            player2.erase(find(player2.begin(), player2.end(), card));
                            cout << "玩家2出牌:" << card << endl;
                            break;
                        }
                    }
                    
                    if (list.back() == minCard) {
                        list.pop_back();
                        cout << "玩家2选择了过" << endl;
                    }
                    break;
                }
                case 3: {
                    cout << "玩家3的牌为:" << endl;
                    display(player3);
                    
                    string minCard = list.front();
                    for (const string& card : player3) {
                        if (card > minCard) {
                            list.push_back(card);
                            player3.erase(find(player3.begin(), player3.end(), card));
                            cout << "玩家3出牌:" << card << endl;
                            break;
                        }
                    }
                    
                    if (list.back() == minCard) {
                        list.pop_back();
                        cout << "玩家3选择了过" << endl;
                    }
                    break;
                }
                default:
                    cout << "无效的玩家选择" << endl;
                    break;
            }
            
            if (player1.empty() || player2.empty() || player3.empty()) {
                break;
            }
            
            round++;
        }
        
        cout << "游戏结束!" << endl;
        int numPlayers = 0;
        if (player1.empty()) {
            cout << "玩家1出完牌,获得第1名" << endl;
            numPlayers++;
        }
        if (player2.empty()) {
            cout << "玩家2出完牌,获得第" << numPlayers + 1 << "名" << endl;
            numPlayers++;
        }
        if (player3.empty()) {
            cout << "玩家3出完牌,获得第" << numPlayers + 1 << "名" << endl;
            numPlayers++;
        }
        
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(11条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 10月31日

悬赏问题

  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 为什么在iis上部署网站,服务器可以访问,但是本地电脑访问不了
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
  • ¥15 ADS时域 连续相位观察方法
  • ¥15 Opencv配置出错
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像