0% found this document useful (0 votes)
45 views13 pages

Assignment No 1 of Data Structure Note - Attempt Al...

The document provides C++ code to define classes for military time and temperature, and functions to read temperature data, display samples and a bar graph, and get temperatures within a time range. It defines classes MilitaryTime and Temperature with setter/getter methods. Functions read input, check for valid data, and display output like temperatures from a start to end time entered by the user.

Uploaded by

rj usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views13 pages

Assignment No 1 of Data Structure Note - Attempt Al...

The document provides C++ code to define classes for military time and temperature, and functions to read temperature data, display samples and a bar graph, and get temperatures within a time range. It defines classes MilitaryTime and Temperature with setter/getter methods. Functions read input, check for valid data, and display output like temperatures from a start to end time entered by the user.

Uploaded by

rj usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

 

Home Study tools  My courses  My books My folder Career Life

home / study / engineering / computer science / computer science questions and answers / 

assignment...

Question: Assignment no 1 of Data Structure Note: Attempt all


questions using C++ and oop, data structure: ...
Find solutions for your homework

​answer please asap


me

Show transcribed image text

Answers
Anonymous
answered this

CODE :--

//Header file section

#include <iostream>

#include <string>

#include <vector>

using namespace std;

//Create a class name, "MilitaryTime"

class MilitaryTime

//Declare attributes

private:

int hours;

int minutes;

int seconds;

string twoDigits(const int num) const;

public:

//Meber functions

void setHours(const int hrs);

void setMinutes(const int mins);

void setSeconds(const int secs);

int getHours() const;

int getMinutes() const;

int getSeconds() const;

string toString() const;

};

//Create a class name, "Temperature" for

//get details of temperature

class Temperature

//Declare attributes
private:

int temp;

string label;

MilitaryTime time;

public:

//Member functions

void setTemp(const int tmp);

int getTemp() const;

void setLabel(const string lbl);

string getLabel() const;

void setTime(const MilitaryTime t);

MilitaryTime getTime() const;

string toString() const;

};

// FUNCTION PROTOTYPES

void readTempData(vector<Temperature>& temps, const


int HIGHEST);

void printTempSamples(vector<Temperature> temps);

void printBarGraph(vector<Temperature> temps, const int


HIGHEST);

void getTimeLimit(vector<Temperature> temps);

string readLabelData(vector<Temperature> temps);

bool isUpper(char c);

bool isChoice(vector<Temperature> temps, string str);

bool checkTimeValid(vector<Temperature> temps,


MilitaryTime time);

int getTemp(const int HIGHEST);

MilitaryTime getTime(vector<Temperature> temps);

MilitaryTime readTime();

//Program begins a main method

int main()

// Declare constant

const int HIGHEST = 100;

vector<Temperature> temps;
// Call the function in TASK 6

//in the question requirements

readTempData(temps, HIGHEST);

// Call functions in TASK 14, TASK 15, and TASK 16

//in the question requirements

printTempSamples(temps);

printBarGraph(temps, HIGHEST);

getTimeLimit(temps);

return 0;

// CLASS MEMBER FUNCTION DEFINITIONS

//setter and getter methods

void MilitaryTime::setHours(const int hrs)

hours = hrs;

void MilitaryTime::setMinutes(const int mins)

minutes = mins;

void MilitaryTime::setSeconds(const int secs)

seconds = secs;

int MilitaryTime::getHours() const

return hours;

int MilitaryTime::getMinutes() const

return minutes;

}
int MilitaryTime::getSeconds() const

return seconds;

//Function definition of twoDigits: This function is

//used to display the two digits numbers on screen

string MilitaryTime::twoDigits(const int number) const

string str = "";

if (number < 10)

str = '0' + to_string(number);

else

str += to_string(number);

return str;

//Function definition of toString

string MilitaryTime::toString() const

return twoDigits(hours) + ":" + twoDigits(minutes)

+ ":" + twoDigits(seconds);

void Temperature::setTemp(const int tmp)

this->temp = tmp;

//Function definition of getTemp

int Temperature::getTemp() const

{
return this->temp;

//Function definition of setLabel

void Temperature::setLabel(const string lbl)

this->label = lbl;

//Function definition of getLabel

string Temperature::getLabel() const

return this->label;

//Function definition of setTime

void Temperature::setTime(const MilitaryTime t)

this->time.setHours(t.getHours());

this->time.setMinutes(t.getMinutes());

this->time.setSeconds(t.getSeconds());

//Function definition of getTime

MilitaryTime Temperature::getTime() const

return time;

//Function definition of toString

string Temperature::toString() const

return label + ", " + to_string(temp) + "F, "

+ time.toString();

//FUNCTION DEFINITIONS

//Function definition of readTempData: This function is used


// to prompt and read input data from the user

void readTempData(vector<Temperature>& temps, const


int HIGHEST)

//Initialize the variable

int tempSample = 1;

//Create a Temperature class's object

Temperature t1;

//Prompt and read sample temperatures

cout << "Enter today's temperatures: " << endl;

while (true)

cout << "Sample #" << tempSample << "


-\n";

string label = readLabelData(temps);

if (label.length() == 0)

break;

else

t1.setLabel(label);

t1.setTemp(getTemp(HIGHEST));

t1.setTime(getTime(temps));

temps.push_back(t1);

cout << t1.toString() << endl;

tempSample += 1;

cout << endl;

cin.ignore();

//Function definition of readLabelData: This function is

//used to take temperature vector as parametr


//Prompt for label of temperature

string readLabelData(vector<Temperature> temps)

string label;

cout << "Label: ";

getline(cin, label);

if (label.empty()) {

return label;

//Check if it is valid label, then return label

while (label.length() != 2 ||

!isUpper(label[0]) ||

!isUpper(label[1]) ||

isChoice(temps, label)) {

cout << "Invalid label. Try again\n";

cout << "Label: ";

cin >> label;

return label;

//Function definition of isUpperLablel: This

//function to check a charcter is uppercase

bool isUpper(char c) {

return isupper(c);

//Function definition of isChoice: This function

//check the label passed already present in

//temperature vector

bool isChoice(vector<Temperature> temps, string str)

for (int i = 0; i < temps.size(); i++)

{
if (temps[i].getLabel() == str)

return true;

return false;

//Function definition of getTemp: This function

//return temperature within range

int getTemp(const int HIGHEST)

int temp;

cout << "Temp: ";

cin >> temp;

while (temp <= 0 || temp > HIGHEST) {

cout << "Invalid temp, Try again.\n";

cout << "Temp: ";

cin >> temp;

return temp;

//Function definition of getTime: This function

//get valid time and return it

MilitaryTime getTime(vector<Temperature> temps)

MilitaryTime time = readTime();

while (!checkTimeValid(temps, time))

time = readTime();

return time;

}
//Function definition of readTime: This function

//prompt for time and verify it is in the

// range and return time

MilitaryTime readTime()

MilitaryTime time;

int hr, min, sec;

cout << "Time (hrs, min, secs): ";

cin >> hr >> min >> sec;

while (hr < 0 || hr>23 || min < 0 || min>59 || sec


< 0 || sec>59) {

cout << "Invalid time. Try again.\n";

cout << "Time (hrs, min, secs): ";

cin >> hr >> min >> sec;

time.setHours(hr);

time.setMinutes(min);

time.setSeconds(sec);

return time;

//Function definition of checkTimeValid. It is used to

//check is the same time already present in temperature


vector

bool checkTimeValid(vector<Temperature> temps,


MilitaryTime time)

for (int i = 0; i < temps.size(); i++)

if (temps[i].getTime().toString() == time.toString())

return false;

return true;

//Function definition of printTempSamples: This function

//is used to display all temperature details

void printTempSamples(vector<Temperature> temps)

cout << "\nSamples:\n";

for (int i = 0; i < temps.size(); i++)

cout << temps[i].toString() << endl;

//Function definition of printBarGraph for

//display bar graph of the temperatures

void printBarGraph(vector<Temperature> temps, const int


HIGHEST)

int steps = HIGHEST / 5;

cout << "\nBar Graph:\n";

for (int i = steps; i <= HIGHEST; i += steps)

int prevStep = i - steps;

cout << i << ": ";

for (int j = 0; j < temps.size(); j++)

if (temps[j].getTemp() <= i && temps[j].getTemp() >


prevStep)

cout << "*";

cout << endl;

//Function definition of getTimeLimit for display

//all temperatures within start and end time

//user enterd and an error check always start

//time less than end time

void getTimeLimit(vector<Temperature> temps)

cout << "\nEnter start and end time:\n";

MilitaryTime sTime = readTime();

MilitaryTime eTime = readTime();

while (true)

if (sTime.getHours() > eTime.getHours())

cout << "Invalid start and end times. Try again\n";


}

else if (sTime.getHours() == eTime.getHours()

&& sTime.getMinutes() > eTime.getMinutes())

cout << "Invalid start and end times. Try again\n";


}

else if (sTime.getHours() == eTime.getHours()

&& sTime.getMinutes() == eTime.getMinutes()

&& sTime.getSeconds() > eTime.getSeconds())

cout << "Invalid start and end times. Try again\n";


}

else

for (int i = 0; i < temps.size(); i++)

if (temps[i].getTime().getHours() >= sTime.getHours()

&& temps[i].getTime().getHours() <=


eTime.getHours()

&& temps[i].getTime().getMinutes() >=


sTime.getMinutes()

&& temps[i].getTime().getMinutes() <=


eTime.getMinutes()

&& temps[i].getTime().getSeconds() >=


sTime.getSeconds()

&& temps[i].getTime().getSeconds() <=


eTime.getSeconds())

cout << temps[i].toString() << endl;

break;

MilitaryTime sTime = readTime();

MilitaryTime eTime = readTime();

0 Comments

Was this answer helpful? 0

COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK
CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like