从输入中删除括号 [英] removing brackets from input

查看:89
本文介绍了从输入中删除括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要处理:


我们会要求用户输入任何内容,我们会将其重新放入

标准输出中删除了一组括号。我们不会删除任何

单个支架,例如


INPUT:comp.lang。(c ++)

OUTPUT:comp.lang 。


INPUT:comp.lang。(c ++

OUTPUT:comp.lang。(c ++


INPUT :Bjarn)e st)roustr(上)

输出:Bjarn)e st)roustr


我们不想删除任何单个开始或结束括号。我们只有b / b
只对一组括号感兴趣。

我的想法:


我无法决定使用哪个容器。我想使用

一个Veoctr,如果我确定我不会给b $ b找到任何一个结束括号,那么只能将输入推到它上面。但是我怎么知道我在括号内的
或者我只是在读一个支架。我阅读了有关Stacks的内容,并且想出了这段代码,但是在一个左括号后,它删除了每个

的parethesis和元素,

和因为它是一个堆栈,它会反转所有输入:


#include< iostream>

#include< stack>

#include< string>

int main()

{

std :: stack< charstrStack;

bool P_ON = false;

char in_char;

while(std :: cin> in_char)

{

if(in_char ==''('')

{

P_ON = true;

}


if(in_char =='')'')

{

P_ON = false;

}


if(P_ON == false&& in_char!='')'')

{

strStack.push(in_char );

}

else if(P_ON == false&& in_char =='')'')

{

strStack.push('''');

strStac k.push(''*'');

strStack.push('''');

/ * a''*''空间儿子双方代表一套括号

删除* /

}

}


/ *打印删除了所需元素的堆栈* /

std :: cout<< " \\\
------------在删除元素后堆叠--------------- \ n" ;;

while (strStack.empty()== false)

{

std :: cout<< strStack.top();

strStack.pop();

}


std :: cout<< std :: endl;

返回0;

}


=========== OUTPUT = ===========

/ home / arnuld / programming / C ++ $ ./a.out

comp.lang。(c ++)


------------删除元素后堆叠---------------

* .gnal.pmoc


/ home / arnuld / programming / C ++ $ ./a.out

comp.lan(g。(c ++(使用PAN)


------------删除元素后堆叠---------------

* nal.pmoc

I have a problem to work on:

we will ask user to input anything and we will put that back onto the
standard output with all set of brackets removed. We will not remove any
single bracket e.g.

INPUT: comp.lang.(c++)
OUTPUT: comp.lang.

INPUT: comp.lang.(c++
OUTPUT: comp.lang.(c++

INPUT: Bjarn)e St)roustr(up)
OUTPUT: Bjarn)e St)roustr

we do not want to remove any single opening or closing bracket. We are
interested in only set of brackets.
MY IDEA:

I am not able to decide which container to use. I thought of using
a Veoctr and only pushing input onto it if I am sure that I will not
find any closing bracket for the opening one. But how will I know I am
inside of set of brackets or I am just reading a single bracket. I was
reading about Stacks and came up with this code but it removes every
parethesis and elements following after a single opening parenthesis,
and since it is a Stack, it reverses all the input:

#include <iostream>
#include <stack>
#include <string>
int main()
{
std::stack<charstrStack;
bool P_ON = false;
char in_char;
while( std::cin >in_char )
{
if( in_char == ''('' )
{
P_ON = true;
}

if ( in_char == '')'')
{
P_ON = false;
}

if( P_ON == false && in_char != '')'')
{
strStack.push( in_char );
}
else if( P_ON == false && in_char == '')'' )
{
strStack.push( '' '' );
strStack.push( ''*'' );
strStack.push( '' '' );
/* a ''*'' with space son both side will represent a set of parentheseis
removed */
}
}

/* print the stack with desired elements removed*/
std::cout << "\n------------ STACK after elements removed ---------------\n";
while( strStack.empty() == false )
{
std::cout << strStack.top();
strStack.pop();
}

std::cout << std::endl;
return 0;
}

=========== OUTPUT ============
/home/arnuld/programming/C++ $ ./a.out
comp.lang.(c++)

------------ STACK after elements removed ---------------
* .gnal.pmoc

/home/arnuld/programming/C++ $ ./a.out
comp.lan(g. (c++(using PAN)

------------ STACK after elements removed ---------------
* nal.pmoc

推荐答案

./ a.out

comp.lang。(c ++)


------------删除元素后堆叠---------------

* .gnal.pmoc


/ home / arnuld / programming / C ++
./a.out
comp.lang.(c++)

------------ STACK after elements removed ---------------
* .gnal.pmoc

/home/arnuld/programming/C++


./ a.out

comp.lan(g。(c ++(使用PAN)


------------删除元素后堆叠---------- -----

* nal.pmoc

./a.out
comp.lan(g. (c++(using PAN)

------------ STACK after elements removed ---------------
* nal.pmoc


2007年10月20日星期六10:39:55 +0500, cront写道:
On Sat, 20 Oct 2007 10:39:55 +0500, cront wrote:

我有问题要处理:


我们会要求用户输入任何内容,我们会将其重新放回

标准输出,删除了所有括号。我们不会删除任何

单个支架,例如


INPUT:comp.lang。(c ++)

OUTPUT:comp.lang 。
I have a problem to work on:

we will ask user to input anything and we will put that back onto the
standard output with all set of brackets removed. We will not remove any
single bracket e.g.

INPUT: comp.lang.(c++)
OUTPUT: comp.lang.


.... [SNIP] ........
.... [SNIP]........


#include< iostream>

#include< stack>

#include< string>


int main()

{

std :: stack< charstrStack;

bool P_ON = false;

char in_char;

while(std :: cin> in_char)
#include <iostream>
#include <stack>
#include <string>
int main()
{
std::stack<charstrStack;
bool P_ON = false;
char in_char;
while( std::cin >in_char )



嘿,这是我的代码(虽然丑陋)。不要声称你写了它。


HEY, that''s my code (ugly though). Don''t claim that you wrote it.


这篇关于从输入中删除括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆