ASDV-Cpp/Assignments/MP1_CalebFontenot/main.cpp

203 lines
7.4 KiB
C++

2024-02-28 16:11:37 +07:00
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/cppFiles/main.cc to edit this template
*/
/*
* File: main.cpp
* Author: caleb
*
* Created on February 26, 2024, 11:30AM
*/
2024-03-01 09:38:35 +07:00
#include <iomanip>
2024-02-28 16:11:37 +07:00
#include <cstdlib>
2024-03-01 09:38:35 +07:00
#include <iostream>
#include <tuple>
#include <cmath>
2024-03-04 11:54:27 +07:00
#include <boost/algorithm/string.hpp>
#include <regex>
#include <vector>
2024-02-28 16:11:37 +07:00
using namespace std;
/*
*
*/
2024-03-01 09:38:35 +07:00
int milesPerGallon(int gallons, int milesDriven) {
return milesDriven / gallons;
}
2024-02-28 16:11:37 +07:00
2024-03-01 09:38:35 +07:00
std::tuple<double, double> maleFemalePercentages(int maleStudents, int femaleStudents) {
2024-03-04 11:54:27 +07:00
int totalStudents = maleStudents + femaleStudents;
2024-03-01 09:38:35 +07:00
double malePercentage = (double) maleStudents / totalStudents;
double femalePercentage = (double) femaleStudents / totalStudents;
return {malePercentage, femalePercentage};
}
2024-02-28 16:11:37 +07:00
2024-03-01 09:38:35 +07:00
double celciusToFahrenheit(double celcius) {
return 9.0 / 5.0 * celcius + 32.0;
}
2024-03-04 11:54:27 +07:00
std::string getMonthName(int month) {
std::string returnString = "";
switch (month) {
case 1:
returnString = "January";
break;
case 2:
returnString = "February";
break;
case 3:
returnString = "March";
break;
case 4:
returnString = "April";
break;
case 5:
returnString = "May";
break;
case 6:
returnString = "June";
break;
case 7:
returnString = "July";
break;
case 8:
returnString = "August";
break;
case 9:
returnString = "September";
break;
case 10:
returnString = "October";
case 11:
returnString = "November";
break;
case 12:
returnString = "December";
break;
}
return returnString;
}
void prettyPrintSalesTax(int month, int year, int total, double totalIncome, double stateSalesTax, double countySalesTax, double totalSalesTax) {
printf("%s %d\n", getMonthName(month).c_str(), year);
printf("---------------------------------------\n");
printf("Total collected:%*c$ %.2f\n", 5, totalIncome);
printf("Sales:%*c$ %.2f\n", 16, stateSalesTax);
printf("County Sales tax:%*c$ %.2f\n", 5, countySalesTax);
printf("Total sales tax:%*c$ %.2f\n", 6, totalSalesTax);
}
void monthlySalesTax(int month, int year, double total) {
double totalIncome = total / 1.06;
double stateSalesTax = total * 0.04;
double countySalesTax = total * 0.02;
double totalSalesTax = stateSalesTax + countySalesTax;
prettyPrintSalesTax(month, year, total, totalIncome, stateSalesTax, countySalesTax, totalSalesTax);
}
bool is_integer(const std::string & s) {
return std::regex_match(s, std::regex("[-+]?[0-9]+"));
}
void mathTutor() {
string userInput = "";
int rand1, rand2 = 0;
do {
cout << "Enter Q/q to quit." << endl;
rand1 = (rand() % 100);
rand2 = (rand() % 100);
cout << "What's " << rand1 << " + " << rand2 << "?" << endl;
cin >> userInput;
boost::algorithm::to_lower(userInput);
if (is_integer(userInput)) {
if (rand1 + rand2 == stoi(userInput)) {
cout << "You are correct!" << endl;
} else {
cout << "Sorry, the answer is " << (rand1 + rand2) << ". Try again." << endl;
}
}
} while (userInput.compare("q") != 0);
}
void prettyPrintMonthlyPayments(double payment, double rate, double numOfPayments, double loanAmount, double annualRate, double amountPaid, double amountPaidBack) {
printf("---------------------------------------\n");
printf("Loan amount:%*c$ %.2f\n", 15, loanAmount);
printf("Monthly Interest Rate:%*c%.0f%%\n", 14, rate);
printf("Number of Payments:%*c%.0f\n", 17, numOfPayments);
printf("Monthly Payment:%*c$ %.2f\n", 12, payment);
printf("Amount Paid Back:%*c$ %.2f\n", 11, amountPaidBack);
printf("Interest Paid:%*c$ %.2f\n", 14, amountPaid);
}
void monthlyPayments(double annualRate, double numOfPayments, double loanAmount, double amountPaid, double amountPaidBack) {
double rate = (annualRate / 12.0);
double topHalf = rate * pow((1.0 + rate), numOfPayments);
double bottomHalf = pow((1.0 + rate), numOfPayments) - 1.0;
double payment = (topHalf / bottomHalf) * loanAmount;
prettyPrintMonthlyPayments(payment, rate, numOfPayments, loanAmount, annualRate, amountPaid, amountPaidBack);
}
// Function to replace placeholders in the story with user inputs
std::string replacePlaceholders(const std::string& story, const std::vector<std::string>& inputs) {
std::string result = story;
std::regex placeholderRegex("<([^>]+)>");
auto inputIter = inputs.begin();
for (std::sregex_iterator iter(story.begin(), story.end(), placeholderRegex), end; iter != end; ++iter) {
const std::string& placeholder = iter->str();
if (inputIter != inputs.end()) {
result.replace(result.find(placeholder), placeholder.length(), *inputIter);
++inputIter;
} else {
// Handle the case where there are more placeholders than inputs
break;
}
}
return result;
}
void madLib() {
string storyTemplate = "There once was a person named <name> who lived in <city>. At the age of <age>, <name> went to a collage at <college>. <Name> graduated and went to work as a <profession>. Then, <name> adopted a(n) <animal> named <pet name>. They both lived happily ever after!";
std::regex placeholderRegex("<([^>]+)>");
std::vector<std::string> placeholders;
for (std::sregex_iterator iter(storyTemplate.begin(), storyTemplate.end(), placeholderRegex), end; iter != end; ++iter) {
placeholders.push_back(iter->str());
}
std::vector<std::string> inputs;
for (const auto& placeholder : placeholders) {
std::string input;
std::cout << "Enter " << placeholder << ": ";
std::getline(std::cin, input);
inputs.push_back(input);
}
// Replace placeholders with user inputs and print the complete story
std::string completedStory = replacePlaceholders(storyTemplate, inputs);
std::cout << "Completed story:\n" << completedStory << std::endl;
2024-03-01 09:38:35 +07:00
}
int main(int argc, char** argv) {
cout << "The car that drove 375 miles and burned 15 gallons of gasoline consumed the fuel at a rate of " << milesPerGallon(15, 375) << " MPG.\n";
int maleStudents, femaleStudents = 0;
//double malePercentage, femalePercentage = 0.0;
cout << "Enter the number of male students, followed by the number of female students: ";
2024-03-04 11:54:27 +07:00
//cin >> maleStudents >> femaleStudents;
//auto [malePercentage, femalePercentage] = maleFemalePercentages(maleStudents, femaleStudents);
//cout << "The ratio of male to female students is " << setprecision(2) << (malePercentage * 10) << "/" << setprecision(2) <<(femalePercentage * 10) << endl;
2024-03-01 09:38:35 +07:00
cout << "0C is " << std::to_string(celciusToFahrenheit(0)) << "F." << endl;
cout << "100C is " << std::to_string(celciusToFahrenheit(100)) << "F." << endl;
cout << "23.8889C is " << std::to_string(celciusToFahrenheit(23.8889)) << "F." << endl;
2024-03-04 11:54:27 +07:00
monthlySalesTax(3, 2024, 100000.08);
//mathTutor();
monthlyPayments(12, 36, 10000.00, 1957.15, 11957.15);
madLib();
2024-02-28 16:11:37 +07:00
return 0;
}