C++ C++;“程序返回”;“有效”;结果不考虑用户输入

C++ C++;“程序返回”;“有效”;结果不考虑用户输入,c++,C++,该程序用于检查括号是否平衡(打开和关闭)。程序运行,但返回所有用户输入的valid #ifndef _STACKLSAB_H_ #define _STACKSLAB_H_ #include <stack> #include <string> #include <vector> #include "namevaluepair.h" using namespace std; class Stacks { public: boo

该程序用于检查括号是否平衡(打开和关闭)。程序运行,但返回所有用户输入的
valid

#ifndef _STACKLSAB_H_
#define _STACKSLAB_H_

#include <stack>
#include <string>
#include <vector>
#include "namevaluepair.h"

using namespace std;

class Stacks
{
  public:
    bool balanced(string exp);
    bool match(char open, char closed);
           
  private:
    string st_PostData;
};

#endif
\ifndef\u STACKLSAB\u H_
#定义_STACKSLAB_H_
#包括
#包括
#包括
#包括“namevaluepair.h”
使用名称空间std;
类堆栈
{
公众:
布尔平衡(字符串扩展);
布尔匹配(字符打开,字符关闭);
私人:
字符串st_PostData;
};
#恩迪夫
stacksLab.cpp(我的程序在repl.it中工作,但不与cgi一起工作):

#包括
#包括
#包括“stacksLab.h”
#包括
使用名称空间std;
布尔堆栈::匹配(字符打开,字符关闭)
{
if(open=='('&&closed==')')返回true;
else if(open=='{'&&closed=='}')返回true;
else if(open=='['&&closed==']')返回true;
返回false;
}
布尔堆栈::平衡(字符串扩展)
{
堆栈S;
对于(int i=0;i表达式;
if(标准平衡(表达式))

cout问题不在于堆栈处理(我给了您的代码一些基本测试,它似乎可以工作),而在于您接受输入的方式


cin与问题无关,但您应该阅读以下内容:调试,第一步:回显输入,以验证它是否是您认为的输入。哪些输入给您的是“有效”而不是“无效”?技术上也包括以下划线开头,后跟大写字母的标识符"例如
#ifndef\u STACKLSAB\u H
是为实现保留的。此处不太可能导致问题,但通常情况下,发生潜在冲突不是一个好主意。您在回答中包含但未提供的
namevaluepair.H
中包含了什么内容?如果不相关,则应将问题缩小到最小值如果输入看起来是html,他可能还想将整个流读入
表达式
,但我不知道一个简单的方法that@MooingDuck如果是这样的话(我将等待OP的澄清)然后我们可以将
expression
设置为空,在
while
循环中,继续读取另一个字符串并将其连接到
expression
,直到流耗尽。
#include <iostream>
#include <sstream>
#include "stacksLab.h"
#include <stack>

using namespace std;

bool Stacks::match(char open,char closed)
{
    if(open == '(' && closed == ')') return true;
    else if(open == '{' && closed == '}') return true;
    else if(open == '[' && closed == ']') return true;
    return false;
}
bool Stacks::balanced(string exp)
{
    stack<char>  S;

    for(int i = 0;i<exp.length();i++)
    {
        if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
            S.push(exp[i]);
        else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
        {
            if(S.empty() || !match(S.top(),exp[i]))
                return false;
            else
                S.pop();
        }
    }
    return S.empty();
}
#include <iostream>
#include "stacksLab.h"

#include <stack>

using namespace std;

int main()
{
  /// complete the http header
  cout << "Content-type: text/html\n\n";

  Stacks st;

  string expression;
  cin>>expression;

   if(st.balanced(expression))
        cout<<"Valid\n";
    else
        cout<<"Not Valid\n";

}
<form action="runme.cgi" method="POST">

<table align="center" bgcolor="antiquewhite">

<tr>
   <td align="right">Enter parentheses here:</td>
   <td><input type="text" name="parentheses "/> </td>
</tr>

<tr>
    <td align="right"><input type="submit" /></td>
</tr>

</table>
</form>