【发布时间】:2020-04-18 11:28:54
【问题描述】:
因此,作为一项任务,我必须从用户那里获取一个字符串并将其转换为一个列表,然后检查括号平衡。但是,我不断收到`警告:控制到达非无效函数的结尾 [-Wreturn-type]
#include<iostream>
#include<string>
#include<stacks>
class Node{
public:
char data;
Node *head;
Node *tail;
Node *next;
Node *node;
void addToList(char);
bool CheckList();
void StringToList(string, Node*);
};
Node* addToList(char data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
};
Node* StringToList(string text, Node* head)
{
head = addToList(text[0]);
Node *CurrentNode = head;
for (int i = 1; i <text.size(); i++){
CurrentNode->next = addToList(text[i]);
CurrentNode = CurrentNode->next;
}
return head;
}
bool CheckList(Node* head)
{
char c;
stack <char> p;
int i = 0;
Node* CurrentNode = head;
while(CurrentNode != NULL){
if('(' || '{' || '['== CurrentNode->data){
p.push(CurrentNode->data);
if(')' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '{' || c == '['){
return false;
}
}
else if('}' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '(' || c == '['){
return false;
}
}
else if('}' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '(' || c == '['){
return false;
}
}
}
}
CurrentNode = CurrentNode->next;
}
int main()
{
string text = "(check)[";
Node *head = NULL;
head = StringToList(text, head);
if(CheckList(head)){
cout<<"MAMA MIA IT WORKED-A!";
}
else
cout<<"IT'S-A STILL WORKING!!!";
return 0;
}
任何帮助将不胜感激。再次感谢您的宝贵时间。另外,很抱歉,如果我的代码看起来有点乱,但我对堆栈和列表有点陌生。
【问题讨论】:
-
警告非常适合指出问题代码需要修复的行号。始终在 gcc/clang 上使用
-Wall -Wextra -pedantic最低值进行编译,或者在 VS (cl.exe) 中使用/W3,并且在编译时没有警告之前不要接受代码。还可以考虑添加-Wshadow来识别和隐藏可能有问题的变量。你可以从你的编译器中学到很多东西(虽然 STL 的警告有点多……)