Sie sind auf Seite 1von 3

COE116L Practical Exam 1

Instructions:
1) Add a new project PractExam1 to your solution.
2) Include the header files given, you will use them in Problems 1 and 2.
All classes for problems 2 should use Stack and Queue ADT classes.
3) Zip the project for collection.
===========================
Problem No. 1
Filename: PractExam1_1.cpp
When a communications site transmits a message through a packet-switching networ
k,
it does not send the message as a continuous stream of data. Instead, it divides
the
message into pieces called packets. These packets are sent through the network t
o a
receiving site, which reassembles the message. Packets may be transmitted to the
receiving site along different paths. As a result, they are likely to arrive out
of
sequence. In order for the receiving site to reassemble the message correctly, e
ach
packet must include the relative position of the packet within the message.
For example, if we break the message A SHORT MESSAGE into packets five
characters long and preface each packet with a number denoting the packet s positi
on
in the message, the result is the following set of packets.
Packet 1: A SHO
Packet 2: RT ME
Packet 3: SSAGE
No matter in what order these packets arrive, a receiving site can correctly rea
ssemble
the message by placing the packets in ascending order based on their position
numbers.
Create a program that reassembles the packets contained in a text file and
outputs the corresponding message. Your program should use the Ordered List
ADT to assist in reassembling the packets in a message. Assume that each
packet in the message file contains a position number and five characters
from the message (the packet format shown in the preceding paragraph). Base
your program on the following declarations.
const int packetSize = 6;

// Number of characters in a packet


// including null ( \0 ) terminator

struct DataType
{
int position; // Packet s position w/in the message
char body[packetSize]; // Characters in the packet
int key () const
{ return position;
};

} // Returns the key field

Test your program with five messages which are separated by newlines in the text
file
message.dat.
********************************************************************************
**************************
2. Problem No. 2
Filename: PractExam1_2.cpp
Infix expressions are the form of mathematical notation most people
are used to, for instance 3 + 4 or 3 + 4 * (2 - 1). The shunting-yard algorit
hm
is a method for parsing mathematical expressions specified in infix notation.
This was designed by Edsger Dijkstra. Implement the Shunting-yard algorithm i
n a class.
Test your program with five infix expressions which are separated by newlines
in the text file
infixExpressions.text.
Shunting-yard algorithm
***********************
1) While there are tokens to be read:
A. Read a token.
B. If the token is a number, then add it to the output queue.
C. If the token is a function token, then push it onto the stack.
D. If the token is a function argument separator (e.g., a comma):
. Until the token at the top of the stack is a left parenthesis,
pop operators off the stack onto the output queue. If no left
parentheses are encountered, either the separator was misplaced
or parentheses were mismatched.
E. If the token is an operator, o1, then:
* while there is an operator token, o2, at the top of the stack, and
either o1 is left-associative and its precedence is less
than or equal to that of o2, or o1 is right-associative and its
precedence is less than that of o2, pop o2 off the stack,
onto the output queue;
* push o1 onto the stack.
F. If the token is a left parenthesis, then push it onto the stack.
G. If the token is a right parenthesis:
* Until the token at the top of the stack is a left parenthesis,
pop operators off the stack onto the output queue.
* Pop the left parenthesis from the stack, but not onto the output queu
e.
* If the token at the top of the stack is a function token, pop it
onto the output queue.
* If the stack runs out without finding a left parenthesis, then there
are mismatched parentheses.
2) When there are no more tokens to read:
While there are still operator tokens in the stack:
* If the operator token on the top of the stack is a parenthesis, then
there are mismatched parentheses.
* Pop the operator onto the output queue.

3) Exit.

Das könnte Ihnen auch gefallen