~hamblingreen/practical-cpp

25f40f32d6b89e83b194cf1b6a055fbd0956d23a — Bobby Hamblin 8 months ago f04f601
add exercises through chapter 5
R hello/Makefile => 02/hello/Makefile +0 -0
R hello/hello.cpp => 02/hello/hello.cpp +0 -0
A 04/print-e/Makefile => 04/print-e/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for the program print-e

# Turn on debugging
CXXFLAGS=-g
print-e:print-e.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:print-e
	rm $^

A 04/print-e/print-e.cpp => 04/print-e/print-e.cpp +20 -0
@@ 0,0 1,20 @@
/****************************************
* print-e -- print a block letter E to	*
*   the terminal			*
****************************************/

#include <iostream>
using namespace std;

int main()
{
	std::cout << "*****\n";
	std::cout << "*\n";
	std::cout << "*\n";
	std::cout << "***\n";
	std::cout << "*\n";
	std::cout << "*\n";
	std::cout << "*****\n";
	return(0);
}


A 04/print-hello/Makefile => 04/print-hello/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for the program print-hello

# Turn on debugging
CXXFLAGS=-g
print-hello:print-hello.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:print-hello
	rm $^

A 04/print-hello/print-hello.cpp => 04/print-hello/print-hello.cpp +20 -0
@@ 0,0 1,20 @@
/****************************************
* print-hello -- print "Hello" in block	*
*   letters the terminal		*
****************************************/

#include <iostream>
using namespace std;

int main()
{
	std::cout << "*   *  *****  *      *      *****\n";
	std::cout << "*   *  *      *      *      *   *\n";
	std::cout << "*   *  *      *      *      *   *\n";
	std::cout << "*****  ***    *      *      *   *\n";
	std::cout << "*   *  *      *      *      *   *\n";
	std::cout << "*   *  *      *      *      *   *\n";
	std::cout << "*   *  *****  *****  *****  *****\n";
	return(0);
}


A 04/print-me/Makefile => 04/print-me/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for the program print-me

# Turn on debugging
CXXFLAGS=-g
print-me:print-me.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:print-me
	rm $^

A 04/print-me/print-me.cpp => 04/print-me/print-me.cpp +20 -0
@@ 0,0 1,20 @@
/****************************************
* print-me -- print personal info to	*
*   the terminal			*
****************************************/

#include <iostream>
using namespace std;

std::string name = "Bobby Hamblin";
std::string ssn = "801-390-209";
std::string birthdate = "05/11/2006";

int main()
{
	std::cout << "Name: " << name << "\n";
	std::cout << "Social Security Number: " << ssn << "\n";
	std::cout << "Birthdate: " << birthdate << "\n";
	return(0);
}


A 04/rectangle/Makefile => 04/rectangle/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for the program rectangle

# Turn on debugging
CXXFLAGS=-g
rectangle:rectangle.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:rectangle
	rm $^

A 04/rectangle/rectangle.cpp => 04/rectangle/rectangle.cpp +20 -0
@@ 0,0 1,20 @@
/****************************************
* rectangle -- calculate and print the	*
*   area of a rectangle			*
****************************************/

#include <iostream>
using namespace std;

int width1 = 3;
int height1 = 5;
float width2 = 6.8;
float height2 = 2.3;

int main()
{
	std::cout << "Area of rectangle 1: " << width1 * height1 << "\n";
	std::cout << "Area of rectangle 2: " << width2 * height2 << "\n";
	return(0);
}


A 05/celsius-to-fahrenheit/Makefile => 05/celsius-to-fahrenheit/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program celsius-to-fahrenheit

# Turn on debugging
CXXFLAGS = -g
celsius-to-fahrenheit:celsius-to-fahrenheit.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:celsius-to-fahrenheit
	rm $^

A 05/celsius-to-fahrenheit/celsius-to-fahrenheit.cpp => 05/celsius-to-fahrenheit/celsius-to-fahrenheit.cpp +17 -0
@@ 0,0 1,17 @@
/************************************************
* celsius-to-fahrenheit -- convert from C to F	*
************************************************/

#include <iostream>

using namespace std;

float input; // input in degrees C
float output; // output in degrees F

int main() {
	std::cout << "Enter degrees in Celsius: ";
	std::cin >> input;
	output = input * (9.0 / 5.0) + 32;
	std::cout << input << " Celsius is " << output << " Fahrenheit\n";
}

A 05/hours-to-minutes/Makefile => 05/hours-to-minutes/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program hours-to-minutes

# Turn on debugging
CXXFLAGS = -g
hours-to-minutes:hours-to-minutes.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:hours-to-minutes
	rm $^

A 05/hours-to-minutes/hours-to-minutes.cpp => 05/hours-to-minutes/hours-to-minutes.cpp +19 -0
@@ 0,0 1,19 @@
/************************************************
* hours-to-minutes -- convert hours and minutes	*
*   to minutes					*
************************************************/

#include <iostream>

using namespace std;

int hours; // input time in hours
int minutes; // input time in minutes
int output; // output time in minutes

int main() {
	std::cout << "Enter time in hours and seconds separated by a space (e.g. 1 30): ";
	std::cin >> hours >> minutes;
	output = (hours * 60) + minutes;
	std::cout << hours << ":" << minutes << " hours is equal to " << output << " minutes\n";
}

A 05/kph-to-mph/Makefile => 05/kph-to-mph/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program kph-to-mph

# Turn on debugging
CXXFLAGS = -g
kph-to-mph:kph-to-mph.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:kph-to-mph
	rm $^

A 05/kph-to-mph/kph-to-mph.cpp => 05/kph-to-mph/kph-to-mph.cpp +18 -0
@@ 0,0 1,18 @@
/************************************************
* kmph-to-mph -- convert from kilometers per	*
*   hour (kmph) to miles per hour (mph)		*
************************************************/

#include <iostream>

using namespace std;

double kilometers_per_hour; // input in kmph
double miles_per_hour; // output in mph

int main() {
	std::cout << "Enter speed in kilometers per hour (kmph): ";
	std::cin >> kilometers_per_hour;
	miles_per_hour = kilometers_per_hour * 0.6213712;
	std::cout << kilometers_per_hour << " kilometers per hour is equivalent to  " << miles_per_hour << " miles per hour\n";
}

A 05/minutes-to-hours/Makefile => 05/minutes-to-hours/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program minutes-to-hours

# Turn on debugging
CXXFLAGS = -g
minutes-to-hours:minutes-to-hours.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:minutes-to-hours
	rm $^

A 05/minutes-to-hours/minutes-to-hours.cpp => 05/minutes-to-hours/minutes-to-hours.cpp +20 -0
@@ 0,0 1,20 @@
/************************************************
* minutes-to-hours -- convert minutes to hours	*
*   and minutes					*
************************************************/

#include <iostream>

using namespace std;

int input; // input time in minutes
int hours; // output time in hours
int minutes; // output time in minutes

int main() {
	std::cout << "Enter time in minutes (e.g. 90): ";
	std::cin >> input;
	hours = input / 60;
	minutes = input % 60;
	std::cout << input << " minutes is equal to " << hours << ":" << minutes << " hours\n";
}

A 05/perimeter/Makefile => 05/perimeter/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program perimeter

# Turn on debugging
CXXFLAGS = -g
perimeter:perimeter.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:perimeter
	rm $^

A 05/perimeter/perimeter.cpp => 05/perimeter/perimeter.cpp +21 -0
@@ 0,0 1,21 @@
/************************************************
* perimeter -- calculate and print perimeter of *
*   a rectangle given width and height		*
************************************************/

#include <iostream>

using namespace std;

float width; // input width of rectangle
float height; // input height of rectangle
float perimeter; // output perimeter

int main() {
	std::cout << "Enter rectangle width: ";
	std::cin >> width;
	std::cout << "Enter rectangle height: ";
	std::cin >> height;
	perimeter = 2.0 * (width + height);
	std::cout << "The perimeter of a rectangle with dimensions of " << width << " by " << height  << " is " << perimeter << "\n";
}

A 05/sphere-volume/Makefile => 05/sphere-volume/Makefile +9 -0
@@ 0,0 1,9 @@
# Makefile for program sphere-volume

# Turn on debugging
CXXFLAGS = -g
sphere-volume:sphere-volume.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:sphere-volume
	rm $^

A 05/sphere-volume/sphere-volume.cpp => 05/sphere-volume/sphere-volume.cpp +19 -0
@@ 0,0 1,19 @@
/************************************************
* sphere-volume -- calculate and print the	*
*   volume of a sphere				*
************************************************/

#include <iostream>

using namespace std;

const float PI = 3.14159; // pi constant
float input; // input radius in units
float output; // output in units cubed

int main() {
	std::cout << "Enter radius: ";
	std::cin >> input;
	output = (4.0 / 3.0) * PI * (input * input * input);
	std::cout << "The volume of a sphere with a radius of " << input << " is " << output << " units cubed\n";
}