Smart Recent Blog Post List

Sep 10, 2011

Post a Suggestion/Query

Open post for adding suggestions/queries to be considered for next posting.


Queries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and SuggestionsQueries and Suggestions





7 comments:

Latest problem. Need to convert the following code from a taking an infix expression and converting to a postfix, to code that takes postfix and converts it to infix. It should be simple but again, my great teacher ha given no notes on what does what. Heres the code;




#include
#include
#include

using namespace std;


void Convert(const string & Infix, string & Postfix);

bool IsOperand(char ch);

bool TakesPrecedence(char OperatorA, char OperatorB);


int main(void)
{
char Reply;

do
{
string Infix, Postfix; // local to this loop

cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"
<< endl;
cin >> Infix;

Convert(Infix, Postfix);
cout << "The equivalent postfix expression is:" << endl
<< Postfix << endl;
cout << endl << "Do another (y/n)? ";
cin >> Reply;
}
while (tolower(Reply) == 'y');

return 0;
}


/* Given: ch A character.
Task: To determine whether ch represents an operand (here understood
to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false otherwise.
*/
bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}


/* Given: OperatorA A character representing an operator or parenthesis.
OperatorB A character representing an operator or parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.
Return: In the function name: true, if OperatorA takes precedence over
OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;

}


/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/
void Convert(const string & Infix, string & Postfix)
{
stack OperatorStack;
char TopSymbol, Symbol;
int k;

for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}

while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}

Postfix to infix will not give you exact expression in terms of paranthesis, though it will give you same result on evaluation.

e.g. (a+b+c)*2
Postfix will be :- ab+c+2*
And postfix to infix will give :- (((a+b)+c)*2)

Algorithm for it:-
Define one operand stack.

Read expression left-to-right.
step#1 If it is an operand push it on stack.
step#2 If it is an operator then
a) pop two value from stack and insert operator in them.
c) put them in parenthesis () too

perform above step for all the symbol in expression from left-to-right order.

Hiii, tajendra what is intraprocedural optimization performed by compiler on inline function and how to specify intraprocedural optimization?

Can you please specify your context\requirement ? so that i can help you in better way. Intraprocedural optimization is more of a compiler thing and it varies with different compilers. Though there are some standard techniques of optimization when it comes to intraprocedural things. One of the key thing here under this kind of optimization is 'high locality of reference'.

HI, TS. Sorry for very late reply. I am just visiting ur site after my 1st comment. I read in S meyers book that when you inline a function u may enable the compiler to perform context specific optimization on the body of inline function. Such optimizations aren't possible for normal function calls. I think that compiler can produce optimized object code when it performs context specific optimization. Correct me if am wrong. Please explain about induction variable elimination optimization. Please explain the following disadvantage also.
If used in header file, it will make your header file size large and may also make it unreadable. will you please explain it by giving suitable example.

Please reply me.

Your answer will be so helpful 4 me.

Thanks in advance

When Inline function substitution it made the calling function more aware about the context, and hence it provide more scope in calling function for better context based optimization. So if one feed more context (like inline function ) to compiler, it will help in generating better optimized code. Induction Variable optimization is a kind of loop optimization technique, where number of variable based operations are reduced by identifying common substitute logic. check this link (http://www.compileroptimizations.com/category/ive.htm)

Post a Comment

Smart Recent Blog Post List