~hamblingreen/practical-cpp

3e0cb25202e89354b14c31764c69b556432dd565 — Bobby Hamblin 8 months ago 25f40f3
exercises from chapters 6 and 7
A 06/auto-grader/Makefile => 06/auto-grader/Makefile +10 -0
@@ 0,0 1,10 @@
# Makefile for program auto-grader

# Turn on debugging
CXXFLAGS = -g

auto-grader:auto-grader.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:auto-grader
	rm $^

A 06/auto-grader/auto-grader.cpp => 06/auto-grader/auto-grader.cpp +62 -0
@@ 0,0 1,62 @@
/************************************************
* auto-grader -- convert numerical grades to	*
*   letter grades according to the following	*
*   table:					*
*						*
*   A: 91-100					*
*   B: 81-90					*
*   C: 71-80					*
*   D: 61-70					*
*   F: 0-60					*
*						*
*   additionally, each grade is given a (+) or	*
*   (-) modifier according to the following:	*
*						*
*   1-3: -					*
*   4-7: blank					*
*   8-0: +					*
************************************************/

#include <iostream>

using namespace std;

int numerical_grade; // input number grade between 0-100
int numerical_grade_modifier; // last digit of grade input
char letter_grade = 'A'; // output A, B, C, D, F
char letter_grade_modifier = '+'; // output (+) or (-)

int main() {
	// get user input
	std::cout << "Enter numerical grade between 0 and 100: ";
	std::cin >> numerical_grade;

	// determine letter grade

	if(numerical_grade <= 90)
		letter_grade = 'B';
	if(numerical_grade <= 80)
		letter_grade = 'C';
	if(numerical_grade <= 70)
		letter_grade = 'D';
	if(numerical_grade <= 60)
		letter_grade = 'F';

	// determine letter grade modifier
	numerical_grade_modifier = numerical_grade % 10; // isolate last digit of numerical grade

	if(letter_grade != 'F') {
		if(numerical_grade_modifier <= 7)
			letter_grade_modifier = ' ';
		if(numerical_grade_modifier <= 3)
			letter_grade_modifier = '-';
	} else {
		letter_grade_modifier = ' ';
	}
		
	if (numerical_grade == 100)
		letter_grade_modifier = '+';

	// print final grade
	std::cout << "The numerical grade " << numerical_grade << "% corresponds to the lettter grade " << letter_grade  << letter_grade_modifier << "\n";
}

A 06/change/Makefile => 06/change/Makefile +10 -0
@@ 0,0 1,10 @@
# Makefile for program change

# Turn on debugging
CXXFLAGS = -g

change:change.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:change
	rm $^

A 06/change/change.cpp => 06/change/change.cpp +40 -0
@@ 0,0 1,40 @@
/************************************************
* change -- calculate and print change given	*
*   integer number of cents between 0 and 100	*
************************************************/

#include <iostream>

using namespace std;

int cents; // input number cents between 0-100
int working_cents; // copy of cents to modify
int quarters; // output number of quarters (25c)
int dimes; // output number of dimes (10c)
int nickels; // output number of nickels (5c)
int pennies; // output number of pennies (1c)

int main() {
	// get user input
	std::cout << "Enter number of cents between 0 and 100: ";
	std::cin >> cents;
	working_cents = cents;

	// calculate quarters
	quarters = working_cents / 25;
	working_cents %= 25;

	// calculate dimes
	dimes = working_cents / 10;
	working_cents %= 10;

	// calculate nickels
	nickels = working_cents / 5;
	working_cents %= 5;

	// calculate pennies
	pennies = working_cents;

	// print final result
	std::cout << cents << " cents is best divided into " << quarters << " quarters, " << dimes << " dimes, " << nickels << " nickels, and " << pennies << " pennies\n";
}

A 06/leap-year/Makefile => 06/leap-year/Makefile +10 -0
@@ 0,0 1,10 @@
# Makefile for program leap-year

# Turn on debugging
CXXFLAGS = -g

leap-year:leap-year.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:leap-year
	rm $^

A 06/leap-year/leap-year.cpp => 06/leap-year/leap-year.cpp +32 -0
@@ 0,0 1,32 @@
/************************************************
* leap-year -- determine whether an input year	*
*   is a leap year or not			*
************************************************/

#include <iostream>

using namespace std;

int year; // input year
bool is_leap_year = false; // determines whether it is a leap year or not

int main() {
	// get user input
	std::cout << "Enter year: ";
	std::cin >> year;

	// determine whether it is a leap year
	if(year % 4 == 0) // if the year is divisible by 4, it is a leap year
		is_leap_year = true;
	if(year % 100 == 0) // unless it is divisible by 100
		is_leap_year = false;
	if(year % 400 == 0) // but if divisible by 400, it still is a leap year
		is_leap_year = true;


	// print whether it is a leap year
	if(is_leap_year)
		std::cout << "The year " << year << " is a leap year\n";
	else if (!is_leap_year)
		std::cout << "The year " << year << " is not a leap year\n";
}

A 06/weekly-pay/Makefile => 06/weekly-pay/Makefile +10 -0
@@ 0,0 1,10 @@
# Makefile for program weekly-pay

# Turn on debugging
CXXFLAGS = -g

weekly-pay:weekly-pay.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:weekly-pay
	rm $^

A 06/weekly-pay/weekly-pay.cpp => 06/weekly-pay/weekly-pay.cpp +38 -0
@@ 0,0 1,38 @@
/************************************************
* weekly-pay -- calculate and print weekly pay	*
*   given the number of hours an employee	*
*   worked in a week and their hourly wage.	*
*   counts overtime (more than 40 hours) as	*
*   1.5x pay					*
************************************************/

#include <iostream>

using namespace std;

float hours_worked;		// hours worked in a week
float overtime_hours;		// hours worked past 40 hours
float hourly_wage;		// hourly wage in USD
float total_pay;		// output total weekly wage

main () {
	// get user input
	std::cout << "Enter number of hours and hourly wage (e.g. 35 15.75): ";
	std::cin >> hours_worked >> hourly_wage;

	if (hours_worked > 40) {
		overtime_hours = hours_worked - 40;
		hours_worked = 40;
	}

	// calculate normal hours worked
	total_pay = hours_worked * hourly_wage;

	// calculate overtime if applicable
	if (hours_worked == 40) {
		total_pay += overtime_hours * (hourly_wage * 1.5);
	}

	// print final result
	std::cout << "The employee's total weekly pay is " << total_pay << "\n";
}

A 06/weekly-pay/weekly-pay.cpp~ => 06/weekly-pay/weekly-pay.cpp~ +42 -0
@@ 0,0 1,42 @@
/************************************************
* weekly-pay -- calculate and print weekly pay	*
*   given the number of hours an employee	*
*   worked in a week and their hourly wage.	*
*   counts overtime (more than 40 hours) as	*
*   1.5x pay					*
************************************************/

#include <iostream>

using namespace std;

float hours_worked;		// hours worked in a week
float overtime_hours;		// hours worked past 40 hours
float hourly_wage;		// hourly wage in USD
float total_pay;		// output total weekly wage

int
main ()
{
  // get user input
  std::cout << "Enter number of hours and hourly wage (e.g. 35 15.75): ";
  std::cin >> hours_worked >> hourly_wage;

  if (hours_worked > 40)
    {
      overtime_hours = hours_worked - 40;
      hours_worked = 40;
    }

// calculate normal hours worked
total_pay = hours_worked * hourly_wage;

// calculate overtime if applicable
  if (hours_worked == 40)
    {
      total_pay += overtime_hours * (hourly_wage * 1.5);
    }

// print final result
  std::cout << "The employee's total weekly pay is " << total_pay << "\n";
}

A 07/count-signs/Makefile => 07/count-signs/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program count-signs

# Turn on debugging
CXXFLAGS = -g

count-signs:count-signs.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:count-signs
	rm $^

run:count-signs
	./$^

A 07/count-signs/count-signs.cpp => 07/count-signs/count-signs.cpp +38 -0
@@ 0,0 1,38 @@
/****************************************
* count-signs -- count signs of numbers	*
*   in a number set			*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: Enter numbers separated by new	*
*   lines. Enter 0 to stop		*
****************************************/

#include <iostream>

using namespace std;

int number; // number to be counted
int positive; // amount of positive numbers in data set
int negative; // amount of negative numbers in data set

int main() {
	std::cout << "count-signs\n";

	// user input loop
	while(1) {
		// receive user input
		std::cout << "Input number to count or 0 to stop: ";
		std::cin >> number;

		// determine whether positive, negative, or 0
		if(number == 0)
			break;
		else if (number > 0)
			positive++;
		else if (number < 0)
			negative++;
	}

	// print results
	std::cout << positive << " positive numbers, " << negative << " negative numbers\n";
}

A 07/count-signs/count-signs.md => 07/count-signs/count-signs.md +14 -0
@@ 0,0 1,14 @@
# count-signs

> Count signs of numbers in a number set
> Users' Specification
> Jan. 13, 2023 Bobby Hamblin

Takes an input of numbers separated by newlines, and counts and outputs
the amount of positive and negative numbers in the input. Input 0 to
stop the list

## Example
> count-signs
> Numbers to count: 68 -47 85 -46 78 -72 -71 -65 29 -85
> 4 positive numbers, 6 negative numbers

A 07/date-arithmetic/Makefile => 07/date-arithmetic/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program date-arithmetic

# Turn on debugging
CXXFLAGS = -g

date-arithmetic:date-arithmetic.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:date-arithmetic
	rm $^

run:date-arithmetic
	./$^

A 07/date-arithmetic/date-arithmetic.cpp => 07/date-arithmetic/date-arithmetic.cpp +53 -0
@@ 0,0 1,53 @@
/****************************************
* date-arithmetic -- calculate time	*
*   between two input dates		*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: Input the two dates in YYYY MM *
*   DD format at the prompt. e.g.	*
*   > 1989 04 25
*   > 2024 01 13			*
*   to quit, enter a 0 for any day,	*
*   month, or year in either input date	*
****************************************/

#include <iostream>

using namespace std;

int start_year;
int start_month;
int start_day;

int end_year;
int end_month;
int end_day;

int calculated_year;
int calculated_month;
int calculated_day;

int main() {
	std::cout << "date-arithmetic\n";
	// main loop
	while(true) {
		// receive user input
		std::cout << "Enter the first date in YYYY MM DD format: ";
		std::cin >> start_year >> start_month >> start_day;
		std::cout << "Enter the second date in YYYY MM DD format: ";
		std::cin >> end_year >> end_month >> end_day;

		// exit condition
		if(start_year == 0 || start_month == 0 || start_day == 0 || end_year == 0 || end_month == 0 || end_day == 0)
			return(0);

		// calculate time between dates
		calculated_year = end_year - start_year;
		calculated_month = end_month - start_month;
		calculated_day = end_day - end_month;

		// print result
		std::cout << "Between the dates provided, there are " << calculated_year << " years, " << calculated_month << " months, and " << calculated_day << " days.\n";
	}
}


A 07/date-arithmetic/date-arithmetic.md => 07/date-arithmetic/date-arithmetic.md +20 -0
@@ 0,0 1,20 @@
# date-arithmetic

> Perform date arithmetic  
> Users' Specification  
> Jan. 13, 2023 Bobby Hamblin  

Date Arithmetic calculates time between two input dates

When the program is run, the user is prompted to input two dates and  
outputs the number of days, months, and years between them. This  
program uses the YYYY-MM-DD standard.

To quit the program, enter 0 for any date

## Example
> date-arithmetic  
> Enter the first date in YYYY MM DD format: **1989 04 25**  
> Enter the second date in YYYY MM DD format: **2024 01 13**  
> Between the dates specified, there are 34 years, 8 months, and 19 days


A 07/english-to-metric/Makefile => 07/english-to-metric/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program english-to-metric

# Turn on debugging
CXXFLAGS = -g

english-to-metric:english-to-metric.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:english-to-metric
	rm $^

run:english-to-metric
	./$^

A 07/english-to-metric/english-to-metric.cpp => 07/english-to-metric/english-to-metric.cpp +51 -0
@@ 0,0 1,51 @@
/****************************************
* english-to-metric -- convert english	*
*   units to metric			*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: Input the amount to convert	*
*   and the initial unit like so	*
*   > 20 f				*
*   available unit options are listed	*
*   below				*
*   f - foot				*
*   p - pound				*
*   d - fahrenheit			*
*   to quit, enter an input amount of 0	*
*   followed by any unit		*
****************************************/

#include <iostream>

using namespace std;

float amount; // amount to be converted
char unit; // unit to convert
float converted_amount; // amount after conversion

int main() {
	std::cout << "english-to-metric\n";
	// main loop
	while(true) {
		// receive user input
		std::cout << "Enter an amount to convert followed by a unit (e.g. 10 f): ";
		std::cin >> amount >> unit;

		// calculate and print converted amount
		if(amount == 0) {
			return(0);
		} else if(unit == 'f') {
			converted_amount = amount * 0.3048;
			std::cout << converted_amount << " meters\n";
		} else if(unit == 'p') {
			converted_amount = amount * 0.4536;
			std::cout << converted_amount << " kilograms\n";
		} else if(unit == 'd') {
			converted_amount = (amount - 32) / 1.8;
			std::cout << converted_amount << " degrees celsius\n";
		} else {
			std::cout << "Invalid unit, try again\n";
			continue;
		}
	}
}

A 07/english-to-metric/english-to-metric.md => 07/english-to-metric/english-to-metric.md +34 -0
@@ 0,0 1,34 @@
# english-to-metric

> Convert English units to Metric (SI) units
> Users' Specification
> Jan. 13, 2023 Bobby Hamblin

English-to-metric does as the name suggests, it converts inferior  
english units to metric units. Save yourself the trouble of Google-ing  
a conversion chart or calculator ever again by using this program.  

When the program is run, the user is prompted to input the amount and  
unit seperated by a space like so  

> Enter the amount to convert followed by the type of unit: 10 m

All available unit options are in the table below

| Operator	| English	| Metric	| Factor	|
| ------------- | ------------- | ------------- | ------------- |
| f		| foot		| meter		| 0.3048	|
| p		| pound		| kilogram	| 0.4536	|
| d		| fahrenheit	| celsius	| (x - 32)/1.8	|

To quit the program, enter 0 for the amount followed by any unit operator

## Example
> english-to-metric
> Enter the amount to convert followed by the type of unit (e.g. 10 f): **20 f**
> 6.0957 meters
> Enter the amount to convert followed by the type of unit (e.g. 10 f): **155.2 p**
> 70.3987 kilograms
> Enter the amount to convert followed by the type of unit (e.g. 10 f): **72 d**
> 22.2222 degrees celsuis


A 07/prime/Makefile => 07/prime/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program prime

# Turn on debugging
CXXFLAGS = -g

prime:prime.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:prime
	rm $^

run:prime
	./$^

A 07/prime/prime.cpp => 07/prime/prime.cpp +45 -0
@@ 0,0 1,45 @@
/****************************************
* prime -- determine whether a number	*
*   is prime or not			*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: Input the number to check, or	*
*   0 to exit				*
****************************************/

#include <iostream>

using namespace std;

int number; // number to check
bool is_prime = true; // identifies whether number is prime or not

int main() {
	std::cout << "prime\n";
	// main loop
	while(true) {
		// receive user input
		std::cout << "Number to check: ";
		std::cin >> number;

		// exit condition
		if(number == 0)
			return(0);

		// calculate and print converted amount
		for(int i = 2; i < number; i++) {
			if(number % i == 0) {
				is_prime = false;
				break;
			} else {
				is_prime = true;
			}
		}

		// print result
		if(is_prime)
			std::cout << number << " is prime\n";
		else
			std::cout << number << " is not prime\n";
	}
}

A 07/prime/prime.md => 07/prime/prime.md +22 -0
@@ 0,0 1,22 @@
# prime

> Determine whether an input number is prime
> Users' Specification
> Jan. 13, 2023 Bobby Hamblin

Determines whether an input number is prime by iterating through each
possible factor and determining whether that factor multiplies evenly
into the input number.

When the program is run, the user is prompted to input the number to
check. If the input number is 0, the program exits.

## Example
> prime
> Number to check: **10**
> 10 is not prime
> Number to check: **19**
> 10 is prime
> Number to check: **1928402**
> 1928402 is not prime


A 07/sales-tax/Makefile => 07/sales-tax/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program sales-tax

# Turn on debugging
CXXFLAGS = -g

sales-tax:sales-tax.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:sales-tax
	rm $^

run:sales-tax
	./$^

A 07/sales-tax/sales-tax.cpp => 07/sales-tax/sales-tax.cpp +42 -0
@@ 0,0 1,42 @@
/****************************************
* sales-tax -- apply sales tax		*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: On startup, input the sales	*
*   tax percentage. Then, input the	*
*   dollar amount to modify at the	*
*   prompt. Enter an input amount of $0	*
*   to quit				*
****************************************/

#include <iostream>

using namespace std;

float tax_percentage; // tax percentage to apply
float amount; // amount of money to tax

int main() {
	std::cout << "sales-tax\n";

	// get sales tax percentage
	std::cout << "Sales tax percentage to apply (e.g 6.2): ";
	std::cin >> tax_percentage;
	tax_percentage /= 100;

	// main loop
	while(true) {
		// receive user input
		std::cout << "$";
		std::cin >> amount;

		// exit if amount 0
		if(amount == 0) {
			return(0);
		}

		// calculate and print sales tax
		amount += amount * tax_percentage;
		printf("%.2f\n", amount);
	}
}

A 07/sales-tax/sales-tax.md => 07/sales-tax/sales-tax.md +22 -0
@@ 0,0 1,22 @@
# sales-tax

> Apply sales tax to an amount of money
> Users' Specification
> Jan. 13, 2023 Bobby Hamblin

Applies sales tax to an amount specified by the user. Upon being run,
the proram initially asks for the sales tax percentage and then enters
a loop where the user inputs an amount and is returned the amount with
sales tax applied.

Enter an amount of $0 to exit.

## Example
> sales-tax
> Sales tax percentage to apply: 6
> $**85**
> Taxed: $90.10
> $**8.91**
> Taxed: $9.44
> $**38.19283**
> Taxed: $40.48

A 07/serial-estimator/Makefile => 07/serial-estimator/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program serial-estimator

# Turn on debugging
CXXFLAGS = -g

serial-estimator:serial-estimator.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:serial-estimator
	rm $^

run:serial-estimator
	./$^

A 07/serial-estimator/serial-estimator.cpp => 07/serial-estimator/serial-estimator.cpp +72 -0
@@ 0,0 1,72 @@
/****************************************
* serial-estimator -- calculate serial	*
*   data transfer speed			*
* Author: Bobby Hamblin			*
*   <hamblingreen@hotmail.com>		*
* Usage: Enter the amount of data to be	*
*   transferred, followed by the baud	*
*   rate of your serial connection	*
*   > 400 M 960				*
*   These unit options are valid:	*
*   B - byte				*
*   K - kilobyte			*
*   M - megabyte			*
*   G - gigabyte			*
*   T - terabyte			*
*   To quit, enter 0 for either integer	*
****************************************/

#include <iostream>

using namespace std;

float bytes; // amount of data to calculate
char unit; // unit of bytes
float baud_rate; // baud rate of serial connection
int seconds; // time transfer will take in seconds

int minutes;
int hours;
int days;
int years;

int main() {
	std::cout << "serial-estimator\n";
	// main loop
	while(true) {
		// receive user input
		std::cout << "Enter the amount of data to be transferred and the baud rate (e.g. 400 M 950): ";
		std::cin >> bytes >> unit >> baud_rate;

		// calculate adjusted byte size for unit
		if(bytes <= 0 || baud_rate <= 0) {
			return(0);
		} else if(unit == 'B') {
			bytes = bytes; // don't adjust bytes for unit
		} else if(unit == 'K') {
			bytes = bytes * 1000; // kilobyte
		} else if(unit == 'M') {
			bytes = bytes * 1000000; // megabyte
		} else if(unit == 'G') {
			bytes = bytes * 1000000000; // gigabyte
		} else if(unit == 'T') {
			bytes = bytes * 1000000000000; // terabyte
		} else {
			std::cout << "Invalid unit, try again\n";
			continue;
		}

		// calculate estimated time
		years = (bytes / baud_rate) / 31536000;
		seconds = (int)(bytes / baud_rate) % 31536000;
		days = seconds / 86400;
		seconds %= 86400;
		hours = seconds / 3600;
		seconds %= 3600;
		minutes = seconds / 60;
		seconds %= 60;

		// print output
		std::cout << years << " years, " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << "\n";
	}
}

A 07/serial-estimator/serial-estimator.md => 07/serial-estimator/serial-estimator.md +40 -0
@@ 0,0 1,40 @@
# serial-estimator

> Estimate serial data transmission time
> Users' Specification
> Jan. 13, 2023 Bobby Hamblin

Calculates how long it will take to send a file over a serial cable  
given the file size and baud rate

When the program is run, the user is prompted to input the amount of  
data to be transferred in any byte unit up to terabytes, followed by  
the baud rate of your serial connection. Example below.

> 400 M 960

All available unit options are in the table below

| Operator	| Unit		| Byte value	|
| ------------- | ------------- | ------------- |
| B		| byte		| 1		|
| K		| kilobyte	| 1000		|
| M		| megabyte	| 1000^2	|
| G		| gigabyte	| 1000^3	|
| T		| terabyte	| 1000^4	|

To quit the program, enter 0 for either integer

## Example
> serial-estimator
> Enter the amount of data to be transferred followed by the baud rate (e.g. 400 M 960): **100 B 115200**
> 0 seconds
> Enter the amount of data to be transferred followed by the baud rate (e.g. 400 M 960): **96 K 1800**
> 53 seconds
> Enter the amount of data to be transferred followed by the baud rate (e.g. 400 M 960): **580 M 9600**
> 16 hours, 46 minutes, 56 seconds
> Enter the amount of data to be transferred followed by the baud rate (e.g. 400 M 960): **3.2 G 128000**
> 6 hours, 56 minutes, 40 seconds
> Enter the amount of data to be transferred followed by the baud rate (e.g. 400 M 960): **5 T 56000**
> 2 years, 303 days, 9 hours, 35 minutes, 12 seconds