有五个选择结构的题,友友们帮我🥺🥺

这些题我都做了,有的可以编译但是和要求的不 同,有的我做不出来,编译失败了,希望会写的友友可以指点一下

img

img

img

img

img

你先把自己做的代码贴上来吧

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7664761
  • 你也可以参考下这篇文章:求两组整数的交集 【问题描述】 读入两组整数(每组不超过20个整数,并且同一组中的整数各不相同),编程求两组整数的交集,即在两组整数中都出现的整数,并按从大到小的顺序输出。若交集为空,则什么都不输
  • 除此之外, 这篇博客: 指针是什么?指针怎么理解?怎么理解指针?最简单最通俗的讲解,即使不理解,记住之后也会理解代码,非常有效,希望对你有帮助!中的 其实指针就那么几句话,我写了好几遍,第一次总结可能不够精简。希望大家理解。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 最后,特别感谢郝斌老师

  • 以下回答由chatgpt基于相关博客总结生成:
    1. 根据广义表字符串(((a,b,c),(d)),e)分解表头字符串、表尾字符串
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    string getHeadString(string str) {
        if (str == "") { // 如果字符串为空,则返回空字符串
            return "";
        }
        int len = str.length();
        if (str[0] == '(') { // 如果字符串的首字符是左括号,则分解表头字符串
            int count = 1;
            for (int i = 1; i < len; i++) { // 从第二个字符开始遍历
                if (str[i] == '(') { // 左括号数量加一
                    count++;
                } else if (str[i] == ')') { // 右括号数量减一
                    count--;
                }
                if (count == 0) { // 遇到第一个平衡的右括号,则表头字符串结束
                    return str.substr(0, i + 1);
                }
            }
        }
        return "";
    }
    
    string getTailString(string str) {
        if (str == "") { // 如果字符串为空,则返回空字符串
            return "";
        }
        int len = str.length();
        if (str[0] == '(') { // 如果字符串的首字符是左括号,则分解表尾字符串
            int count = 1;
            for (int i = 1; i < len; i++) { // 从第二个字符开始遍历
                if (str[i] == '(') { // 左括号数量加一
                    count++;
                } else if (str[i] == ')') { // 右括号数量减一
                    count--;
                }
                if (count == 0) { // 遇到第一个平衡的右括号,则表尾字符串开始
                    return str.substr(i + 1);
                }
            }
        }
        return "";
    }
    
    int main() {
        string str = "(((a,b,c),(d)),e)";
        string head = getHeadString(str);
        string tail = getTailString(str);
        cout << head << endl; // 输出 (((a,b,c),(d)))
        cout << tail << endl; // 输出 e)
        return 0;
    }
    
    1. 根据分解好的字符串创建广义表
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct GListNode { // 广义表的结点
        bool isAtom; // 是否是原子
        union {
            char atom; // 原子值
            GListNode *sublist; // 子表
        };
        GListNode(bool valIsAtom, char valAtom, GListNode *valSublist) { // 结点构造函数
            isAtom = valIsAtom;
            if (isAtom) {
                atom = valAtom;
                sublist = NULL;
            } else {
                atom = '\0';
                sublist = valSublist;
            }
        }
    };
    
    GListNode *createGList(string str) {
        if (str == "") { // 如果字符串为空,则返回空指针
            return NULL;
        }
        int len = str.length();
        if (str[0] == '(') { // 如果字符串的首字符是左括号,则创建子表
            int count = 1;
            for (int i = 1; i < len; i++) { // 从第二个字符开始遍历
                if (str[i] == '(') { // 左括号数量加一
                    count++;
                } else if (str[i] == ')') { // 右括号数量减一
                    count--;
                }
                if (count == 0) { // 遇到第一个平衡的右括号,则找到了全部的子表
                    GListNode *sublist = createGList(str.substr(1, i - 1)); // 递归创建子表
                    GListNode *next = createGList(str.substr(i + 1)); // 递归创建下一个结点
                    return new GListNode(false, '\0', sublist); // 创建当前结点为子表结点,原子为空,子表为递归创建的子表
                }
            }
        } else { // 如果字符串的首字符不是左括号,则创建原子结点
            char atom = str[0];
            GListNode *next = createGList(str.substr(1));
            return new GListNode(true, atom, NULL); // 创建当前结点为原子结点,原子为字符串的首字符,子表为空
        }
        return NULL; // 如果程序能走到这里,说明创建失败了,返回空指针
    }
    
    int main() {
        string str = "(((a,b,c),(d)),e)";
        GListNode *gList = createGList(str);
        return 0;
    }
    
    1. 求深度
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    // 广义表的结点
    struct GListNode {
        bool isAtom; // 是否是原子
        union {
            char atom; // 原子值
            GListNode *sublist; // 子表
        };
        GListNode(bool valIsAtom, char valAtom, GListNode *valSublist) { // 结点构造函数
            isAtom = valIsAtom;
            if (isAtom) {
                atom = valAtom;
                sublist = NULL;
            } else {
                atom = '\0';
                sublist = valSublist;
            }
        }
    };
    
    // 求深度
    int getDepth(GListNode *gList) {
        if (!gList) { // 如果当前结点为空,则深度为0
            return 0;
        }
        if (gList->isAtom) { // 如果当前结点是原子,则深度为0
            return 0;
        }
        int maxSublistDepth = 0;
        GListNode *sublist = gList->sublist;
        while (sublist) {
            int sublistDepth = getDepth(sublist);
            maxSublistDepth = max(maxSublistDepth, sublistDepth);
            sublist = sublist->sublist;
        }
        return maxSublistDepth + 1;
    }
    
    int main() {
        // 原广义表为 (((a,b,c),(d)),e)
        GListNode *gList = new GListNode(false, '\0',
                new GListNode(false, '\0',
                    new GListNode(true, 'a', NULL),
                    new GListNode(false, '\0',
                        new GListNode(true, 'b', NULL),
                        new GListNode(true, 'c', NULL),
                        NULL),
                    NULL),
                new GListNode(false, '\0',
                    new GListNode(true, 'd', NULL),
                    NULL),
                new GListNode(true, 'e', NULL),
                NULL);
        cout << getDepth(gList) << endl; // 输出 3
        return 0;
    }
    
    1. 求长度
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    // 广义表的结点
    struct GListNode {
        bool isAtom; // 是否是原子
        union {
            char atom; // 原子值
            GListNode *sublist; // 子表
        };
        GListNode(bool valIsAtom, char valAtom, GListNode *valSublist) { // 结点构造函数
            isAtom = valIsAtom;
            if (isAtom) {
                atom = valAtom;
                sublist = NULL;
            } else {
                atom = '\0';
                sublist = valSublist;
            }
        }
    };
    
    // 求长度
    int getLength(GListNode *gList) {
        if (!gList) { // 如果当前结点为空,则长度为0
            return 0;
        }
        if (gList->isAtom) { // 如果当前结点是原子,则长度为1
            return 1;
        }
        int len = 0;
        GListNode *sublist = gList->sublist;
        while (sublist) {
            len += getLength(sublist);
            sublist = sublist->sublist;
        }
        return len;
    }
    
    int main() {
        // 原广义表为 (((a,b,c),(d)),e)
        GListNode *gList = new GListNode(false, '\0',
                new GListNode(false, '\0',
                    new GListNode(true, 'a', NULL),
                    new GListNode(false, '\0',
                        new GListNode(true, 'b', NULL),
                        new GListNode(true, 'c', NULL),
                        NULL),
                    NULL),
                new GListNode(false, '\0',
                    new GListNode(true, 'd', NULL),
                    NULL),
                new GListNode(true, 'e', NULL),
                NULL);
        cout << getLength(gList) << endl; // 输出 5
        return 0;
    }
    
    1. 查找原子个数

    ```cpp

    include

    include

    include

    using namespace std;

    // 广义表的结点 struct GListNode { bool isAtom; // 是否是原子 union { char atom; // 原子值 GListNode *sublist; // 子表 };