Programming: Week 3 (cont.)
|
Ex. 3 Write a Here is the program:
#include
#include
#include
class TemperatureConverter
{
public:
void SetTemp(std::string input)//set methods - provide data entered by the user to the Class
{
int n = input.length();
char sist = input.at(n - 1);
float temp_part = std::stof(input.erase(n - 1, 1));
temperature = temp_part;
};
void SetSource(std::string input)
{
int n = input.length();
char sist = input.at(n - 1);
source = sist;
};
void SetTarget(char convert_to)
{
target = convert_to;
};
float GetTemp()//Get methods - provide data in member variables to other methods in the Class
{
return temperature;
};
char GetSource()
{
return source;
};
char GetTarget()
{
return target;
};
float C2K()//Convert methods - convert temperature values from one scale to another
{
return GetTemp() + 273.15f;
};
float K2C()
{
return GetTemp() - 273.15f;
};
float K2F()
{
return 9 * GetTemp() / 5 - 459.67f;
};
float F2K()
{
return 5 * GetTemp() / 9 + 255.37f;
};
float F2C()
{
return 5 * (GetTemp() - 32) / 9;
};
float C2F()
{
return 9 * GetTemp() / 5 + 32;
};
void Convert()//determine which conversion method to run and call PrintResult() method
{
if (GetTarget() == GetSource())
std::cout << "Target scale same as source scale!" << std::endl;
else switch (GetTarget())
{
case 'C':
{
if (GetSource() == 'F') PrintResult(F2C());
else PrintResult(K2C());
break;
};
case 'F':
{
if (GetSource() == 'C') PrintResult(C2F());
else PrintResult(K2F());
break;
};
case 'K':
{
if (GetSource() == 'F') PrintResult(F2K());
else PrintResult(C2K());
break;
};
default:
std::cout << "Wrong input!" << std::endl;
break;
};
};
void PrintResult(float converted)//output the converted value to the user
{
std::cout << GetTemp() << " " << GetSource() << " is " << converted << " " << GetTarget() << std::endl;
};
private:
float temperature;
char source;
char target;
};
void main(){
TemperatureConverter temperature;
std::string entered_temp;
char to_scale = '0';
std::cout << "*******************************************************" << std::endl;
std::cout << "*** TEMPERATURE CONVERTER ***" << std::endl;
std::cout << "*******************************************************" << "nn";
std::cout << "Allowed temperature conversions:n" << std::endl;
std::cout << "(C) to Celsius" << std::endl;
std::cout << "(F) to Fahrenheit" << std::endl;
std::cout << "(K) to Kelvin" << std::endl;
std::cout << "nEnter a temperature followed by a degrees symbol, e.g.,n-16.45C for -16.45 degrees Celsius.n" << std::endl;
std::getline(std::cin, entered_temp);
while (entered_temp.at(0) != 'q')
{
temperature.SetTemp(entered_temp);
temperature.SetSource(entered_temp);
std::cout << "nChoose scale to convert to, e.g. enter K to convert to Kelvin." <> to_scale;
temperature.SetTarget(to_scale);
temperature.Convert();
std::cout << std::endl << "nEnter another temperature or 'q' to quit." << std::endl;
std::cin.ignore();
std::getline(std::cin, entered_temp);
}
}
main() Function The The program asks the user to input a string ending in C, F or K. This makes the program aware which scale the initial temperature is in. The program then asks for one more input, which scale to convert to – again, C, F or K is expected. Both inputs are passed to the set-methods of the class. Then The statements that read user input, pass it to the class and call The variable Class Structure Within the class, three private member variables are declared (lines 96-98): The three set-methods, The three get-methods, Each of the six convert-methods, Once the value is known, it is passed to the
String Opeartions When doing these exercises, I often need to manipulate strings. Here are some useful string methods (a
std::string test = "abcde"; test.erase(2,2); std::cout << test; will print out
Final Comments If the user follows instructions and does not enter wrong data the program works as designed. I did not include data validation because that’s too much time-consuming and not the point of this exercise. Also, you can understand how the original temperature entered by the user, e.g., Ex. 4 Below is a code fragment that declares a class:
class Time{
public:
Time(int hour, int minute, int second);
unsigned int GetSecondsSinceMidnight();
private:
unsigned int m_hours;
unsigned int m_minutes;
unsigned int m_seconds;
};
Finish defining the class. Implement the methods. Make sure parameters are validated. Here is the program:
#include
#include
#include // to format how integers are output with std::cout <<
class Time
{
public:
Time(int hour, int minute, int second)//This is the constructor of the class!
{
SetTime(hour,minute,second);
};
void SetTime(int hour, int minute, int second)
{
m_hours = hour;
m_minutes = minute;
m_seconds = second;
};
unsigned int GetSecondsSinceMidnight()
{
return 3600 * m_hours + 60 * m_minutes + m_seconds;
};
private:
unsigned int m_hours;
unsigned int m_minutes;
unsigned int m_seconds;
};
int main()
{
unsigned int hh, mm, ss;
bool cont = true;
while (cont)
{
std::cout <> hh;
std::cout <> mm;
std::cout <> ss;
if (hh > 23 || mm > 59 || ss > 59)
{
std::cout << "One or more data is wrong!n";
continue;
};
std::cout << std::setfill('0') << "Current time is " << std::setw(2) << hh << ":" << std::setw(2) << mm << ":" << std::setw(2) << ss << std::endl;
Time time(hh, mm, ss);
std::cout << "Number of seconds elapsed since midnight: " << time.GetSecondsSinceMidnight() << std::endl;
std::cout <> cont;
}
return 0;
}
Since the first method in the class has the same name as the class itself, we must assume that is the class’s constructor. It takes three arguments which it must receive from the
The Validation itself checks if the number of hours, minutes and seconds does not exceed 23, 59 and 59, respectively. Since the variables To make sure the values are output in the usual electronic clock format, i.e., 00:00:00, I need to format the integers being output. For that, I include
and not
|