我正在尝试实现一个堆栈来检查文件是否具有平衡 ()、[] 和 {}。该程序应该接收一个文件并检查它是否平衡并返回一个 bool 值。当我运行该程序时,它仅适用于文件中的最后一个括号。我如何更改代码以使其适用于最后一对之前的括号。输入文件只是一个简单的 c 文件。
附带问题:
如果我想让这个程序与 html 文件一起工作,我只需要用 html 标签更改 ()、[]、{} 吗?
这是我的代码
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
using namespace std;
bool balanced(string A[], int n)
{
int i;
stack <string> a;
for (i = 0; i < n; i++) {
if (A[i] == "(" || A[i] == "{" || A[i] == "["){
a.push(A[i]);
} else
if (A[i] == ")" || A[i] == "}" || A[i] == "]") {
if (a.empty()) {
return false;
} else {
a.pop();
return true;
}
}
}
}
int main()
{
ifstream infile;
infile.open ("Text.txt");
string A[1000];
int i = 0;
int n = (sizeof(A) / sizeof(*A));
while (!infile.eof()) {
getline(infile, A[i], '\n');
i++;
}
bool out;
out = balanced(A, n);
if (out == true) {
cout << "legal";
} else {
cout << "illegal";
}
return 0;
}
最佳答案
即使您按照@bipll 的建议修复错误,您的解决方案也仅适用于一种括号。 This是将解决您的问题的解决方案的完整说明。这是 C++ 代码:
#include<bits/stdc++.h>
using namespace std;
// function to check if paranthesis are balanced
bool areParanthesisBalanced(char expr[])
{
stack<char> s;
char a, b, c;
// Traversing the Expression
for (int i=0; i<strlen(expr); i++)
{
if (expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
// Push the element in the stack
s.push(expr[i]);
}
else
{
switch (expr[i])
{
case ')':
// Store the top element in a
a = s.top();
s.pop();
if (a=='{'||a=='[')
cout<<"Not Balancedn";
break;
case '}':
// Store the top element in b
b = s.top();
s.pop();
if (b=='('||b=='[')
cout<<"Not Balancedn";
break;
case ']':
// Store the top element in c
c=s.top();
s.pop();
if (c=='('||c=='{')
cout<<"Not Balancedn";
break;
}
}
}
// Check Empty Stack
if (s.empty())
return true;
else
return false;
}
// Driver program to test above function
int main()
{
char expr[]="{()}[]";
if(areParanthesisBalanced(expr))
cout<<"Balanced";
else
cout<<"Not Balanced";
return 0;
}
关于c++ - 检查 C++ 括号的堆栈实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49641410/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes