![Programming Language Pragmatics, Fourth Edition](https://www.bartleby.com/isbn_cover_images/9780124104099/9780124104099_largeCoverImage.gif)
Explanation of Solution
Code which supports unary negation and the four standard arithmetic operators:
//include the required header files
#include <cmath>
using std::abs;
using std::round;
#include <iostream>
using std::ostream;
using std::cout;
//definition of class "rational"
class rational
{
//declare the required variables
int num;
int den;
//definition of "gcd" function
int gcd(int i, int j)
{
//set the absolute values
i = abs(i);
j = abs(j);
//check "i" is not equal to "j"
while (i != j)
{
//check "i" is greater than "j"
if (i > j)
/*subtract the values and store the result in "i"*/
i = i - j;
//otherwise
else
/*subtract the values and store the result in "j"*/
j = j - i;
}
//return the "i" value
return i;
}
//definition of "normalize" function
void normalize()
{
//check "den" is less than 0
if (den < 0)
{
//calculate numerator value
num = -num;
//calculate denominator value
den = -den;
}
//call the "gcd" function with a parameters
int g = gcd(num, den);
//calculate numerator value
num /= g;
//calculate denominator value
den /= g;
}
//access specifier
public:
//definition constructor
rational(int n, int d)
{
//set the values
num = n;
den = d;
//call the function
normalize();
}
// definition of constructor
rational(double r, double e)
{
//set the values
num = (int) r;
den = 1;
//check the condition
while (abs(r - ((double)num)/((double)den)) > e)
{
//increment the value
++den;
//calculate numerator value
num = round(den*r);
}
}
//definition of operator "-"
rational operator-()
{
//calculate numerator value
num = -num;
//return the value
return *this;
}
//definition of operator "+"
rational operator+(const rational o)
{
//declare the variable and calculate the value
int p = den * o.den;
//calculate numerator value
num = num * o.den + den * o.num;
//set the value
den = p;
//call the function
normalize();
//return the value
return *this;
}
//definition of operator "-"
rational operator-(const rational o)
{
//declare the variable and calculate the value
int p = den * o.den;
//calculate numerator value
num = num * o.den - den * o.num;
//set the value
den = p;
//call the function
normalize();
//return the value
return *this;
}
//definition of operator "*"
rational operator*(const rational o)
{
//calculate numerator value
num *= o.num;
//calculate denominator value
den *= o.den;
//call the function
normalize();
//return the value
return *this;
}
//definition of operator "/"
rational operator/(const rational o)
{
//calculate numerator value
num *= o.den;
//calculate denominator value
den *= o.num;
//call the function
normalize();
//return the value
return *this;
}
};
Explanation:
- The header files are included in the beginning of the program.
- The “rational” class is defined. Inside the class,
- The “num” and “den” variables are declared.
- The “gcd” function is defined
- Get the absolute values of “i” and “j” and store it in the corresponding variables.
- The “while” loop is used to check “i” is not equal to “j”.
- If the “i” value is greater than “j” value, then subtract the “i” and “j” value and store the result in the “i” variable.
- Otherwise, subtract the “j” and “i” value and store the result in the “j” variable.
- Return the “i” value.
- The “normalize” function is defined.
- If the “den” is less than 0, then calculate the “num” and “den” values.
- Call the “gcd” function with the parameters.
- Calculate the numerator and denominator values.
- The constructor of “rational” is defined with two parameters.
- Set the values
- Call the “normalize” function.
- Again, the constructor of “rational” is defined with another two parameters.
- Set the values.
- Check the “while” condition
- Increment the denominator value
- Calculate the numerator
- The operator “-” is defined
- Calculate the “num” value
- Return the result
- The operator “+” is defined with parameter
- Calculate the “p” and “num” values
- Set the value
- Call the “normalize” function
- Return the result
- The operator “-” is defined with parameter
- Calculate the “p” and “num” values
- Set the value
- Call the “normalize” function
- Return the result
- The operator “*” is defined with parameter
- Calculate the “den” and “num” values
- Call the “normalize” function
- Return the result
- The operator “/” is defined with parameter
- Calculate the “den” and “num” values
- Call the “normalize” function
- Return the result
Want to see more full solutions like this?
Chapter 3 Solutions
Programming Language Pragmatics, Fourth Edition
- can you solve this pleasearrow_forwardIn the previous homework scenario problem below: You have been hired by TechCo to create and manage their employee training portal. Your first task is to develop a program that will create and track different training sessions in the portal. Each training session has the following properties: • A session ID (e.g., "TECH101", "TECH205") • A session title (e.g., "Machine learning", "Advanced Java Programming") • A total duration in hours (e.g., 5.0, 8.0) • Current number of participants (e.g., 25) Each session must have at least a session ID and a total duration and must met the following requirements: • The maximum participant for each session is 30. • The total duration of a session must not exceed 10 hours. • The current number of participants should never exceed the maximum number of participants. Design an object-oriented solution to create a data definition class(DDC) and an implementation class for the session object. In the DDC, a session class must include: • Constructors to…arrow_forwardIn the previous homework scenario problem below: You have been hired by TechCo to create and manage their employee training portal. Your first task is to develop a program that will create and track different training sessions in the portal. Each training session has the following properties: • A session ID (e.g., "TECH101", "TECH205") • A session title (e.g., "Machine learning", "Advanced Java Programming") • A total duration in hours (e.g., 5.0, 8.0) • Current number of participants (e.g., 25) Each session must have at least a session ID and a total duration and must met the following requirements: • The maximum participant for each session is 30. • The total duration of a session must not exceed 10 hours. • The current number of participants should never exceed the maximum number of participants. Design an object-oriented solution to create a data definition class(DDC) and an implementation class for the session object. In the DDC, a session class must include: • Constructors to…arrow_forward
- Send me the lexer and parserarrow_forwardHere is my code please draw a transition diagram and nfa on paper public class Lexer { private static final char EOF = 0; private static final int BUFFER_SIZE = 10; private Parser yyparser; // parent parser object private java.io.Reader reader; // input stream public int lineno; // line number public int column; // column // Double buffering implementation private char[] buffer1; private char[] buffer2; private boolean usingBuffer1; private int currentPos; private int bufferLength; private boolean endReached; // Keywords private static final String[] keywords = { "int", "print", "if", "else", "while", "void" }; public Lexer(java.io.Reader reader, Parser yyparser) throws Exception { this.reader = reader; this.yyparser = yyparser; this.lineno = 1; this.column = 0; // Initialize double buffering buffer1 = new char[BUFFER_SIZE]; buffer2 = new char[BUFFER_SIZE]; usingBuffer1 = true; currentPos = 0; bufferLength = 0; endReached = false; // Initial buffer fill fillBuffer(); } private…arrow_forwardIf integer x is divisible by 3, can you prove that ceil(x/2) + floor(x/6) = floor(x/2) + ceil(x/6)arrow_forward
- Draw the NFA for thisarrow_forwardWhat are three examples each of closed-ended, open-ended, and range-of-response questions? thank youarrow_forwardCreate 2 charts using this data. One without using wind speed and one including max speed in mph. Write a Report and a short report explaining your visualizations and design decisions. Include the following: Lead Story: Identify the key story or insight based on your visualizations. Shaffer’s 4C Framework: Describe how you applied Shaffer’s 4C principles in the design of your charts. External Data Integration: Explain the second data and how you integrated it with the Halloween dataset. Compare the two datasets. Attach screenshots of the two charts (Bar graph or Line graph) The Shaffer 4 C’s of Data Visualization Clear - easily seen; sharply defined• who's the audience? what's the message? clarity more important than aestheticsClean - thorough; complete; unadulterated, labels, axis, gridlines, formatting, right chart type, colorchoice, etc.Concise - brief but comprehensive. not minimalist but not verboseCaptivating - to attract and hold by beauty or excellence does it capture…arrow_forward
- How can I resolve the following issue?arrow_forwardI need help to resolve, thank you.arrow_forwardLet the user choose encryption or decryption. For encryption, let user input the key in Hexadecimal number, the plain text in Hexadecimal number, output the ciphertext (in hexadecimal numbers). For decryption, let user input the key in Hexadecimal number, the ciphertext (in hexadecimal numbers), output the decrypted message (Hexadecimal number). Both encryption and decryption should output the different operation results for each round like the following: For example: Round 1: E(R0) = ...... (Hex or Binary) K1 = …… E(Ro) xor K1 = S-box outputs = …… f(Ro1, K1) = ….. L2 =R1 =……. La = Ra Round 2: .....• No Encryption/Decryption libraries or functions provided by the third party are allowed. Submit your program codes to Moodle with the notes of how to compile and run your program.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
![Text book image](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)