Common Problems

Download Report

Transcript Common Problems

Pgm #1 Infix to Postfix
Common Problems/Issues/Mistakes
#1 I don’t know how to use getToken
• getToken returns the next token via a string array as its second
parameter. (You don’t have to pass an address since C already does
that for arrays.)
• getToken is passed a pointer (as the first parameter) which represents
the current position in the text line.
• getToken must have a mechanism to advance in the text line. It
functionally returns the next position. That is a pointer to the
position after the token.
• On a subsequent call, you must pass that new position as the first
parameter in the next call.
• Look at the example on the bottom of the stacksArray.docx. One of
the reasons for that example is to show how to use getToken.
#2 What do I pass to categorize?
• Categorize modifies an Element structure
// Element typedef used for Element values placed in the stack or out array
typedef struct
{
Token szToken;
// Could be a variable, numeric constant, operator,
// left parenthesis or right parenthesis
int iCategory;
// A value used to make it easier to handle
// different cases for the types of tokens
int iSubcategory;
// If the category is CAT_OPERAND, this
// specifies whether it is a VARIABLE or a CONSTANT.
int iPrecedence;
// An integer representing the operator
// precedence.
Higher values imply
// higher precedence.
} Element;
#2 What do I pass to categorize? (continued)
• It must be passed the address of an Element structure. A common
mistake:
Element element;
…
while (pszInfix != NULL)
{
categorize(element);
• The compiler will say something about the Element structure doesn’t
match a pointer to an element structure.
• Next common mistake:
Element *pElement;
…
while (pszInfix != NULL)
{
categorize(pElement);
#2 What do I pass to categorize? (continued)
• We really need to pass the address of the an Element structure:
Element element;
…
while (pszInfix != NULL)
{
categorize(&element);
• Make certain element.szToken has the current token.
#3 Not understanding the algorithm before
attempting to code it
• Make certain you understand how the algorithm works conceptually
before you code.
• Follow an example on the board.
#4 My program doesn’t work, what do I do?
• A debugger (e.g., ddd) is one of the most important tools you can use.
• One student had code like the following with a subtle bug:
pszText = getToken(pszText, szToken, MAX_TOKEN);
while (pszText != NULL);
{
categorize(&element);
switch(element.iCategory)
{
case CAT_OPERAND:
addOut(out, element);
break;
case CAT_LPAREN:
push(stack, element);
break;
#5 The pain of carriage returns
• Linux places only a new line character (\n) after the end of each line.
• Microsoft Windows has both a new line character (\n) and a carriage
return (\r) at the end of each line.
• If you copy/paste text lines from Windows to Linux, you might get
carriage returns in your file. You must remove them.
• On my setup page:
How can I remove carriage returns (\r) from Microsoft files on linux?
To see if a file contains carriage returns:
cat filename | od -c
To remove carriage returns from a file:
sed -i 's/\r//' filename
#5 The pain of carriage returns (continued)
To remove carriage returns from a file:
sed -i 's/\r//' filename
#6 Not asking for help
• I took this job to help students. That is what I enjoy doing!
• Since tutors and TAs might not be familiar with your assignment and
course notes, before asking a TA or tutor:
•
•
•
•
•
•
Read the assignment.
Examine the .h file.
Examine the provided input files
Examine the driver.
Look for relevant information in the course notes.
Print out all of those files
#7 Starting late  less time to ask for help