本文整理汇总了C++中Bracket类的典型用法代码示例。如果您正苦于以下问题:C++ Bracket类的具体用法?C++ Bracket怎么用?C++ Bracket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bracket类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
Bracket b;
if (b.BracketBalance())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
示例2: main
int main() {
std::string text;
getline(std::cin, text); // get the input string
std::stack <Bracket> opening_brackets_stack;
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
if (next == '(' || next == '[' || next == '{') { // store the left bracket in the stack
Bracket parenStart(next, position);
opening_brackets_stack.push(parenStart);
}
else if (next == ')' || next == ']' || next == '}') { // if right bracket is detected
if (opening_brackets_stack.empty()) // if the stack is empty, which means no left bracket is stored
{
std::cout << (position + 1) << std::endl; // output the position of this right bracket, break loop
break;
}
else
{
Bracket parenStartOut = opening_brackets_stack.top(); // get the latest left bracket,
//remove it from stack
opening_brackets_stack.pop();
if(!parenStartOut.Matchc(next)) // check if the latest left bracket is matched with
// the current right bracket
{
std::cout << (position + 1) << std::endl; // if not, output the position of this right bracket, break loop
break;
}
}
}
if (position == text.length() - 1) // if reach the end of the string
{
if (!opening_brackets_stack.empty()) // if the stack is not empty, output the position of
//left bracket at the end of the stack (the first unmatched one)
{
Bracket firstUnMatched('(',0); // initialization, initial values don't matter here
while(!opening_brackets_stack.empty())
{
firstUnMatched = opening_brackets_stack.top();
opening_brackets_stack.pop();
}
std::cout << (firstUnMatched.position + 1) << std::endl;
}
else // if stack has no left bracket, which means all the brackets have been matched
{
std::cout << "Success" << std::endl;
}
}
}
return 0;
}
示例3: score
Element* Bracket::drop(const DropData& data)
{
Element* e = data.element;
if (e->type() == Element::Type::BRACKET) {
Bracket* b = static_cast<Bracket*>(e);
score()->undoChangeBracketType(this, b->bracketType());
delete e;
return this;
}
delete e;
return 0;
}
示例4: main
int main() {
bool isBalanced = true;
int errorPos = 0;
std::string text;
getline(std::cin, text);
std::stack <Bracket> opening_brackets_stack;
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
if (next == '(' || next == '[' || next == '{') {
// Process opening bracket, write your code here
Bracket b = Bracket(next, position);
opening_brackets_stack.push(b);
}
if (next == ')' || next == ']' || next == '}') {
// Process closing bracket, write your code here
if( opening_brackets_stack.empty() == false )
{
Bracket b = opening_brackets_stack.top();
if ( b.Matchc(next) == false ) {
isBalanced = false;
errorPos = position;
break;
} else
opening_brackets_stack.pop();
} else
{
isBalanced = false;
errorPos = position;
break;
}
}
}
if(opening_brackets_stack.empty() == false && isBalanced == true)
{
isBalanced = false;
errorPos = opening_brackets_stack.top().position;
}
// Printing answer, write your code here
if ( isBalanced )
std::cout << "Success" << "\n";
else
std::cout << errorPos+1 << "\n";
return 0;
}
示例5: drop
Element* Bracket::drop(const DropData& data)
{
Element* e = data.element;
if (e->type() == BRACKET) {
Bracket* b = static_cast<Bracket*>(e);
b->setParent(parent());
b->setTrack(track());
b->setSpan(span());
b->setLevel(level());
score()->undoRemoveElement(this);
score()->undoAddElement(b);
return b;
}
delete e;
return 0;
}
示例6: instrumentNameOffset
void System::layoutSystem(qreal xo1)
{
if (_staves.empty()) // ignore vbox
return;
static const Spatium instrumentNameOffset(1.0); // TODO: make style value
int nstaves = _staves.size();
//---------------------------------------------------
// find x position of staves
// create brackets
//---------------------------------------------------
qreal xoff2 = 0.0; // x offset for instrument name
int bracketLevels = 0;
for (int idx = 0; idx < nstaves; ++idx)
bracketLevels = qMax(bracketLevels, score()->staff(idx)->bracketLevels());
qreal bracketWidth[bracketLevels];
for (int i = 0; i < bracketLevels; ++i)
bracketWidth[i] = 0.0;
QList<Bracket*> bl;
bl.swap(_brackets);
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
Staff* s = score()->staff(staffIdx);
for (int i = 0; i < bracketLevels; ++i) {
if (s->bracket(i) == BracketType::NO_BRACKET)
continue;
int firstStaff = staffIdx;
int lastStaff = staffIdx + s->bracketSpan(i) - 1;
if (lastStaff >= nstaves)
lastStaff = nstaves - 1;
for (; firstStaff <= lastStaff; ++firstStaff) {
if (score()->staff(firstStaff)->show())
break;
}
for (; lastStaff >= firstStaff; --lastStaff) {
if (score()->staff(lastStaff)->show())
break;
}
int span = lastStaff - firstStaff + 1;
//
// do not show bracket if it only spans one
// system due to some invisible staves
//
if ((span > 1) || (s->bracketSpan(i) == span)) {
//
// this bracket is visible
//
Bracket* b = 0;
int track = staffIdx * VOICES;
for (int k = 0; k < bl.size(); ++k) {
if (bl[k]->track() == track && bl[k]->level() == i) {
b = bl.takeAt(k);
break;
}
}
if (b == 0) {
b = new Bracket(score());
b->setGenerated(true);
b->setTrack(track);
b->setLevel(i);
}
add(b);
b->setFirstStaff(firstStaff);
b->setLastStaff(lastStaff);
b->setBracketType(s->bracket(i));
b->setSpan(s->bracketSpan(i));
bracketWidth[i] = qMax(bracketWidth[i], b->width());
}
}
if (!s->show())
continue;
for (InstrumentName* t : _staves[staffIdx]->instrumentNames) {
t->layout();
qreal w = t->width() + point(instrumentNameOffset);
if (w > xoff2)
xoff2 = w;
}
}
for (Bracket* b : bl)
delete b;
//---------------------------------------------------
// layout SysStaff and StaffLines
//---------------------------------------------------
_leftMargin = xoff2;
qreal bd = score()->styleP(StyleIdx::bracketDistance);
if ( _brackets.size() > 0) {
for (int i = 0; i < bracketLevels; ++i)
_leftMargin += bracketWidth[i] + bd;
//.........这里部分代码省略.........
示例7: bracketMinimum
Bracket OneDimensionOptimizationTools::bracketMinimum(
double a,
double b,
Function * function,
ParameterList parameters)
{
Bracket bracket;
// Copy the parameter to use.
bracket.a.x = a;
parameters[0].setValue(bracket.a.x);
bracket.a.f = function->f(parameters);
bracket.b.x = b;
parameters[0].setValue(bracket.b.x);
bracket.b.f = function->f(parameters);
if (bracket.b.f > bracket.a.f)
{
// Switch roles of first and second point so that we can go downhill
// in the direction from a to b.
NumTools::swap<double>(bracket.a.x, bracket.b.x);
NumTools::swap<double>(bracket.a.f, bracket.b.f);
}
// First guess for third point:
bracket.c.x = bracket.b.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.b.x - bracket.a.x);
parameters[0].setValue(bracket.c.x);
bracket.c.f = function->f(parameters);
// Keep returning here until we bracket:
while (bracket.b.f > bracket.c.f)
{
// Compute xu by parabolic extrapolation from a, b, c. TINY is used to prevent
// any possible division by 0.
double r = (bracket.b.x - bracket.a.x) * (bracket.b.f - bracket.c.f);
double q = (bracket.b.x - bracket.c.x) * (bracket.b.f - bracket.a.f);
double xu = bracket.b.x - ((bracket.b.x - bracket.c.x) * q - (bracket.b.x - bracket.a.x) * r) /
(2.0 * NumTools::sign(NumTools::max(NumTools::abs(q - r), NumConstants::VERY_TINY()), q - r));
double xulim = (bracket.b.x) + GLIMIT * (bracket.c.x - bracket.b.x);
double fu;
// We don't go farther than this.
// Test various possibilities:
if ((bracket.b.x - xu) * (xu - bracket.c.x) > 0.0)
{
parameters[0].setValue(xu);
fu = function->f(parameters);
if (fu < bracket.c.f)
{
bracket.setA(bracket.b.x, bracket.b.f);
bracket.setB(xu, fu);
return bracket;
}
else if (fu > bracket.b.f)
{
bracket.setC(xu, fu);
return bracket;
}
// Parabolic fit was no use.
// Use default magnification.
xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
parameters[0].setValue(xu);
fu = function->f(parameters);
}
else if ((bracket.c.x - xu) * (xu - xulim) > 0.0)
{
// Parabolic fit is between point 3 and its allowed limit.
parameters[0].setValue(xu);
fu = function->f(parameters);
if (fu < bracket.c.f)
{
NumTools::shift<double>(bracket.b.x, bracket.c.x, xu, bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x));
parameters[0].setValue(xu);
NumTools::shift<double>(bracket.b.f, bracket.c.f, fu, function->f(parameters));
}
}
else if ((xu - xulim) * (xulim - bracket.c.x) >= 0.0)
{
// Limit parabolic xu to maximum allowed value.
xu = xulim;
parameters[0].setValue(xu);
fu = function->f(parameters);
}
else
{
// Reject parabolic xu, use default magnification.
xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
parameters[0].setValue(xu);
fu = function->f(parameters);
}
// Eliminate oldest point and continue.
NumTools::shift<double>(bracket.a.x, bracket.b.x, bracket.c.x, xu);
NumTools::shift<double>(bracket.a.f, bracket.b.f, bracket.c.f, fu);
}
return bracket;
}
示例8: spatium
//.........这里部分代码省略.........
int n = ml.size();
for (int i = 0; i < n; ++i) {
MeasureBase* m = ml.at(i);
if (m->type() == Element::Type::MEASURE) {
// note that the factor 2 * _spatium must be corrected for when exporting
// system distance in MusicXML (issue #24733)
m->bbox().setRect(0.0, -_spatium, m->width(), systemHeight + 2 * _spatium);
}
else if (m->type() == Element::Type::HBOX) {
m->bbox().setRect(0.0, 0.0, m->width(), systemHeight);
static_cast<HBox*>(m)->layout2();
}
}
if (_barLine) {
_barLine->setTrack(firstStaffIdx * VOICES);
_barLine->setSpan(lastStaffIdx - firstStaffIdx + 1);
if (score()->staff(0)->lines() == 1)
_barLine->setSpanFrom(BARLINE_SPAN_1LINESTAFF_FROM);
int spanTo = (score()->staff(lastStaffIdx)->lines() == 1) ?
BARLINE_SPAN_1LINESTAFF_TO :
(score()->staff(lastStaffIdx)->lines()-1)*2;
_barLine->setSpanTo(spanTo);
_barLine->layout();
}
//---------------------------------------------------
// layout brackets vertical position
//---------------------------------------------------
n = _brackets.size();
for (int i = 0; i < n; ++i) {
Bracket* b = _brackets.at(i);
int staffIdx1 = b->firstStaff();
int staffIdx2 = b->lastStaff();
qreal sy = 0; // assume bracket not visible
qreal ey = 0;
// if start staff not visible, try next staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx1]->show())
++staffIdx1;
// if end staff not visible, try prev staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx2]->show())
--staffIdx2;
// the bracket will be shown IF:
// it spans at least 2 visible staves (staffIdx1 < staffIdx2) OR
// it spans just one visible staff (staffIdx1 == staffIdx2) but it is required to do so
// (the second case happens at least when the bracket is initially dropped)
bool notHidden = (staffIdx1 < staffIdx2) || (b->span() == 1 && staffIdx1 == staffIdx2);
if (notHidden) { // set vert. pos. and height to visible spanned staves
sy = _staves[staffIdx1]->bbox().top();
ey = _staves[staffIdx2]->bbox().bottom();
}
b->rypos() = sy;
b->setHeight(ey - sy);
b->layout();
}
//---------------------------------------------------
// layout instrument names
//---------------------------------------------------
int staffIdx = 0;
n = score()->parts().size();
for (Part* p : score()->parts()) {