这些题我都做了,有的可以编译但是和要求的不 同,有的我做不出来,编译失败了,希望会写的友友可以指点一下
你先把自己做的代码贴上来吧
最后,特别感谢郝斌老师
#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;
}
#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;
}
#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;
}
#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;
}
```cpp
using namespace std;
// 广义表的结点 struct GListNode { bool isAtom; // 是否是原子 union { char atom; // 原子值 GListNode *sublist; // 子表 };