# Data Structures

Computing

## Quiz 4 :Standard I - O and String Classes

Question Type
A rational number is of the form a / b , where a and b are integers with b ? 0. Develop and test a class for processing rational numbers. The class should have a numerator data member and a denominator data member. It should read and display all rational numbers in the format a / b ; for output, just display a if the denominator is 1. The following examples illustrate the operations that should be provided.
Free
Essay

Program Plan:
Rnumber.h
• Create the class "Rnumber"
• Declare the required member function as public
• Declare the required member variables as private
Rnumber.cpp
• Definition of "Reduce()" function in which it checks if the numerator and denominator value is less than zero
• Definition of "Mixed number()" with two parameter
• "gcd()" function in which it stores the numerator by denominator value.
• Function definition for "lcd()"
Main.cpp
• Declaration of main
• "help()" function is declared
• The for loop gets executed unconditionally until if the user enter quit
• Switch case operation is performed for the various condition
• If the switch case operation is not satisfied then "default" operation are performed.
Main.cpp
/ / Required header files are included
#include
using namespace std;
#include "Rnumber.h"
/ / Declaration of the main
int main()
{
/ / Displays the given content if user enter h
const char HELP[] =
"\n The input can be entered in the form of"
"\n a / b. (e.g. 1 / 2)"
"\n"
"\n The valid inputs given as:"
"\n a + b addition (lowest terms)"
"\n a - b subtraction (lowest terms)"
"\n a * b multiplication (lowest terms)"
"\n a / b division (lowest terms)"
"\n a I inverse of a"
"\n a M mixed number of a"
"\n a R lowest term reduction of a"
"\n a G greatest common divisor of a"
"\n a L b lowest common denominator"
"\n a b condition is true if a is less than b"
"\n a = b condition is true if a is less than or equal to b"
"\n a b condition is true if a is greater than b"
"\n a = b condition is true if a is greater than or equal to b"
"\n a = b condition is true if a is equal to b";
/ / If user entered invalid input condition then this statement is displayed in output
const char INVALID_INPUT[] = "*** Invalid input operation..Press h for help ***\n";
/ / Display the content
cout "\n This program is a rational number calculator.\n";
/ / unconditional for loop
for (;;)
{
/ / Print statement which displays the in output
cout "\nInput (h=help,q=quit): ";
/ / check the user input is equal to 'h'
if (cin.peek() == 'h')
{
/ / get() function is used to get the value
cin.get();
/ / print the output
cout HELP endl;
}
/ / check the user input is equal to 'q'
else if (cin.peek() == 'q')
{
/ / get() function to get the user value
cin.get();
return 0;
}
else
{
Rnumber a, b;
char op;
/ / Get the input from the user
cin a op;
/ / switch case operation is based on the operations given
switch (op)
{
case '+':
cin b;
cout a + b endl;
break;
case '-':
cin b;
cout a - b endl;
break;
case '*':
cin b;
cout a * b endl;
break;
case ' / ':
cin b;
cout a / b endl;
break;
case 'i': case 'I':
cout a.inverse() endl;
break;
case 'm': case 'M':
long i;
a.mixedNumber(i, b);
if (i 0)
cout i " + ";
cout b endl;
break;
case 'r': case 'R':
a.reduce();
cout a endl;
break;
case 'g': case 'G':
cout a.gcd() endl;
break;
case 'l': case 'L':
cin b;
cout lcd(a, b) endl;
break;
case ' ':
if (cin.peek() == '=')
{
cin op b;
cout (a = b ? "True" : "False") endl;
}
else
{
cin b;
cout ((a
}
break;
case ' ':
if (cin.peek() == '=')
{
cin op b;
cout ((a = b) ? "True" : "False") endl;
}
else
{
cin b;
cout ((a b) ? "True" : "False") endl;
}
break;
case '=':
cin b;
cout ((a == b) ? "True" : "False") endl;
break;
case 'a': case 'A':
cin op;
if (op == '+')
{
cin b op;
if (op == '=')
{
Rnumber c;
cin c;
cout (c - b) / a endl;
}
else
/ / It displays invalid input
cout INVALID_INPUT;
}
else
/ / It displays invalid input
cout INVALID_INPUT;
break;
default:
/ / prints the output as invalid input
cout INVALID_INPUT;
}
}
/ / checks if the condition fails
if (cin.fail())
{
/ / print the output as invalid input
cout INVALID_INPUT;
/ / clear in output screen
cin.clear();
}
/ / ignore until newline
cin.ignore(9999, '\n');
}
}
Rnumber.h:
/ / Required header files are included
#ifndef RNUMBER
#define RNUMBER
#include
/ / Declaration of the class
class Rnumber
{
/ / Access specifier
public:
/ / calling of constructor
Rnumber()
{}
/ / constructor with one parameter
Rnumber(long Integer)
{
myNumerator = Integer; myDenominator = 1;
}
/ / constructor with two parameter
Rnumber(long numer, long denom)
{
myNumerator = numer; myDenominator = denom;
}
/ / definition of inverse() function
Rnumber inverse() const
{
/ / return the class reference
return Rnumber(myDenominator, myNumerator);
}
/ / definition of numerator() function
long numerator() const
{
/ / return the myNumerator
return myNumerator;
}
/ / definition of denominator() function
long denominator() const
{
/ / return the myDenominator
return myDenominator;
}
/ / reduce function which reduces to lower terms
void reduce();
/ / This function will produce the value in mixed number
void mixedNumber(long whole, Rnumber fraction) const;
/ / This function will produce greatest common divisor value
long gcd() const;
/ / This function will produce the lowest common denominator value
friend long lcd(const Rnumber a, const Rnumber b);
Rnumber operator+=(const Rnumber a);
friend Rnumber operator+(const Rnumber a, const Rnumber b);
/ / The operator overloading function for subtraction using the operator '-=' and '-'
Rnumber operator-=(const Rnumber a);
friend Rnumber operator-(const Rnumber a, const Rnumber b);
/ / The operator overloading function for multiplication using the operator '*=' and '*'
Rnumber operator*=(const Rnumber a);
friend Rnumber operator*(const Rnumber a, const Rnumber b);
/ / The operator overloading function for division using the operator ' / =' and ' / '
Rnumber operator / =(const Rnumber a);
friend Rnumber operator / (const Rnumber a, const Rnumber b);
friend int operator==(const Rnumber a, const Rnumber b);
friend int operator!=(const Rnumber a, const Rnumber b);
friend int operator (const Rnumber a, const Rnumber b);
friend int operator (const Rnumber a, const Rnumber b);
friend int operator =(const Rnumber a, const Rnumber b);
friend int operator =(const Rnumber a, const Rnumber b);
friend ostream operator (ostream out, const Rnumber a);
friend istream operator (istream in, Rnumber a);
/ / Access specifier
private:
/ / myNumerator and myDenominator is declared in private
long myNumerator,
myDenominator;
};
#endif
Rnumber.cpp:
/ / Required header files are included
#include
using namespace std;
#include "Rnumber.h"
/ / calling of constructor
void Rnumber::reduce()
{
/ / negative value is assigned as zero
int negative = 0;
/ / checks if the myNumerator value is less than zero
if (myNumerator 0)
{
/ / assign non negative value to negative
negative = !negative;
/ / assign negative of myNumerator value to myNumerator
myNumerator = -myNumerator;
}
/ / checks if the myDenominator is less than zero
if (myDenominator 0)
{
/ / assign non negative value to negative
negative = !negative;
/ / assign negative of myDenominator value to myDenominator
myDenominator = -myDenominator;
}
/ / gcd() function is assigned with long gcdNumDenom
long gcdNumDenom = gcd();
/ / numberator value divided by the gcd() function
myNumerator / = gcdNumDenom;
/ / denominator value divided by the gcd() function
myDenominator / = gcdNumDenom;
/ / checks if negative
if (negative)
/ / negative of myNumerator value is assigned with myNumerator
myNumerator = -myNumerator;
}
/ / Definition of mixedNumber()
void Rnumber::mixedNumber(long whole, Rnumber fraction) const
{
/ / the whole value stores the myNumerator by myDenominator value
whole = myNumerator / myDenominator;
/ / The fraction value is stored here
fraction = Rnumber(myNumerator % myDenominator, myDenominator);
}
/ / Definition of gcd()
long Rnumber::gcd() const
{
/ / Declaration for myNumerator,myDenominator and r value
long aa = myNumerator,
bb = myDenominator,
r = aa % bb;
/ / loop which checks whether r not equal to zero
while (r != 0)
{
/ / assign bb value to aa
aa = bb;
/ / assign r value to bb
bb = r;
/ / assing remainder value of (aa % bb) to r
r = aa % bb;
}
/ / return bb value
return bb;
}
/ / function definition of lcd
long lcd(const Rnumber a, const Rnumber b)
{
/ / assign a. myDenominator value to aa
long aa = a.myDenominator,
/ / assign b.myDenominator value to bb
bb = b.myDenominator,
/ / assign aa value to a1
a1 = aa,
/ / assign bb value to b1
b1 = bb;
/ / this loop runs till a1 is equal to b1
while (a1 != b1)
{
/ / this checks if a1 is greater than b1
if (a1 b1)
/ / update b1 value by adding b1 and bb
b1 += bb;
else
/ / update a1 value by adding a1 and aa
a1 += aa;
}
/ / return a1 value
return a1;
}
Rnumber Rnumber::operator+=(const Rnumber a)
{
/ / update myNumerator value
myNumerator = myNumerator * a.myDenominator +
myDenominator * a.myNumerator;
/ / update myDenominator value by multiplying myDenominator value and
/ / a.myDenominator
myDenominator *= a.myDenominator;
/ / call the function reduce()
reduce();
/ / return "this" value
return *this;
}
Rnumber operator+(const Rnumber a, const Rnumber b)
{
/ / assign a to answer
}
Rnumber Rnumber::operator-=(const Rnumber a)
{
/ / update myNumerator value
myNumerator = myNumerator * a.myDenominator -
myDenominator * a.myNumerator;
/ / update myDenominator value
myDenominator *= a.myDenominator;
/ / call the function reduce()
reduce();
/ / return "this" value
return *this;
}
Rnumber operator-(const Rnumber a, const Rnumber b)
{
/ / assing a value to answer
}
Rnumber Rnumber::operator*=(const Rnumber a)
{
myNumerator *= a.myNumerator;
myDenominator *= a.myDenominator;
reduce();
return *this;
}
Rnumber operator* (const Rnumber a, const Rnumber b)
{
/ / assing a value to answer
}
Rnumber Rnumber::operator / =(const Rnumber a)
{
/ / update myNumerator value by multiplying myNumerator
/ / a.myDenominator
myNumerator *= a.myDenominator;
/ / update myDenominator value by multiplying myDenominator
/ / a.myNumerator
myDenominator *= a.myNumerator;
/ / call the function'reduce()'
reduce();
/ / return 'this' value
return *this;
}
Rnumber operator / (const Rnumber a, const Rnumber b)
{
/ / assign a value to answer
}
int operator==(const Rnumber a, const Rnumber b)
{
/ / assign a value to aRed
Rnumber aRed = a,
/ / assign b value to bRed
bRed = b;
/ / call the function 'aRed.reduce()'
aRed.reduce();
/ / call the function 'bRed.reduce()'
bRed.reduce();
/ / return the value
return (aRed.myNumerator == bRed.myNumerator)
(aRed.myDenominator == bRed.myDenominator);
}
int operator!=(const Rnumber a, const Rnumber b)
{
/ / assign a value to 'aRed' and 'bRed'
Rnumber aRed = a,
bRed = b;
/ / call the function 'aRed.reduce()'
aRed.reduce();
/ / call the function 'bRed.reduce()'
bRed.reduce();
/ / return the value
return (aRed.myNumerator != bRed.myNumerator)
(aRed.myDenominator != bRed.myDenominator);
}
int operator (const Rnumber a, const Rnumber b)
{
/ / update left value
long left = a.myNumerator * b.myDenominator,
/ / update right value
right = a.myDenominator * b.myNumerator;
/ / returns the value if left is less then right
return left right;
}
int operator (const Rnumber a, const Rnumber b)
{
/ / update left value
long left = a.myNumerator * b.myDenominator,
/ / update right value
right = a.myDenominator * b.myNumerator;
/ / return the value in which left is greater than right
return left right;
}
int operator =(const Rnumber a, const Rnumber b)
{
/ / update left value
long left = a.myNumerator * b.myDenominator,
/ / update right value
right = a.myDenominator * b.myNumerator;
/ / return the value in which left is less than or equal to right
return left = right;
}
int operator =(const Rnumber a, const Rnumber b)
{
/ / update left value
long left = a.myNumerator * b.myDenominator,
/ / update right value
right = a.myDenominator * b.myNumerator;
/ / return the value in which left value is greater than or equal to right
return left = right;
}
ostream operator (ostream out, const Rnumber a)
{
/ / prints the a.myNumerator value
out a.myNumerator;
/ / checks the condition if a.myDenominator is not equal to 1
if (a.myDenominator != 1)
/ / prints the a.myDenominator value
out ' / ' a.myDenominator;
/ / returns the out
return out;
}
istream operator (istream in, Rnumber a)
{
/ / gets the value of a.myNumerator
in a.myNumerator;
/ / checks if peek value is equal to ' / '
if (in.peek() == ' / ')
{
/ / gets the value
in.get();
/ / get the denominator value
in a.myDenominator;
}
/ / otherwise
else
/ / denominator value is assigned as 1
a.myDenominator = 1;
/ / returns in
return in;
}
Sample Output:
This program is a rational number calculator.
Input (h=help,q=quit): 12 / 24R
1 / 2
Input (h=help,q=quit): 12 / 24G
12
Input (h=help,q=quit): 1 / 2 + 2 / 3
7 / 6
Input (h=help,q=quit): 1 / 2 -1 / 2
0

Tags
Choose question tag
Write a driver program to test the weather statistics class of Exercise 15.
Free
Essay

Problem Plan:
• enum is created for "weather condition"
o The weather condition is displayed according to the user
• Declaration of class with name "Weather"
o Private access specifier is created to declare the required variables
o Public access specifier is created for required member function
• Definition of "input()" function
o "input()" function is used to get the required input from the user
• Definition of "display()" function
o It displays the input given by the user
o Switch case condition to check for the weather condtion
• Declaration of main
o Object is created for "Weather"
o Call the "input()" function
o Call the "display()" function
Program:
/ *********************************************************** Program to find the Weather Statistics * ********************************************************** /
/ / Required header files are included
#include
#include
using namespace std;
/ / weather condition is created with "enum"
enum weather_condition
{
/ / weather conditions
Skies, Partly_cloudy, Cloudy, Stormy
};
/ / Declaration of class
class weather
{
/ / Access specifier
private:
/ * city, state, country are declared with "character" data type* /
char city[20], state[20], country[20];
/ * date, month and year are declared with "integer" data type* /
int date, month, year;
/ * temperature, bpressure and w are declared with "integer" data type* /
int temperature, bpressure, w;
/ / Access specifier
public:
/ / input() function which is used to get the input
void input();
/ / display() function is used to print the given input
void display();
};
/ / Definition of input() function
void weather::input()
{
/ / print statement to get name
cout "Enter City:";
/ / gets the user input
cin city;
/ / print statement to get state
cout "Enter State:";
/ / gets the user input
cin state;
/ / print statement to get country
cout "Enter Country:";
/ / gets the user input
cin country;
/ / print statement to get temperature
cout "Enter temperature:";
/ / gets the user input
cin temperature;
/ / print statement to get barometric pressure
cout "Enter Barometric pressure:";
/ / gets the user input
cin bpressure;
/ / print statement to get weather date
cout "Enter weather date:";
/ / gets the user input
cin date;
/ / print statement to get weather month
cout "Enter weather month:";
/ / gets the user input
cin month;
/ / print statement to get weather year
cout "Enter weather year:";
/ / gets the user input
cin year;
/ / print statement to get choice of weather condition
/ / gets the user input
cin w;
}
/ / Definition of "display()" function
void weather::display()
{
/ / print statement to get city
cout "\ncity:" city;
/ / print statement to get state
cout "\nState:" state;
/ / print statement to get weather date
cout "\nWeather_date:" date " / " month " / " year;
/ / print statement to get country
cout "\nCountry:" country;
/ / print statement to get temperature
cout "\nTemperature (in degrees):" temperature;
/ / print statement to get bpressure
cout "\nBarometric pressure (in pascal):" bpressure;
/ *checks for switch case condition for weather condition* /
switch (w)
{
case Skies:
cout "\nWeather condition:Skies\n";
break;
case Partly_cloudy:
cout "\nWeather condition:Partly cloudy\n";
break;
case Cloudy:
cout "\nWeather condition:Cloudy\n";
break;
case Stormy:
cout "\nWeather condition:Stormy\n";
break;
}
}
/ / Declaration of main
int main()
{
/ / object is created for weather
weather w;
/ / call the input function
w.input();
/ / call the display function
w.display();
/ / get the current time
time_t rawtime;
/ / declare struct tm pointer
struct tm * timeinfo;
/ / get the raw time from the system
time( rawtime);
/ / store the time in the variable 'timeinfo'
timeinfo = localtime( rawtime);
/ / display the time
cout "Time " asctime(timeinfo);
}
Sample Output:
Enter City:karaikal
Enter State:Pondicherry
Enter Country:India
Enter temperature:23
Enter Barometric pressure:173
Enter weather date:27
Enter weather month:1
Enter weather year:2017
city:karaikal
State:Pondicherry
Weather_date:27 / 1 / 2017
Country:India
Temperature (in degrees):23
Barometric pressure (in pascal):173
Weather condition:Skies
Time Fri Jan 27 13:02:16 2017

Tags
Choose question tag
Write a driver program to test the class PhoneNumber of Exercise 8.
Free
Essay

Problem Plan:
PhoneNo.cpp:
• Constructor is created for "PhoneNo()" and the required variables are assigned.
• Calling of three parameterized constructor.
• The function is declared for areaCode in which it returns the areaCode value
• The function is declared for localExchange in which it returns the localExchange value
• The function is declared for number in which it returns the number
PhoneNo.h:
• Declaration of the class "PhoneNo"
• Constructor is created for "PhoneNo"
• Declare the required member function as public
• Declare the required member variables as private
Main.cpp:
• Main function is created.
• Object is declared for the class "PhoneNo"
• Function call is made for "printPhoneNo()"
Program:
PhoneNo.cpp:
/ / include the required header files
#include "PhoneNo.h"
/ / calling of the constructor
PhoneNo::PhoneNo()
{
/ / assign '123' to areaCode
areaCode = 123;
/ / assign 'ABCD' to localExchange
localExchange = "ABCD";
/ / assign '987654321' to number
number = 987654321;
}
/ / calling of the 3 parameter constructor
PhoneNo::PhoneNo(int area, string local, int no)
{
/ / assign area to areaCode
areaCode = area;
/ / assign local to localExchange
localExchange = local;
/ / assign no to number
number = no;
}
/ / calling the destructor
PhoneNo::~PhoneNo() {}
/ / calling the function 'setAreaCode'
void PhoneNo::setAreaCode(int area)
{
/ / assign area to areaCode
areaCode = area;
}
/ / calling the function 'setLocalExchange'
void PhoneNo::setLocalExchange(string local)
{
/ / assign local to localExchange
localExchange = local;
}
/ / calling the function 'setNumber'
void PhoneNo::setNumber(int no)
{
/ / assign no to number
number = no;
}
/ / calling the function 'getAreaCode'
int PhoneNo::getAreaCode()
{
/ / return areaCode
return areaCode;
}
/ / calling the function 'getLocalExchage'
string PhoneNo::getLocalExchage()
{
/ / return localExchange
return localExchange;
}
/ / calling the function 'getNumber'
int PhoneNo::getNumber()
{
/ / return number
return number;
}
/ / calling the function 'getNumber'
void PhoneNo::printPhoneNo()
{
/ / print the areaCode
cout areaCode " ";
/ / print localExchange
cout localExchange " ";
/ / print the number
cout number endl;
}
PhoneNo.h:
/ / include the required header files
#ifndef PHONENo_H
#define PHONENo_H
#include
#include
using namespace std;
/ / declaration of the class
class PhoneNo
{
/ / access specifier
public:
/ / constructor with no parameters
PhoneNo();
/ / constructor with three parameters
PhoneNo(int area, string local, int no);
/ / destructor
~PhoneNo();
/ / declaration of the function 'setAreaCode()'
void setAreaCode(int area);
/ / declaration of the function 'setLocalExchange()'
void setLocalExchange(string local);
/ / declaration of the function 'setNumber()'
void setNumber(int no);
/ / declaration of the function 'getAreaCode()'
int getAreaCode();
/ / declaration of the function 'getLocalExchage()'
string getLocalExchage();
/ / declaration of the function 'getNumber()'
int getNumber();
/ / declaration of the function 'printPhoneNo()'
void printPhoneNo();
/ / access specifier
private:
/ * declaration of the variable 'areaCode' with integer datatype * /
int areaCode;
/ * declaration of the variable 'localExchange' with string datatype * /
string localExchange;
/ * declaration of the variable 'number' with integer datatype * /
int number;
};
#endif
TestClasses.cpp:
#include "PhoneNo.h"
#include
#include
using namespace std;
/ / main function
int main()
{
/ / object for the class
PhoneNo number;
/ / display the below content
cout "PhoneNumber with code,localexchange and number has been obtained: ";
/ / calls the function 'printPhoneNo()'
number.printPhoneNo();
/ / return 0
return 0;
}
Output:
PhoneNumber with code,localexchange and number has been obtained:
123 ABCD 987654321

Tags
Choose question tag
Write a driver program to test the class Card of Exercise 12.
Essay
Tags
Choose question tag
Problems 10-12 deal with lines in the Cartesian plane. The point-slope equation of a line with slope m and passing through point P with coordinates ( x 1 , y 1 ) is The slope-intercept equation of a line with slope m and y -intercept b is Write and test a LineSegment class, described by two CartesianPoint endpoints (see Exercises 4 and 10). In addition to the usual operations (such as constructors, input, and output), this class should provide operations to compute a. The midpoint of the line segment joining two points b. The equation of the perpendicular bisector of this line segment
Essay
Tags
Choose question tag
Write a driver program to test the class Date of Exercise 7.
Essay
Tags
Choose question tag
For Exercises 1-6, define the private portion of a class to model the given item. A class PhoneNumber for telephone numbers consisting of area code, local exchange, and number
Essay
Tags
Choose question tag
Write a driver program to test the baseball statistics class of Exercise 14.
Essay
Tags
Choose question tag
Write a driver program to test the class PolarPoint of Exercise 11.
Essay
Tags
Choose question tag
Write a driver program to test the class CartesianPoint of Exercise 10.
Essay
Tags
Choose question tag
Problems 10-12 deal with lines in the Cartesian plane. The point-slope equation of a line with slope m and passing through point P with coordinates ( x 1 , y 1 ) is The slope-intercept equation of a line with slope m and y -intercept b is Write and test a class for a Line, described by its slope and a point on the line, with functions that a. Find the point-slope equation of the line b. Find the slope-intercept equation of the line
Essay
Tags
Choose question tag
Write a driver program to test the personal information class of Exercise 13.
Essay
Tags
Choose question tag
For Exercises 1-6, define the private portion of a class to model the given item. A class Date for dates consisting of a month, day, and year
Essay
Tags
Choose question tag
For Exercises 1-6, define the private portion of a class to model the given item. A class Checker for the position of a checker on a board
Essay
Tags
Choose question tag
A complex number has the form a + bi , where a and b are real numbers and i 2 = ?1. The standard C++ library includes a class complex for processing complex numbers. Examine the structure of this class to find the operations it provides and what data members it uses. Write a program to read two complex numbers and a symbol for an operation and that then performs the indicated operation.
Essay
Tags
Choose question tag
For Exercises 1-6, define the private portion of a class to model the given item. A class PolarPoint for a point ( r , ?) in a polar coordinate system
Essay
Tags
Choose question tag
Write a driver program to test the class Checker of Exercise 9.
Essay
Tags
Choose question tag
Problems 10-12 deal with lines in the Cartesian plane. The point-slope equation of a line with slope m and passing through point P with coordinates ( x 1 , y 1 ) is The slope-intercept equation of a line with slope m and y -intercept b is Write a program to read the point and slope information for two lines and to determine whether they intersect or are parallel. If they intersect, find the point of intersection and also determine whether they are perpendicular. Use the classes CartesianPoint from Exercises 4 and 10 and Line from Problem 11 to represent points and lines, respectively.
Essay