/* * 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:30 AM */ #include #include #include #include #include #include #include #include using namespace std; /* * */ int milesPerGallon(int gallons, int milesDriven) { return milesDriven / gallons; } std::tuple maleFemalePercentages(int maleStudents, int femaleStudents) { int totalStudents = maleStudents + femaleStudents; double malePercentage = (double) maleStudents / totalStudents; double femalePercentage = (double) femaleStudents / totalStudents; return {malePercentage, femalePercentage}; } double celciusToFahrenheit(double celcius) { return 9.0 / 5.0 * celcius + 32.0; } 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& 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 who lived in . At the age of , went to a collage at . graduated and went to work as a . Then, adopted a(n) named . They both lived happily ever after!"; std::regex placeholderRegex("<([^>]+)>"); std::vector placeholders; for (std::sregex_iterator iter(storyTemplate.begin(), storyTemplate.end(), placeholderRegex), end; iter != end; ++iter) { placeholders.push_back(iter->str()); } std::vector 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; } 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: "; //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; 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; monthlySalesTax(3, 2024, 100000.08); //mathTutor(); monthlyPayments(12, 36, 10000.00, 1957.15, 11957.15); madLib(); return 0; }