fork download
  1. /*
  2.   C++ Program to check for balanced parentheses in an expression using stack.
  3.   Given an expression as string comprising of opening and closing characters
  4.   of parentheses - (), curly braces - {} and square brackets - [], we need to
  5.   check whether symbols are balanced or not.
  6. */
  7. #include<iostream>
  8. #include<stack>
  9. #include<string>
  10. using namespace std;
  11. // Function to check whether two characters are opening
  12. // and closing of same type.
  13. bool ArePair(char opening,char closing)
  14. {
  15. if(opening == '(' && closing == ')') return true;
  16. else if(opening == '{' && closing == '}') return true;
  17. else if(opening == '[' && closing == ']') return true;
  18. return false;
  19. }
  20. bool AreParanthesesBalanced(string exp)
  21. {
  22. stack<char> S;
  23. for(int i =0;i<exp.length();i++)
  24. {
  25. if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
  26. S.push(exp[i]);
  27. else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
  28. {
  29. if(!(ArePair(S.top(),exp[i]) && !S.empty()))
  30. return false;
  31. else
  32. S.pop();
  33. }
  34. }
  35. return S.empty() ? true:false;
  36. }
  37.  
  38. int main()
  39. {
  40. /*Code to test the function AreParanthesesBalanced*/
  41. string expression;
  42. cout<<"Enter an expression: "; // input expression from STDIN/Console
  43. cin>>expression;
  44. if(AreParanthesesBalanced(expression))
  45. cout<<"Balanced\n";
  46. else
  47. cout<<"Not Balanced\n";
  48. }
  49.  
Success #stdin #stdout 0.01s 5540KB
stdin
//  --------------------------------------------------------------------------------------
//  DESCRIPTION
//  Creates UP_98204204
//  ---------------------------------------------------------------------------------------
//
// eslint-disable-next-line no-unused-vars
//  --------------------------------------------------------------------------------------
//  Logger
let log = (logPrefix, logMessage) => {
    me.logger({
        logPrefix: logPrefix,
        logMessage: logMessage,
        logLevel: 'debugGreen'
    });
};

// --------------------------------------------------------------------------------------
const UP_DB = Things['AGCO.UP.Thing.DatabaseServices'];
const PROJECT = 'AGCO.Unicorn-Planning.Data';
const BASE_LOG_PREFIX = me.name + '.UpdateUPnodes()';
const UE_GENERIC_SERVICES = Things["AGCO.UE.Thing.GenericServices"];
const EXCLUDE_FIELDS = ['tags', 'thingTemplate','description','name','AppName','PartView'];

log(BASE_LOG_PREFIX, 'Starting...');
(() => {
    'use strict';
    let count = 0;
    let now = new Date().getTime();
    let cachePartsToCreate = [];
    let partsCreatedSuccessfully = true;

    try {

        let dbParts= UP_DB.GetAllParts({}); //returns id and partnumber
        
        dbParts.rows.toArray().forEach((nextPart) => {
            now++;
            let unicornPartVals = {};
            let nextPartNumber = nextPart.partnumber;
            let nextPartID = nextPart.id;
            let newPartNumber = 'UP_' + now;
            let LOG_PREFIX = BASE_LOG_PREFIX + '.' + nextPartNumber + ': ';
            log(LOG_PREFIX, 'applying thing shape to part: ' + nextPartNumber);

            /* GET THE UNICORN DB VALUES AND ADD TO UP THING PROPERTIES */

            let unicornAttrsVals = UP_DB.SQLGetAttributeListWithValueByWTPartID({
                wtpart_id: nextPartID
            }); //output: AGCO.UP.editAttributeCollection


            if (unicornAttrsVals && unicornAttrsVals.getRowCount()) {
                unicornAttrsVals.rows.toArray().forEach((nextVal) => {
                    let safeAttrName = ((nextVal.attribute_name).split(' ')).join('');
                    if (nextVal.attribute_type === "Date") {
                        if (nextVal.attribute_value.indexOf('-') < 0) {
                            let date = Things["AGCO.UP.Thing.GenericServices"].ConvertTimeStampToDate({inputdate: nextVal.attribute_value});
                            unicornPartVals[safeAttrName] = date;
                        }
                    } else {
                        unicornPartVals[safeAttrName] = nextVal.attribute_value;
                    }
                });
            }
            log(LOG_PREFIX, ' found unicorn part vals ' + JSON.stringify(unicornPartVals));
            /* CREATE UP THING WITH BINDINGS TO WINDCHILL THING */

            // add thing shape to matching thing in classification tree
            try {
                let implementedPart = ThingTemplates[nextPartNumber].GetImplementingThingsWithData();
                let unicornParts = [];
                let windchillPart;
                let newUPThing;

                implementedPart.rows.toArray().forEach(nextThing => {
                    if ((nextThing.name).startsWith('UP_')) {
                        unicornParts.push(nextThing.name);
                    } else if ((nextThing.name).startsWith('WC_')) {
                        windchillPart = nextThing.name;
                    }
                });
                if (unicornParts.length) {
                    log(LOG_PREFIX, unicornParts.length +' things already exists');

                    unicornParts.sort();
                    let unicornPart = unicornParts.pop();
                    unicornParts.forEach(oldThing => {
                        Resources["EntityServices"].DeleteThing({
                            name: oldThing /* THINGNAME */
                        });
                    });
                    newUPThing = Things[unicornPart];

                } else {
                    Resources.EntityServices.CreateThing({
                        name: newPartNumber,
                        thingTemplateName: nextPartNumber,
                        projectName: PROJECT
                    });

                    Things[newPartNumber].EnableThing();
                    Things[newPartNumber].RestartThing();

                    Resources.EntityServices.AddShapeToThing({
                        name: newPartNumber,
                        thingShapeName: 'AGCO.UP.ThingShape.PlanningPart.ManageableAttributes'
                    });

                    Resources.EntityServices.AddShapeToThing({
                        name: newPartNumber,
                        thingShapeName: 'AGCO.UP.ThingShape.PlanningPart.SystemAttributes'
                    });

                    Resources.EntityServices.AddShapeToThing({
                        name: newPartNumber,
                        thingShapeName: 'AGCO.UP.ThingShape.PlanningPart.WindchillAttributes'
                    });

                    log(LOG_PREFIX, 'added shape to thing, updating properties');

                    newUPThing = Things[newPartNumber];
                }

                newUPThing.AppName = 'UP';
                newUPThing.PartView = 'Design';

                if (windchillPart) {
                    let windchillPartProperties = Things[windchillPart].GetPropertyValues();
                    let windchillPartFields = windchillPartProperties.dataShape.fields;
                    let windchillPartAttrsArray = [];
                    windchillPartFields.forEach((nextField) => {
                        if (!EXCLUDE_FIELDS.includes(windchillPartFields[nextField]["name"])) {
                            windchillPartAttrsArray.push(windchillPartFields[nextField]["name"]);   
                        }
                    });

                    let newUPThingProperties = newUPThing.GetPropertyValues();
                    let newUPThingFields = newUPThingProperties.dataShape.fields;

                    newUPThingFields.forEach((nextAttribute) => {
                        try {
                            if (!EXCLUDE_FIELDS.includes(newUPThingFields[nextAttribute]["name"])) {
                                //if the newUPThing has an attribute that is found in the windchill part, add a local binding
                                if (windchillPartAttrsArray.includes(newUPThingFields[nextAttribute]["name"])) {
                                    //add windchill cache link
                                    newUPThing.SetLocalPropertyBinding({
                                        propertyName: newUPThingFields[nextAttribute]["name"],
                                        sourceThingName: windchillPart,
                                        sourcePropertyName: newUPThingFields[nextAttribute]["name"]
                                    });
                                    log(LOG_PREFIX + ' attempting to bind to ' + newUPThingFields[nextAttribute]["name"] + '..');
                                } else if (unicornPartVals.hasOwnProperty(newUPThingFields[nextAttribute]["name"])) {
                                    newUPThing[newUPThingFields[nextAttribute]["name"]] = unicornPartVals[newUPThingFields[nextAttribute]["name"]];
                                    log(LOG_PREFIX + ' attempting to add to ' + newUPThingFields[nextAttribute]["name"] + ' with ' + unicornPartVals[newUPThingFields[nextAttribute]["name"]] + '?.');
                                } else {
                                    //log(LOG_PREFIX + ' no match for ' + newUPThingFields[nextAttribute]["name"]);
                                }
                            }
                        } catch (er) {
                            log(LOG_PREFIX, 'error updating = ' + er + newUPThingFields[nextAttribute]["name"]);
                        }
                    });

                } else {
                    log(LOG_PREFIX, ' no wc_* windchill part found for ' + nextPartNumber);
                }

            } catch (err) {
                log(LOG_PREFIX, 'error creating UP thing, err = ' + err);
            }

        });

        result = true;
    } catch (error) {
        log(BASE_LOG_PREFIX, ': Error = ' + error.message);
    }

    log(BASE_LOG_PREFIX, ': Done...' + count);
})();
stdout
Enter an expression:  Balanced