Design and implement a C++ program that uses the MP1 protocol to perform arithme
ID: 3669480 • Letter: D
Question
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:
On each operand (lhs and rhs) your codeshall implement error checks for the conditions presented in Figure 2.
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 3.
Figure 3. Error Handling for Invalid Operations.
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;
// 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.(1)
Explanation / Answer
#include<iostream.h>
#include<conio.h>
int processMessage(ushort lhsNumber, ushort rhsNumber, char operation)
{
return packMessage(lhsNumber, rhsNumber, operation);
}
int packMessage(ushort lhsNumber, ushort rhsNumber, char operation)
{
int a;
switch(operation)
{
case '+':
a=lhsNumber+rhsNumber;
break;
case '-':
a=lhsNumber-rhsNumber;
break;
case '*':
a=lhsNumber*rhsNumber;
break;
case '/':
a=lhsNumber/rhsNumber;
break;
}
return a;
}
void main()
{
int lhsOperand,rhsOperand;
char operation;
while(1)
{
cout<<"Enter positive lhs operand:";
cin>>lhsOperand;
if(lhsOperand<=0 && lhsOperand>32769)
cout<<"Invalid Input";
else
break;
}
while(1)
{
cout<<"Enter operator('+','-','*','/'):";
cin>>operation;
if(operation=='+' || operation=='-' || operation=='*' || operation=='/')
break;
else
cout<<"Invalid Input";
}
while(1)
{
cout<<"Enter positive lhs operand:";
cin>>rhsOperand;
if(rhsOperand<=0 && rhsOperand>32769)
cout<<"Invalid Input";
else
break;
}
cout << " The result of " << lhsOperand << " " << operation << " " << rhsOperand
<< " is " << processMessage( packMessage( lhsOperand, rhsOperand, operation) )
<< endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.