SlideShare une entreprise Scribd logo
1  sur  9
Télécharger pour lire hors ligne
Massachusetts Institute of Technology

Department of Electrical Engineering and Computer Science

6.087: Practical Programming in C
IAP 2010
Problem Set 6
Out: Wednesday, January 20, 2010.	 Due: Friday, January 22, 2010.
This is Part 1 of a two-part assignment. Part 2 will be released Thursday.
Problem 6.1
In this problem, we will implement a simple “four-function” calculator using stacks and queues.
This calculator takes as input a space-delimited infix expression (e.g. 3 + 4 * 7), which you
will convert to postfix notation and evaluate. There are four (binary) operators your calculator
must handle: addition (+), subtraction (-), multiplication (*), and division (/). In addition, your
calculator must handle the unary negation operator (also -). The usual order of operations is in
effect:
•	 the unary negation operator - has higher precedence than the binary operators, and is eval­
uated right-to-left (right-associative)
•	 * and / have higher precedence than + and ­
•	 all binary operators are evaluated left-to-right (left-associative)
To start, we will not consider parentheses in our expressions. The code is started for you in prob1.c,
which is available for download from Stellar. Read over the file, paying special attention to the
data structures used for tokens, the stack, and queue, as well as the functions you will complete.
(a) We have provided code to translate the string to a queue of tokens, arranged in infix (natural)
order. You must:
–	 fill in the infix to postfix() function to construct a queue of tokens arranged in
postfix order (the infix queue should be empty when you’re done)
–	 complete the evaluate postfix() function to evaluate the expression stored in the
postfix queue and return the answer
You may assume the input is a valid infix expression, but it is good practice to make your
code robust by handling possible errors (e.g. not enough tokens) . Turn in a printout of your
code, along with a printout showing the output from your program for a few test cases (infix
expressions of your choosing) to demonstrate it works properly.
(b) Now,	 an infix calculator really is not complete if parentheses are not allowed. So, in this
part, update the function infix to postfix() to handle parentheses as we discussed in
class. Note: your postfix queue should contain no parentheses tokens. Turn in a printout of
your code, along with a printout showing the output from your program for a few test cases
utilizing parentheses.
1
info@programmingassignments.com
https://www.programmingassignments.com/
							Pointers	to	pointers.	Multidimensional	arrays.	Stacks	and	queues.
Go To Code Directly
Submit Assignment For Help
Problem 6.2
A useful data structure for storing lots of strings is the “trie.” This tree structure has the special
property that a node’s key is a prefix of the keys of its children. For instance, if we associate a node
with the string “a,” that node may have a child node with the key “an,” which in turn may have a
child node “any.” When many strings share a common prefix, this structure is a very inexpensive
way to store them. Another consequence of this storage method is that the trie supports very fast
searching – the complexity of finding a string with m characters is O(m).
(root)
pointer array of childre �
�
�
��
�
�
�
��
"a" "c"
�
�
�
��
�
�
�
��
"an" "ca"
�
�
�
��
�
�
�
��
�
�
�
��
"and" "ant" "cat"
Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not
explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key
of its parent node + its index in that parent’s pointer array of children.
In this problem, you will utilize a trie structure and to implement a simple one-way English-
to-French dictionary. The trie structure, shown in Figure 6.2-1, consists of nodes, each of which
contains a string for storing translations for the word specified at that node and an array of pointers
to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the
dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may
contain multiple translations for the same word; in this case, words should be separated by commas.
For example: the word like, which has two meanings, could translate as comme, a preposition, or
as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve
provided code in prob2.c, which can be downloaded from Stellar. You will need to:
•	 fill in the helper functions new node(), delete node()
•	 complete the function add word(), which adds a word to the trie
•	 complete the function lookup word(), which searches the trie for a word and returns its
translation(s)
Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of
your program running in gdb, with a few example translations to demonstrate functionality.
2
Massachusetts Institute of Technology

Department of Electrical Engineering and Computer Science

6.087: Practical Programming in C
IAP 2010
Problem Set 6 – Solutions
Out: Wednesday, January 20, 2010.	 Due: Friday, January 22, 2010.
This is Part 1 of a two-part assignment. Part 2 will be released Thursday.
Problem 6.1
In this problem, we will implement a simple “four-function” calculator using stacks and queues.
This calculator takes as input a space-delimited infix expression (e.g. 3 + 4 * 7), which you
will convert to postfix notation and evaluate. There are four (binary) operators your calculator
must handle: addition (+), subtraction (-), multiplication (*), and division (/). In addition, your
calculator must handle the unary negation operator (also -). The usual order of operations is in
effect:
•	 the unary negation operator - has higher precedence than the binary operators, and is eval­
uated right-to-left (right-associative)
•	 * and / have higher precedence than + and ­
•	 all binary operators are evaluated left-to-right (left-associative)
To start, we will not consider parentheses in our expressions. The code is started for you in prob1.c,
which is available for download from Stellar. Read over the file, paying special attention to the
data structures used for tokens, the stack, and queue, as well as the functions you will complete.
(a) We have provided code to translate the string to a queue of tokens, arranged in infix (natural)
order. You must:
–	 fill in the infix to postfix() function to construct a queue of tokens arranged in
postfix order (the infix queue should be empty when you’re done)
–	 complete the evaluate postfix() function to evaluate the expression stored in the
postfix queue and return the answer
You may assume the input is a valid infix expression, but it is good practice to make your
code robust by handling possible errors (e.g. not enough tokens) . Turn in a printout of your
code, along with a printout showing the output from your program for a few test cases (infix
expressions of your choosing) to demonstrate it works properly.
1

Pointers	to	pointers.	Multidimensional	arrays.	Stacks	and	queues.
Answer: Here’s one possible implementation: (only functions infix to postfix() and
evaluate postfix() shown)
/∗ c r e a t e s a queue of tokens in p o s t f i x order from a queue of tokens in i n f i x order ∗/
/∗ postcondition : returned queue contains a l l the tokens , and pqueue infix should be
empty ∗/
struct token queue i n f i x t o p o s t f i x ( struct token queue ∗ pqueue infix ) {
/∗ TODO: construct postfix −ordered queue from i n f i x −ordered queue ;

a l l tokens from i n f i x queue should be added to p o s t f i x queue or freed ∗/

p expr token stack top = NULL, ptoken ;

struct token queue queue postfix ;

queue postfix . front = queue postfix . back = NULL;
for ( ptoken = dequeue ( pqueue infix ) ; ptoken ; ptoken = dequeue ( pqueue infix )) {

switch ( ptoken−>type ) {

case OPERAND:

/∗	 operands added d i r e c t l y to p o s t f i x queue ∗/
enqueue(&queue postfix , ptoken ) ;

break ;

case OPERATOR:

/∗	 operator added to stack , a f t e r operators of higher

precedence are moved to queue ∗/

while ( stack top &&

( op precedences [ stack top −>value . op code ] >

op precedences [ ptoken−>value . op code ] | |

( op precedences [ stack top −>value . op code ] ==

op	 precedences [ ptoken−>value . op code ] &&
o p	 a s s o c i a t i v i t y [ op precedences [ ptoken−>value . op code ] ] == LEFT) ) )
enqueue(&queue postfix , pop(&stack top ) ) ;

push(&stack top , ptoken ) ;

break ;

default : /∗ other tokens ignored ∗/

f r e e ( ptoken ) ;

break ;

}
}
while ( stack top ) /∗ pop remaining operators o f f stack ∗/

enqueue(&queue postfix , pop(&stack top ) ) ;

return queue postfix ;

}
/∗ evalutes the p o s t f i x expression stored in the queue ∗/

/∗ postcondition : returned value i s f i n a l answer , and pqueue postfix should be empty ∗/

double e v a l u a t e p o s t f i x ( struct token queue ∗ pqueue postfix ) {

/∗ TODO: process postfix −ordered queue and return f i n a l answer ;

a l l tokens from postfix −ordered queue i s freed
 ∗/
double ans = 0 . ;

p expr token stack values = NULL, ptoken , pvalue ;

double operands [ 2 ] ; /∗ max two operands ∗/

union token value value ;

int i ;

while ( ( ptoken = dequeue ( pqueue postfix )) ) {

switch ( ptoken−>type ) {

case OPERAND:

/∗	 operands always pushed to stack ∗/
push(&stack values , ptoken ) ;

break ;

case OPERATOR:

2
/∗ pop operands from stack ∗/
for	 ( i = 0; i < op operands [ ptoken−>value . op code ] ; i++) {
i f ( ( pvalue = pop(& stack values )) ) {

operands [ i ] = pvalue−>value . operand ;

f r e e ( pvalue ) ; /∗ done with token ∗/

else
}
goto e r r o r ;

}
/∗ process operands according to opcode ∗/

/∗ note operands are popped in reverse order ∗/

switch ( ptoken−>value . op code ) {
case ADD:

value . operand = operands [1]+ operands [ 0 ] ;

break ;

case SUBTRACT:

value . operand = operands [1] − operands [ 0 ] ;

break ;

case MULTIPLY:

value . operand = operands [ 1 ] ∗ operands [ 0 ] ;

break ;

case DIVIDE :

value . operand = operands [ 1 ] / operands [ 0 ] ;

break ;

case NEGATE:

value . operand = −operands [ 0 ] ;

}
/∗ push new token with operator r e s u l t to stack ∗/

push(&stack values , new token (OPERAND, value ) ) ;

default :

f r e e ( ptoken ) ; /∗ f r e e token ∗/

break ;

}
}
/∗ return value i s on top of stack ( should be l a s t value on stack ) ∗/
i f ( stack values )
ans = stack values −>value . operand ;
cleanup :
/∗	 f r e e any remaining tokens ∗/
while ( ( ptoken = dequeue ( pqueue postfix )) )

f r e e ( ptoken ) ;

while ( ( pvalue = pop(& stack values )) )

f r e e ( pvalue ) ;

return ans ;

e r r o r :

fputs ( "Error evaluating the expression.n" , s t d e r r ) ;

goto cleanup ;

}
(b) Now,	 an infix calculator really is not complete if parentheses are not allowed. So, in this
part, update the function infix to postfix() to handle parentheses as we discussed in
class. Note: your postfix queue should contain no parentheses tokens. Turn in a printout of
your code, along with a printout showing the output from your program for a few test cases
utilizing parentheses.
3
Answer: Here’s an implementation: (only function infix to postfix() shown)
/∗ c r e a t e s a queue of tokens in p o s t f i x order from a queue of tokens in i n f i x order ∗/
/∗ postcondition : returned queue contains a l l the tokens , and pqueue infix should be
empty ∗/
struct token queue i n f i x t o p o s t f i x ( struct token queue ∗ pqueue infix ) {
/∗ TODO: construct postfix −ordered queue from i n f i x −ordered queue ;
a l l tokens from i n f i x queue should be added to p o s t f i x queue or freed ∗/
p expr token stack top = NULL, ptoken ;

struct token queue queue postfix ;

queue postfix . front = queue postfix . back = NULL;
for ( ptoken = dequeue ( pqueue infix ) ; ptoken ; ptoken = dequeue ( pqueue infix )) {
switch ( ptoken−>type ) {
case OPERAND:
/∗	 operands added d i r e c t l y to p o s t f i x queue ∗/
enqueue(&queue postfix , ptoken ) ;

break ;

case OPERATOR:

/∗	 operator added to stack , a f t e r operators of higher

precedence are moved to queue ∗/

while ( stack top && stack top −>type == OPERATOR &&

( op precedences [ stack top −>value . op code ] >

op precedences [ ptoken−>value . op code ] | |

( op precedences [ stack top −>value . op code ] ==

op	 precedences [ ptoken−>value . op code ] &&
o p	 a s s o c i a t i v i t y [ op precedences [ ptoken−>value . op code ] ] == LEFT) ) )
enqueue(&queue postfix , pop(&stack top ) ) ;

push(&stack top , ptoken ) ;

break ;

case LPARENS:
/∗	 pushed to operator stack ∗/
push(&stack top , ptoken ) ;

break ;

case RPARENS:

/∗ pop operators o f f stack u n t i l l e f t parentheses reached ∗/
f r e e ( ptoken ) ; /∗ parentheses not included in p o s t f i x queue ∗/
while ( ( ptoken = pop(&stack top )) ) {
i f ( ptoken−>type == LPARENS) {

f r e e ( ptoken ) ;

break ;

}
enqueue(&queue postfix , ptoken ) ;
}

}

}
while ( stack top ) /∗ pop remaining operators o f f stack ∗/

enqueue(&queue postfix , pop(&stack top ) ) ;

return queue postfix ;

}
Problem 6.2
A useful data structure for storing lots of strings is the “trie.” This tree structure has the special
property that a node’s key is a prefix of the keys of its children. For instance, if we associate a node
with the string “a,” that node may have a child node with the key “an,” which in turn may have a
child node “any.” When many strings share a common prefix, this structure is a very inexpensive
4
way to store them. Another consequence of this storage method is that the trie supports very fast
searching – the complexity of finding a string with m characters is O(m).
(root)
pointer array of childre �
�
�
��
�
�
�
��
"a" "c"
�
�
�
��
�
�
�
��
"an" "ca"
�
�
�
��
�
�
�
��
�
�
�
��
"and" "ant" "cat"
Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not
explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key
of its parent node + its index in that parent’s pointer array of children.
In this problem, you will utilize a trie structure and to implement a simple one-way English-
to-French dictionary. The trie structure, shown in Figure 6.2-1, consists of nodes, each of which
contains a string for storing translations for the word specified at that node and an array of pointers
to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the
dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may
contain multiple translations for the same word; in this case, words should be separated by commas.
For example: the word like, which has two meanings, could translate as comme, a preposition, or
as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve
provided code in prob2.c, which can be downloaded from Stellar. You will need to:
•	 fill in the helper functions new node(), delete node()
•	 complete the function add word(), which adds a word to the trie
•	 complete the function lookup word(), which searches the trie for a word and returns its
translation(s)
Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of
your program running in gdb, with a few example translations to demonstrate functionality.
5
Answer: one possible implementation of the four functions is shown below:
/∗	 a l l o c a t e new node on the heap
output : pointer to new node ( must be freed ) ∗/
struct s t r i e n o d e ∗ new node ( void ) {
/∗ TODO: a l l o c a t e a new node on the heap , and

i n i t i a l i z e a l l f i e l d s to d ef a ul t values ∗/

struct s t r i e n o d e pnode =∗
( struct s t r i e n o d e ∗) malloc (

sizeof ( struct s t r i e n o d e ) ) ;

int i ;

pnode−>t r a n s l a t i o n = NULL;

for ( i = 0; i < UCHARMAX+1; i++)

pnode−>children [ i ] = NULL;

return pnode ;

}
/∗	 d e l e t e node and a l l i t s children
input : pointer to node to d e l e t e
postcondition : node and children are freed ∗/
void delete node ( struct s t r i e n o d e ∗ pnode ) {
/∗ TODO: d e l e t e node and a l l i t s children

Be sure to f r e e non−n u l l t r a n s l a t i o n s !

Hint : use recursion

∗/
int i ;
i f ( pnode−>t r a n s l a t i o n )

f r e e ( pnode−>t r a n s l a t i o n ) ;

for ( i = 0; i < UCHARMAX+1; i++)

i f ( pnode−>children [ i ] )

delete node ( pnode−>children [ i ] ) ;

f r e e ( pnode ) ;

}
/∗	 add word to t r i e , with t r a n s l a t i o n
input : word and t r a n s l a t i o n
output : non−zero i f new node added , zero otherwise
postcondition : word e x i s t s in t r i e ∗/
int add word ( const char ∗ word , char ∗ t r a n s l a t i o n ) {
/∗ TODO: add word to t r i e structure

I f word ex i st s , append t r a n s l a t i o n to e x i s t i n g

s t r i n g

Be sure to s t o r e a copy of translation , s i n c e

the s t r i n g i s reused by l o a d d i c t i o n a r y ()

∗/
struct s t r i e n o d e pnode = proot ;∗
int i , len = s t r l e n ( word ) , inew = 0;

unsigned char j ;

for ( i = 0; i < len ; i++) {

j = word [ i ] ;

i f (( inew = ! pnode−>children [ j ] ) )

pnode−>children [ j ] = new node ( ) ;

pnode = pnode−>children [ j ] ;

}
i f	 ( pnode−>t r a n s l a t i o n ) {
/∗	 concatenate s t r i n g s ∗/
6
char o l d t r a n s l a t i o n = pnode−>t r a n s l a t i o n ;∗
int oldlen = s t r l e n ( o l d t r a n s l a t i o n ) ,
newlen = s t r l e n ( t r a n s l a t i o n ) ;
pnode−>t r a n s l a t i o n = malloc ( oldlen + newlen + 2 ) ;
strcpy ( pnode−>translation , o l d t r a n s l a t i o n ) ;
strcpy ( pnode−>t r a n s l a t i o n+oldlen , "," ) ;
strcpy ( pnode−>t r a n s l a t i o n+oldlen +1, t r a n s l a t i o n ) ;
f r e e ( o l d t r a n s l a t i o n ) ;
} else

pnode−>t r a n s l a t i o n = strcpy ( malloc (

s t r l e n ( t r a n s l a t i o n )+1) , t r a n s l a t i o n ) ;

return inew ;

}
/∗	 search t r i e structure f o r word and return t r a n s l a t i o n s
input : word to search
output : translation , or NULL i f not found ∗/
char ∗ lookup word ( const char ∗ word ) {
/∗ TODO: search t r i e structure f o r word

return NULL i f word i s not found ∗/

struct s t r i e n o d e pnode = proot ;∗
int i , len = s t r l e n ( word ) ;

unsigned char j ;

for ( i = 0; i < len ; i++) {

j = word [ i ] ;

i f ( ! pnode−>children [ j ] )

return NULL;

pnode = pnode−>children [ j ] ;

}
return pnode−>t r a n s l a t i o n ;
}
7

Contenu connexe

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 

Dernier (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

En vedette

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

En vedette (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

C Programming Assignment Help

  • 1. Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.087: Practical Programming in C IAP 2010 Problem Set 6 Out: Wednesday, January 20, 2010. Due: Friday, January 22, 2010. This is Part 1 of a two-part assignment. Part 2 will be released Thursday. Problem 6.1 In this problem, we will implement a simple “four-function” calculator using stacks and queues. This calculator takes as input a space-delimited infix expression (e.g. 3 + 4 * 7), which you will convert to postfix notation and evaluate. There are four (binary) operators your calculator must handle: addition (+), subtraction (-), multiplication (*), and division (/). In addition, your calculator must handle the unary negation operator (also -). The usual order of operations is in effect: • the unary negation operator - has higher precedence than the binary operators, and is eval­ uated right-to-left (right-associative) • * and / have higher precedence than + and ­ • all binary operators are evaluated left-to-right (left-associative) To start, we will not consider parentheses in our expressions. The code is started for you in prob1.c, which is available for download from Stellar. Read over the file, paying special attention to the data structures used for tokens, the stack, and queue, as well as the functions you will complete. (a) We have provided code to translate the string to a queue of tokens, arranged in infix (natural) order. You must: – fill in the infix to postfix() function to construct a queue of tokens arranged in postfix order (the infix queue should be empty when you’re done) – complete the evaluate postfix() function to evaluate the expression stored in the postfix queue and return the answer You may assume the input is a valid infix expression, but it is good practice to make your code robust by handling possible errors (e.g. not enough tokens) . Turn in a printout of your code, along with a printout showing the output from your program for a few test cases (infix expressions of your choosing) to demonstrate it works properly. (b) Now, an infix calculator really is not complete if parentheses are not allowed. So, in this part, update the function infix to postfix() to handle parentheses as we discussed in class. Note: your postfix queue should contain no parentheses tokens. Turn in a printout of your code, along with a printout showing the output from your program for a few test cases utilizing parentheses. 1 info@programmingassignments.com https://www.programmingassignments.com/ Pointers to pointers. Multidimensional arrays. Stacks and queues. Go To Code Directly Submit Assignment For Help
  • 2. Problem 6.2 A useful data structure for storing lots of strings is the “trie.” This tree structure has the special property that a node’s key is a prefix of the keys of its children. For instance, if we associate a node with the string “a,” that node may have a child node with the key “an,” which in turn may have a child node “any.” When many strings share a common prefix, this structure is a very inexpensive way to store them. Another consequence of this storage method is that the trie supports very fast searching – the complexity of finding a string with m characters is O(m). (root) pointer array of childre � � � �� � � � �� "a" "c" � � � �� � � � �� "an" "ca" � � � �� � � � �� � � � �� "and" "ant" "cat" Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key of its parent node + its index in that parent’s pointer array of children. In this problem, you will utilize a trie structure and to implement a simple one-way English- to-French dictionary. The trie structure, shown in Figure 6.2-1, consists of nodes, each of which contains a string for storing translations for the word specified at that node and an array of pointers to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may contain multiple translations for the same word; in this case, words should be separated by commas. For example: the word like, which has two meanings, could translate as comme, a preposition, or as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve provided code in prob2.c, which can be downloaded from Stellar. You will need to: • fill in the helper functions new node(), delete node() • complete the function add word(), which adds a word to the trie • complete the function lookup word(), which searches the trie for a word and returns its translation(s) Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of your program running in gdb, with a few example translations to demonstrate functionality. 2
  • 3. Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.087: Practical Programming in C IAP 2010 Problem Set 6 – Solutions Out: Wednesday, January 20, 2010. Due: Friday, January 22, 2010. This is Part 1 of a two-part assignment. Part 2 will be released Thursday. Problem 6.1 In this problem, we will implement a simple “four-function” calculator using stacks and queues. This calculator takes as input a space-delimited infix expression (e.g. 3 + 4 * 7), which you will convert to postfix notation and evaluate. There are four (binary) operators your calculator must handle: addition (+), subtraction (-), multiplication (*), and division (/). In addition, your calculator must handle the unary negation operator (also -). The usual order of operations is in effect: • the unary negation operator - has higher precedence than the binary operators, and is eval­ uated right-to-left (right-associative) • * and / have higher precedence than + and ­ • all binary operators are evaluated left-to-right (left-associative) To start, we will not consider parentheses in our expressions. The code is started for you in prob1.c, which is available for download from Stellar. Read over the file, paying special attention to the data structures used for tokens, the stack, and queue, as well as the functions you will complete. (a) We have provided code to translate the string to a queue of tokens, arranged in infix (natural) order. You must: – fill in the infix to postfix() function to construct a queue of tokens arranged in postfix order (the infix queue should be empty when you’re done) – complete the evaluate postfix() function to evaluate the expression stored in the postfix queue and return the answer You may assume the input is a valid infix expression, but it is good practice to make your code robust by handling possible errors (e.g. not enough tokens) . Turn in a printout of your code, along with a printout showing the output from your program for a few test cases (infix expressions of your choosing) to demonstrate it works properly. 1 Pointers to pointers. Multidimensional arrays. Stacks and queues.
  • 4. Answer: Here’s one possible implementation: (only functions infix to postfix() and evaluate postfix() shown) /∗ c r e a t e s a queue of tokens in p o s t f i x order from a queue of tokens in i n f i x order ∗/ /∗ postcondition : returned queue contains a l l the tokens , and pqueue infix should be empty ∗/ struct token queue i n f i x t o p o s t f i x ( struct token queue ∗ pqueue infix ) { /∗ TODO: construct postfix −ordered queue from i n f i x −ordered queue ; a l l tokens from i n f i x queue should be added to p o s t f i x queue or freed ∗/ p expr token stack top = NULL, ptoken ; struct token queue queue postfix ; queue postfix . front = queue postfix . back = NULL; for ( ptoken = dequeue ( pqueue infix ) ; ptoken ; ptoken = dequeue ( pqueue infix )) { switch ( ptoken−>type ) { case OPERAND: /∗ operands added d i r e c t l y to p o s t f i x queue ∗/ enqueue(&queue postfix , ptoken ) ; break ; case OPERATOR: /∗ operator added to stack , a f t e r operators of higher precedence are moved to queue ∗/ while ( stack top && ( op precedences [ stack top −>value . op code ] > op precedences [ ptoken−>value . op code ] | | ( op precedences [ stack top −>value . op code ] == op precedences [ ptoken−>value . op code ] && o p a s s o c i a t i v i t y [ op precedences [ ptoken−>value . op code ] ] == LEFT) ) ) enqueue(&queue postfix , pop(&stack top ) ) ; push(&stack top , ptoken ) ; break ; default : /∗ other tokens ignored ∗/ f r e e ( ptoken ) ; break ; } } while ( stack top ) /∗ pop remaining operators o f f stack ∗/ enqueue(&queue postfix , pop(&stack top ) ) ; return queue postfix ; } /∗ evalutes the p o s t f i x expression stored in the queue ∗/ /∗ postcondition : returned value i s f i n a l answer , and pqueue postfix should be empty ∗/ double e v a l u a t e p o s t f i x ( struct token queue ∗ pqueue postfix ) { /∗ TODO: process postfix −ordered queue and return f i n a l answer ; a l l tokens from postfix −ordered queue i s freed ∗/ double ans = 0 . ; p expr token stack values = NULL, ptoken , pvalue ; double operands [ 2 ] ; /∗ max two operands ∗/ union token value value ; int i ; while ( ( ptoken = dequeue ( pqueue postfix )) ) { switch ( ptoken−>type ) { case OPERAND: /∗ operands always pushed to stack ∗/ push(&stack values , ptoken ) ; break ; case OPERATOR: 2
  • 5. /∗ pop operands from stack ∗/ for ( i = 0; i < op operands [ ptoken−>value . op code ] ; i++) { i f ( ( pvalue = pop(& stack values )) ) { operands [ i ] = pvalue−>value . operand ; f r e e ( pvalue ) ; /∗ done with token ∗/ else } goto e r r o r ; } /∗ process operands according to opcode ∗/ /∗ note operands are popped in reverse order ∗/ switch ( ptoken−>value . op code ) { case ADD: value . operand = operands [1]+ operands [ 0 ] ; break ; case SUBTRACT: value . operand = operands [1] − operands [ 0 ] ; break ; case MULTIPLY: value . operand = operands [ 1 ] ∗ operands [ 0 ] ; break ; case DIVIDE : value . operand = operands [ 1 ] / operands [ 0 ] ; break ; case NEGATE: value . operand = −operands [ 0 ] ; } /∗ push new token with operator r e s u l t to stack ∗/ push(&stack values , new token (OPERAND, value ) ) ; default : f r e e ( ptoken ) ; /∗ f r e e token ∗/ break ; } } /∗ return value i s on top of stack ( should be l a s t value on stack ) ∗/ i f ( stack values ) ans = stack values −>value . operand ; cleanup : /∗ f r e e any remaining tokens ∗/ while ( ( ptoken = dequeue ( pqueue postfix )) ) f r e e ( ptoken ) ; while ( ( pvalue = pop(& stack values )) ) f r e e ( pvalue ) ; return ans ; e r r o r : fputs ( "Error evaluating the expression.n" , s t d e r r ) ; goto cleanup ; } (b) Now, an infix calculator really is not complete if parentheses are not allowed. So, in this part, update the function infix to postfix() to handle parentheses as we discussed in class. Note: your postfix queue should contain no parentheses tokens. Turn in a printout of your code, along with a printout showing the output from your program for a few test cases utilizing parentheses. 3
  • 6. Answer: Here’s an implementation: (only function infix to postfix() shown) /∗ c r e a t e s a queue of tokens in p o s t f i x order from a queue of tokens in i n f i x order ∗/ /∗ postcondition : returned queue contains a l l the tokens , and pqueue infix should be empty ∗/ struct token queue i n f i x t o p o s t f i x ( struct token queue ∗ pqueue infix ) { /∗ TODO: construct postfix −ordered queue from i n f i x −ordered queue ; a l l tokens from i n f i x queue should be added to p o s t f i x queue or freed ∗/ p expr token stack top = NULL, ptoken ; struct token queue queue postfix ; queue postfix . front = queue postfix . back = NULL; for ( ptoken = dequeue ( pqueue infix ) ; ptoken ; ptoken = dequeue ( pqueue infix )) { switch ( ptoken−>type ) { case OPERAND: /∗ operands added d i r e c t l y to p o s t f i x queue ∗/ enqueue(&queue postfix , ptoken ) ; break ; case OPERATOR: /∗ operator added to stack , a f t e r operators of higher precedence are moved to queue ∗/ while ( stack top && stack top −>type == OPERATOR && ( op precedences [ stack top −>value . op code ] > op precedences [ ptoken−>value . op code ] | | ( op precedences [ stack top −>value . op code ] == op precedences [ ptoken−>value . op code ] && o p a s s o c i a t i v i t y [ op precedences [ ptoken−>value . op code ] ] == LEFT) ) ) enqueue(&queue postfix , pop(&stack top ) ) ; push(&stack top , ptoken ) ; break ; case LPARENS: /∗ pushed to operator stack ∗/ push(&stack top , ptoken ) ; break ; case RPARENS: /∗ pop operators o f f stack u n t i l l e f t parentheses reached ∗/ f r e e ( ptoken ) ; /∗ parentheses not included in p o s t f i x queue ∗/ while ( ( ptoken = pop(&stack top )) ) { i f ( ptoken−>type == LPARENS) { f r e e ( ptoken ) ; break ; } enqueue(&queue postfix , ptoken ) ; } } } while ( stack top ) /∗ pop remaining operators o f f stack ∗/ enqueue(&queue postfix , pop(&stack top ) ) ; return queue postfix ; } Problem 6.2 A useful data structure for storing lots of strings is the “trie.” This tree structure has the special property that a node’s key is a prefix of the keys of its children. For instance, if we associate a node with the string “a,” that node may have a child node with the key “an,” which in turn may have a child node “any.” When many strings share a common prefix, this structure is a very inexpensive 4
  • 7. way to store them. Another consequence of this storage method is that the trie supports very fast searching – the complexity of finding a string with m characters is O(m). (root) pointer array of childre � � � �� � � � �� "a" "c" � � � �� � � � �� "an" "ca" � � � �� � � � �� � � � �� "and" "ant" "cat" Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key of its parent node + its index in that parent’s pointer array of children. In this problem, you will utilize a trie structure and to implement a simple one-way English- to-French dictionary. The trie structure, shown in Figure 6.2-1, consists of nodes, each of which contains a string for storing translations for the word specified at that node and an array of pointers to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may contain multiple translations for the same word; in this case, words should be separated by commas. For example: the word like, which has two meanings, could translate as comme, a preposition, or as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve provided code in prob2.c, which can be downloaded from Stellar. You will need to: • fill in the helper functions new node(), delete node() • complete the function add word(), which adds a word to the trie • complete the function lookup word(), which searches the trie for a word and returns its translation(s) Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of your program running in gdb, with a few example translations to demonstrate functionality. 5
  • 8. Answer: one possible implementation of the four functions is shown below: /∗ a l l o c a t e new node on the heap output : pointer to new node ( must be freed ) ∗/ struct s t r i e n o d e ∗ new node ( void ) { /∗ TODO: a l l o c a t e a new node on the heap , and i n i t i a l i z e a l l f i e l d s to d ef a ul t values ∗/ struct s t r i e n o d e pnode =∗ ( struct s t r i e n o d e ∗) malloc ( sizeof ( struct s t r i e n o d e ) ) ; int i ; pnode−>t r a n s l a t i o n = NULL; for ( i = 0; i < UCHARMAX+1; i++) pnode−>children [ i ] = NULL; return pnode ; } /∗ d e l e t e node and a l l i t s children input : pointer to node to d e l e t e postcondition : node and children are freed ∗/ void delete node ( struct s t r i e n o d e ∗ pnode ) { /∗ TODO: d e l e t e node and a l l i t s children Be sure to f r e e non−n u l l t r a n s l a t i o n s ! Hint : use recursion ∗/ int i ; i f ( pnode−>t r a n s l a t i o n ) f r e e ( pnode−>t r a n s l a t i o n ) ; for ( i = 0; i < UCHARMAX+1; i++) i f ( pnode−>children [ i ] ) delete node ( pnode−>children [ i ] ) ; f r e e ( pnode ) ; } /∗ add word to t r i e , with t r a n s l a t i o n input : word and t r a n s l a t i o n output : non−zero i f new node added , zero otherwise postcondition : word e x i s t s in t r i e ∗/ int add word ( const char ∗ word , char ∗ t r a n s l a t i o n ) { /∗ TODO: add word to t r i e structure I f word ex i st s , append t r a n s l a t i o n to e x i s t i n g s t r i n g Be sure to s t o r e a copy of translation , s i n c e the s t r i n g i s reused by l o a d d i c t i o n a r y () ∗/ struct s t r i e n o d e pnode = proot ;∗ int i , len = s t r l e n ( word ) , inew = 0; unsigned char j ; for ( i = 0; i < len ; i++) { j = word [ i ] ; i f (( inew = ! pnode−>children [ j ] ) ) pnode−>children [ j ] = new node ( ) ; pnode = pnode−>children [ j ] ; } i f ( pnode−>t r a n s l a t i o n ) { /∗ concatenate s t r i n g s ∗/ 6
  • 9. char o l d t r a n s l a t i o n = pnode−>t r a n s l a t i o n ;∗ int oldlen = s t r l e n ( o l d t r a n s l a t i o n ) , newlen = s t r l e n ( t r a n s l a t i o n ) ; pnode−>t r a n s l a t i o n = malloc ( oldlen + newlen + 2 ) ; strcpy ( pnode−>translation , o l d t r a n s l a t i o n ) ; strcpy ( pnode−>t r a n s l a t i o n+oldlen , "," ) ; strcpy ( pnode−>t r a n s l a t i o n+oldlen +1, t r a n s l a t i o n ) ; f r e e ( o l d t r a n s l a t i o n ) ; } else pnode−>t r a n s l a t i o n = strcpy ( malloc ( s t r l e n ( t r a n s l a t i o n )+1) , t r a n s l a t i o n ) ; return inew ; } /∗ search t r i e structure f o r word and return t r a n s l a t i o n s input : word to search output : translation , or NULL i f not found ∗/ char ∗ lookup word ( const char ∗ word ) { /∗ TODO: search t r i e structure f o r word return NULL i f word i s not found ∗/ struct s t r i e n o d e pnode = proot ;∗ int i , len = s t r l e n ( word ) ; unsigned char j ; for ( i = 0; i < len ; i++) { j = word [ i ] ; i f ( ! pnode−>children [ j ] ) return NULL; pnode = pnode−>children [ j ] ; } return pnode−>t r a n s l a t i o n ; } 7