L4a_-_Addition_Stack_Applications_(from_previous_semeters)_(4_on_1)

L4a_-_Addition_Stack_Applications_(from_previous_semeters)_(4_on_1)

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Some stack applications: multibaseOutput string multibaseOutput (int num, int b) { string digitChar = "0123456789ABCDEF“; string numStr; // empty string stack<char> stk; do { stk. push (digitChar[num % b]); num /= b; // remove right-most digit from num } while (num != 0); // continue until all digits found while (!stk.empty()){ // flush the stack numStr += stk. top (); // add digit on top of stack to numStr stk. pop (); // pop the stack } return numStr; } 2 Recursive multibaseOutput string multibaseOutput (int n, int base) { const string digitChar = "0123456789ABCDEF"; if (n > 0) { return multibaseOutput (n/base, base) + digitChar [n % base]; } else return "" ; } 3 Application: Bracket Matching ± Ensures that pairs of brackets are properly matched A good example : {a,(b+f[4])*3,d+f[5]} Bad Examples : (..). .) // too many closing brackets (..(. .) // too many open brackets [..(. .]. .) // mismatched brackets 4 Expressions, Operators and operands ± +, -, *, /, % ± =, ?:, ~, !
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/02/2009 for the course ECE CS1102C taught by Professor Dr.tansuntuck during the Spring '07 term at National University of Singapore.

Page1 / 4

L4a_-_Addition_Stack_Applications_(from_previous_semeters)_(4_on_1)

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online