gpt4 book ai didi

c++ - 括号是否平衡!在 C++ 中

转载 作者:行者123 更新时间:2023-11-28 07:03:02 24 4
gpt4 key购买 nike

<分区>

我在检查括号字符串是否平衡的算法上遇到了一些问题。我必须从一个文本文件中获取输入并在另一个文本文件中显示输出。我遇到了这个问题算法。请帮我找出问题

#include <iostream>
#include <string.h>
#include <fstream>
#include "stack.h"
#include "stack.cpp"
using namespace std;
int main() {
StackType<char> c;
ifstream inFile("parentheses.txt");
ofstream outFile("report.txt");
int i, N;
char str[500];
inFile >> N;
inFile >> str;
while (str[i]) {
for (int i = 0; str[i] != '\0'; i++) {
if ((str[i] == '(') || (str[i] == '{') || (str[i] == '[')) {
c.Push(str[i]);
} else if ((str[i] == ')') || str[i] == '}' || str[i] == ']') {
if (c.isEmpty() == 1)
outFile << "Parentheses are not Balanced" << endl;
else if ((str[i] == ')' && str[i] == '(') ||
(str[i] == '}' && str[i] == '{') ||
(str[i] == ']' && str[i] == '[')) {
c.Pop();
} else
outFile << "Parentheses are not Balanced" << endl;
}
}
i++;
}
if (c.isEmpty() == 1)
outFile << "Parentheses are Balanced" << endl;
else
outFile << "Parentheses are not Balanced" << endl;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com