A protocol is a set of rules that define some operation. For example, the Intern
ID: 3671288 • Letter: A
Question
A protocol is a set of rules that define some operation. For example, the Internet Protocol (IP)
specifies how messages are routed throughout the internet. In this problem, you are asked to
implement your 1st messaging protocol (MP1). MP1 is a binary protocol that is used to efficiently
encode a set of arithmetic operations using a 4-byte data type (e.g., int). The specification of the
protocol is as follow. Using a 4-byte (32-bit stream), MP1 messages encode the ‘+’, ‘-‘, ‘*’,
‘/’ arithmetic operations in the first two most significant bits of the bit stream as seen below.
The arithmetic operations carried by an MP1 message (defined in Table) are executed using
two operands, a left-hand-side (lhs) operand, and a right-hand-side (rhs) operand, as seen below:
lhs <<operation from bits Table 1>> rhs
MP1 messages encode two 15-bit values used to represent both lhs and rhs operands. The lhs
numeric value is encoded in bits 29 – 15, while the rhs numeric value is encoded in bits 14 – 0.
Thus, the complete encoding of the MP1 message is as seen below.
Design and implement a C++ program that uses the MP1 protocol to perform arithmetic computations on short data types. Your code shall include the following functions:
// This function receives a lhs operand, rhs operand, and operator and packs it into
// a 32-bit stream per the MP1 messaging protocol. The return value is the packed
// 32-bit (uint) stream that can be unpacked/decoded to execute arithmetic operations.
uint packMessage(ushort lhsNumber, ushort rhsNumber, char operation);
// This function receives an MP1 message (uint), unpacks/decodes it, and process it to // return the result of the operation carried within the message. For valid operation
// and data values, please refer to the MP1 protocol specification.
double processMessage(uint message);
In your implementation for both functions above, you shall use a switch statement. Every bit-mask used throughout the program shall be in the form of constant expression. Please note that each operand has a maximum value of 2^15, so your code must perform appropriate error checking before attempting to encode a number in the MP1 message. Your functions shall match exactly the declaration above.
When your program executes, it must do so infinitely, always prompting users to enter the lhs operand, followed by the operation, followed by the rhs operands, as seen below.
On each operand (lhs and rhs) your code shall implement error checks for the conditions presented as shown on figure:
Similarly, your code shall check for invalid input on the operation, ensuring that only the valid values defined in the MP1 protocol are accepted, as seen in Figure:
Inside your main function, your result shall be displayed to the console using the following code.
// TODO: Add code here to request input (lhs/rhs/operation) from user and validate it.
// ...
// At this point, all inputs have been validated, so we can move forward with processing.
cout << " The result of " << lhsOperand << " " << operation << " " << rhsOperand
<< " is " << processMessage( packMessage( lhsOperand, rhsOperand, operation) )
<< endl;
Explanation / Answer
#include <iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
ushort lhs, rhs;
char op;
ushort limit;
uint message;
limit= pow(2,15);
cout<<"enter positive lhs operand";
cin>>lhs;
if(lhs > limit)
{
cout<<"please enter positive operand in valid range";
cin>>lhs;
}
cout<<"enter operator";
cin>>op;
if( op == "+" || op == "-" || op == "*" || op =="/")
continue;
else
cout<<"please enter calid operator from +,-,*,/";
cout<<"enter positive rhs operand";
cin>>rhs;
if(rhs > limit)
{
cout<<"please enter positive operand in valid range";
cin>>rhs;
message =packMeaage(lhs, rhs, op);
double r=processMessage(message);
cout<<"the result of "<<lhs <<op<<rhs<<"is:"<<r;
}
}
uint packMessage(ushort lhs, ushort rhs, char op)
{
uint result;
switch(op)
{
case '+' :
result = lhs + rhs;
break;
case '-':
result = lhs - rhs;
break;
case '*':
result = lhs * rhs;
break;
case '/':
result = lhs / rhs;
break;
default:
cout<<"wrong operator";
}
return result;
}
double processMessage(uint message)
{
double r1;
r1= double(message);
return r1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.