使用指针查找不平衡括号之间字符的位置
Using pointers to find positions of characters between unbalances parentheses
我有一个 C++ 编程问题:在一个字符串中我需要找到是否有平衡括号。如果没有,我应该使用指针找到未闭合括号之间的字符位置(在第二个开口和最近的闭合之间)。
我知道问题陈述有点混乱。我认为它应该以某种方式工作:
输入#1:
((aba)aaab)
输出:
OK.
输入#2:
(aa(a)ab
输出:
Parentheses not balanced: between characters 1 and 6.
下面的代码解决了闭括号检查的部分问题,还有一个结构来保存开括号的地址。我不确定如何为此目的使用指针,一些尝试没有给出任何结果,所以我在这里需要一些帮助。
#include<iostream>
#include<string>
#include<stack>
using namespace std;
struct br_data{
char br_t;
char *cptr; //store the address of the opening parenthesis
};
int main (){
string input;
int addr;
br_data br;
getline(cin, input);
stack<br_data> braces;
char *a = input[0];
auto init_char = static_cast<void*>(&a); //store the address of the first character in the input string
cout << static_cast<void*>(&a) << endl; //gives the address in memory
for(auto c: input) {
if (c == '(') {
br.br_t = c;
br.cptr = &c; //storing the address of the first parenhesis
braces.push(br);
} else if (c == ')' ) {
if (braces.empty())
cout << "This line does not contain unclosed parentheses\n";
if (!braces.empty())
braces.pop();
}
}
if (!braces.empty()){
//int addr = br.cptr;
cout << "This line does not contain unclosed parentheses\n";
//int pos = (&br.cptr) - (&a); //how to calculate the position??
cout << "Position of the second opening parenthis is " << () << endl;
//cout << "Position of the nearest closing parenthis is " << -how?? (static_cast<void*>(&br.cptr)) << endl;
}
if (braces.empty()){
cout << "Parentheses are balanced in this line\n";
}
return 0;
}
写的时候
br.cptr = &c; //storing the address of the first parenhesis
你实际上存储的是之前声明的 char 类型的本地对象的地址:
auto c: input
在您退出循环的那一刻,它就正式悬空了。
一个最简单的解决方案是实际考虑字符串的字符,而不是它们的本地副本:
for(auto &c: input) {
(更好的是,将 auto 更改为 char 以保持源长度不变,从而更清晰)。然后您可以继续查看您的解决方案需要如何进一步修复。
(一些额外的免费建议:input[0] 是一个 char 类型的右值引用,因此将它分配给类型 char *
的变量是没有意义的,以及您尝试在其中执行的操作行实际上写成 char *a = input.c_str();
或 input.data()
甚至 &input[0]
,选择最佳选项;并且 br.cptr 已经是 pointer-to-char 类型,因此字符在字符串将计算为 br.cptr - a
,您需要减去指针本身,而不是它们的地址。)
#include <iostream>
using namespace std;
int main(){
char str[]="Hello Programming";
char *ptr;
char ch;
char s;
s='n';
ptr=str;
cout<<"To be found Character"<<endl;
cin>>ch;
while(*ptr++ != '[=10=]')
if(*ptr==ch)
s='y';
if (s=='y')
cout<<"FOUND";
else
cout<<"not found";``
return 0;
}
我有一个 C++ 编程问题:在一个字符串中我需要找到是否有平衡括号。如果没有,我应该使用指针找到未闭合括号之间的字符位置(在第二个开口和最近的闭合之间)。 我知道问题陈述有点混乱。我认为它应该以某种方式工作:
输入#1:
((aba)aaab)
输出:
OK.
输入#2:
(aa(a)ab
输出:
Parentheses not balanced: between characters 1 and 6.
下面的代码解决了闭括号检查的部分问题,还有一个结构来保存开括号的地址。我不确定如何为此目的使用指针,一些尝试没有给出任何结果,所以我在这里需要一些帮助。
#include<iostream>
#include<string>
#include<stack>
using namespace std;
struct br_data{
char br_t;
char *cptr; //store the address of the opening parenthesis
};
int main (){
string input;
int addr;
br_data br;
getline(cin, input);
stack<br_data> braces;
char *a = input[0];
auto init_char = static_cast<void*>(&a); //store the address of the first character in the input string
cout << static_cast<void*>(&a) << endl; //gives the address in memory
for(auto c: input) {
if (c == '(') {
br.br_t = c;
br.cptr = &c; //storing the address of the first parenhesis
braces.push(br);
} else if (c == ')' ) {
if (braces.empty())
cout << "This line does not contain unclosed parentheses\n";
if (!braces.empty())
braces.pop();
}
}
if (!braces.empty()){
//int addr = br.cptr;
cout << "This line does not contain unclosed parentheses\n";
//int pos = (&br.cptr) - (&a); //how to calculate the position??
cout << "Position of the second opening parenthis is " << () << endl;
//cout << "Position of the nearest closing parenthis is " << -how?? (static_cast<void*>(&br.cptr)) << endl;
}
if (braces.empty()){
cout << "Parentheses are balanced in this line\n";
}
return 0;
}
写的时候
br.cptr = &c; //storing the address of the first parenhesis
你实际上存储的是之前声明的 char 类型的本地对象的地址:
auto c: input
在您退出循环的那一刻,它就正式悬空了。
一个最简单的解决方案是实际考虑字符串的字符,而不是它们的本地副本:
for(auto &c: input) {
(更好的是,将 auto 更改为 char 以保持源长度不变,从而更清晰)。然后您可以继续查看您的解决方案需要如何进一步修复。
(一些额外的免费建议:input[0] 是一个 char 类型的右值引用,因此将它分配给类型 char *
的变量是没有意义的,以及您尝试在其中执行的操作行实际上写成 char *a = input.c_str();
或 input.data()
甚至 &input[0]
,选择最佳选项;并且 br.cptr 已经是 pointer-to-char 类型,因此字符在字符串将计算为 br.cptr - a
,您需要减去指针本身,而不是它们的地址。)
#include <iostream>
using namespace std;
int main(){
char str[]="Hello Programming";
char *ptr;
char ch;
char s;
s='n';
ptr=str;
cout<<"To be found Character"<<endl;
cin>>ch;
while(*ptr++ != '[=10=]')
if(*ptr==ch)
s='y';
if (s=='y')
cout<<"FOUND";
else
cout<<"not found";``
return 0;
}