236 lines
24 KiB
HTML
236 lines
24 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
<html>
|
||
<head>
|
||
<title>main.cpp</title>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<style type="text/css">
|
||
<!--
|
||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||
.ST0 {color: #808080; background-color: #323232}
|
||
.comment {color: #808080}
|
||
.preprocessor {color: #009b00}
|
||
.whitespace {color: #505050}
|
||
.ST6 {color: #808080; font-family: monospace; font-weight: bold}
|
||
.pragma-omp-keyword-directive {color: #2e92c7}
|
||
.ST2 {font-family: monospace; font-weight: bold}
|
||
.ST3 {color: #6a8759; font-family: monospace; font-weight: bold}
|
||
.string {color: #6a8759}
|
||
.number {color: #6897bb}
|
||
.ST5 {color: #6a8759; background-color: #364135}
|
||
.ST4 {color: #6a8759; background-color: #364135; font-family: monospace; font-weight: bold}
|
||
.ST1 {color: #287bde}
|
||
.literal {color: #cc7832}
|
||
-->
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Cpp/Assignments/MP1_CalebFontenot/main.cpp</td></tr></table>
|
||
<pre>
|
||
<span class="ST0">/*</span>
|
||
<span class="comment">* </span><span class="comment">Click</span> <span class="ST1">nbfs</span><span class="ST1">://</span><span class="ST1">nbhost</span><span class="ST1">/</span><span class="ST1">SystemFileSystem</span><span class="ST1">/</span><span class="ST1">Templates</span><span class="ST1">/</span><span class="ST1">Licenses</span><span class="ST1">/</span><span class="ST1">license</span><span class="ST1">-</span><span class="ST1">default</span><span class="ST1">.</span><span class="ST1">txt</span> <span class="comment">to</span> <span class="comment">change</span> <span class="comment">this</span> <span class="comment">license</span>
|
||
<span class="comment">* </span><span class="comment">Click</span> <span class="ST1">nbfs</span><span class="ST1">://</span><span class="ST1">nbhost</span><span class="ST1">/</span><span class="ST1">SystemFileSystem</span><span class="ST1">/</span><span class="ST1">Templates</span><span class="ST1">/</span><span class="ST1">cppFiles</span><span class="ST1">/</span><span class="ST1">main</span><span class="ST1">.</span><span class="ST1">cc</span> <span class="comment">to</span> <span class="comment">edit</span> <span class="comment">this</span> <span class="comment">template</span>
|
||
<span class="comment">*/</span>
|
||
|
||
<span class="comment">/*</span>
|
||
<span class="comment">* </span><span class="comment">File</span><span class="comment">: </span><span class="comment">main</span><span class="comment">.</span><span class="comment">cpp</span>
|
||
<span class="comment">* </span><span class="comment">Author</span><span class="comment">: </span><span class="comment">caleb</span>
|
||
<span class="comment">*</span>
|
||
<span class="comment">* </span><span class="comment">Created</span> <span class="comment">on</span> <span class="comment">February</span><span class="comment"> 26, 2024, 11:30 </span><span class="comment">AM</span>
|
||
<span class="comment">*/</span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><iomanip></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><cstdlib></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><iostream></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><tuple></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><cmath></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><boost/algorithm/string.hpp></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><regex></span>
|
||
<span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string"><vector></span>
|
||
<span class="literal">using</span> <span class="literal">namespace</span> std;
|
||
|
||
<span class="comment">/*</span>
|
||
<span class="comment">*</span>
|
||
<span class="comment">*/</span>
|
||
|
||
<span class="literal">int</span> <span class="ST2">milesPerGallon</span>(<span class="literal">int</span> gallons, <span class="literal">int</span> milesDriven) {
|
||
<span class="literal">return</span> milesDriven / gallons;
|
||
}
|
||
|
||
std::tuple<<span class="literal">double</span>, <span class="literal">double</span>> <span class="ST2">maleFemalePercentages</span>(<span class="literal">int</span> maleStudents, <span class="literal">int</span> femaleStudents) {
|
||
<span class="literal">int</span> totalStudents = maleStudents + femaleStudents;
|
||
<span class="literal">double</span> malePercentage = (<span class="literal">double</span>) maleStudents / totalStudents;
|
||
<span class="literal">double</span> femalePercentage = (<span class="literal">double</span>) femaleStudents / totalStudents;
|
||
<span class="literal">return</span> {malePercentage, femalePercentage};
|
||
}
|
||
|
||
<span class="literal">double</span> <span class="ST2">celciusToFahrenheit</span>(<span class="literal">double</span> celcius) {
|
||
<span class="literal">return</span> <span class="number">9.0</span> / <span class="number">5.0</span> * celcius + <span class="number">32.0</span>;
|
||
}
|
||
|
||
std::<span class="pragma-omp-keyword-directive">string</span> <span class="ST2">getMonthName</span>(<span class="literal">int</span> month) {
|
||
std::<span class="pragma-omp-keyword-directive">string</span> returnString = <span class="ST3">"</span><span class="ST3">"</span>;
|
||
<span class="literal">switch</span> (month) {
|
||
<span class="literal">case</span> <span class="number">1</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">January</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">2</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">February</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">3</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">March</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">4</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">April</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">5</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">May</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">6</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">June</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">7</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">July</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">8</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">August</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">9</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">September</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">10</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">October</span><span class="ST3">"</span>;
|
||
<span class="literal">case</span> <span class="number">11</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">November</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
<span class="literal">case</span> <span class="number">12</span>:
|
||
returnString = <span class="ST3">"</span><span class="string">December</span><span class="ST3">"</span>;
|
||
<span class="literal">break</span>;
|
||
}
|
||
<span class="literal">return</span> returnString;
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">prettyPrintSalesTax</span>(<span class="literal">int</span> month, <span class="literal">int</span> year, <span class="literal">int</span> total, <span class="literal">double</span> totalIncome, <span class="literal">double</span> stateSalesTax, <span class="literal">double</span> countySalesTax, <span class="literal">double</span> totalSalesTax) {
|
||
printf(<span class="ST3">"</span><span class="string">%s %d</span><span class="ST3">\n</span><span class="ST3">"</span>, getMonthName(month).c_str(), year);
|
||
printf(<span class="ST4">"</span><span class="ST5">---------------------------------------</span><span class="ST4">\n</span><span class="ST4">"</span>);
|
||
printf(<span class="ST3">"</span><span class="string">Total collected:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">5</span>, totalIncome);
|
||
printf(<span class="ST3">"</span><span class="string">Sales:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">16</span>, stateSalesTax);
|
||
printf(<span class="ST3">"</span><span class="string">County Sales tax:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">5</span>, countySalesTax);
|
||
printf(<span class="ST3">"</span><span class="string">Total sales tax:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">6</span>, totalSalesTax);
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">monthlySalesTax</span>(<span class="literal">int</span> month, <span class="literal">int</span> year, <span class="literal">double</span> total) {
|
||
<span class="literal">double</span> totalIncome = total / <span class="number">1.06</span>;
|
||
<span class="literal">double</span> stateSalesTax = total * <span class="number">0.04</span>;
|
||
<span class="literal">double</span> countySalesTax = total * <span class="number">0.02</span>;
|
||
<span class="literal">double</span> totalSalesTax = stateSalesTax + countySalesTax;
|
||
prettyPrintSalesTax(month, year, total, totalIncome, stateSalesTax, countySalesTax, totalSalesTax);
|
||
}
|
||
|
||
<span class="literal">bool</span> <span class="ST2">is_integer</span>(<span class="literal">const</span> std::<span class="pragma-omp-keyword-directive">string</span> & s) {
|
||
<span class="literal">return</span> std::regex_match(s, std::regex(<span class="ST3">"</span><span class="string">[-+]?[0-9]+</span><span class="ST3">"</span>));
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">mathTutor</span>() {
|
||
<span class="pragma-omp-keyword-directive">string</span> userInput = <span class="ST3">"</span><span class="ST3">"</span>;
|
||
<span class="literal">int</span> rand1, rand2 = <span class="number">0</span>;
|
||
<span class="literal">do</span> {
|
||
cout << <span class="ST3">"</span><span class="string">Enter Q/q to quit.</span><span class="ST3">"</span> << endl;
|
||
rand1 = (rand() % <span class="number">100</span>);
|
||
rand2 = (rand() % <span class="number">100</span>);
|
||
cout << <span class="ST3">"</span><span class="string">What</span><span class="string">'</span><span class="string">s </span><span class="ST3">"</span> << rand1 << <span class="ST3">"</span><span class="string"> + </span><span class="ST3">"</span> << rand2 << <span class="ST3">"</span><span class="string">?</span><span class="ST3">"</span> << endl;
|
||
cin >> userInput;
|
||
boost::algorithm::to_lower(userInput);
|
||
|
||
<span class="literal">if</span> (is_integer(userInput)) {
|
||
<span class="literal">if</span> (rand1 + rand2 == stoi(userInput)) {
|
||
cout << <span class="ST3">"</span><span class="string">You are correct!</span><span class="ST3">"</span> << endl;
|
||
} <span class="literal">else</span> {
|
||
cout << <span class="ST3">"</span><span class="string">Sorry, the answer is </span><span class="ST3">"</span> << (rand1 + rand2) << <span class="ST3">"</span><span class="string">. Try again.</span><span class="ST3">"</span> << endl;
|
||
}
|
||
}
|
||
} <span class="literal">while</span> (userInput.compare(<span class="ST3">"</span><span class="string">q</span><span class="ST3">"</span>) != <span class="number">0</span>);
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">prettyPrintMonthlyPayments</span>(<span class="literal">double</span> payment, <span class="literal">double</span> rate, <span class="literal">double</span> numOfPayments, <span class="literal">double</span> loanAmount, <span class="literal">double</span> annualRate, <span class="literal">double</span> amountPaid, <span class="literal">double</span> amountPaidBack) {
|
||
printf(<span class="ST4">"</span><span class="ST5">---------------------------------------</span><span class="ST4">\n</span><span class="ST4">"</span>);
|
||
printf(<span class="ST3">"</span><span class="string">Loan amount:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">15</span>, loanAmount);
|
||
printf(<span class="ST3">"</span><span class="string">Monthly Interest Rate:%*c%.0f%%</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">14</span>, rate);
|
||
printf(<span class="ST3">"</span><span class="string">Number of Payments:%*c%.0f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">17</span>, numOfPayments);
|
||
printf(<span class="ST3">"</span><span class="string">Monthly Payment:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">12</span>, payment);
|
||
printf(<span class="ST3">"</span><span class="string">Amount Paid Back:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">11</span>, amountPaidBack);
|
||
printf(<span class="ST3">"</span><span class="string">Interest Paid:%*c$ %.2f</span><span class="ST3">\n</span><span class="ST3">"</span>, <span class="number">14</span>, amountPaid);
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">monthlyPayments</span>(<span class="literal">double</span> annualRate, <span class="literal">double</span> numOfPayments, <span class="literal">double</span> loanAmount, <span class="literal">double</span> amountPaid, <span class="literal">double</span> amountPaidBack) {
|
||
<span class="literal">double</span> rate = (annualRate / <span class="number">12.0</span>);
|
||
<span class="literal">double</span> topHalf = rate * pow((<span class="number">1.0</span> + rate), numOfPayments);
|
||
<span class="literal">double</span> bottomHalf = pow((<span class="number">1.0</span> + rate), numOfPayments) - <span class="number">1.0</span>;
|
||
<span class="literal">double</span> payment = (topHalf / bottomHalf) * loanAmount;
|
||
prettyPrintMonthlyPayments(payment, rate, numOfPayments, loanAmount, annualRate, amountPaid, amountPaidBack);
|
||
}
|
||
|
||
<span class="comment">//</span> <span class="comment">Function</span> <span class="comment">to</span> <span class="comment">replace</span> <span class="comment">placeholders</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">story</span> <span class="comment">with</span> <span class="comment">user</span> <span class="comment">inputs</span>
|
||
std::<span class="pragma-omp-keyword-directive">string</span> <span class="ST2">replacePlaceholders</span>(<span class="literal">const</span> std::<span class="pragma-omp-keyword-directive">string</span>& story, <span class="literal">const</span> std::vector<std::<span class="pragma-omp-keyword-directive">string</span>>& inputs) {
|
||
std::<span class="pragma-omp-keyword-directive">string</span> result = story;
|
||
std::regex placeholderRegex(<span class="ST3">"</span><span class="string"><([^>]+)></span><span class="ST3">"</span>);
|
||
<span class="literal">auto</span> inputIter = inputs.begin();
|
||
|
||
<span class="literal">for</span> (std::sregex_iterator iter(story.begin(), story.end(), placeholderRegex), end; iter != end; ++iter) {
|
||
<span class="literal">const</span> std::<span class="pragma-omp-keyword-directive">string</span>& placeholder = iter->str();
|
||
<span class="literal">if</span> (inputIter != inputs.end()) {
|
||
result.replace(result.find(placeholder), placeholder.length(), *inputIter);
|
||
++inputIter;
|
||
} <span class="literal">else</span> {
|
||
<span class="comment">//</span> <span class="comment">Handle</span> <span class="comment">the</span> <span class="comment">case</span> <span class="comment">where</span> <span class="comment">there</span> <span class="comment">are</span> <span class="comment">more</span> <span class="comment">placeholders</span> <span class="comment">than</span> <span class="comment">inputs</span>
|
||
<span class="literal">break</span>;
|
||
}
|
||
}
|
||
<span class="literal">return</span> result;
|
||
}
|
||
|
||
<span class="literal">void</span> <span class="ST2">madLib</span>() {
|
||
<span class="pragma-omp-keyword-directive">string</span> storyTemplate = <span class="ST3">"</span><span class="string">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!</span><span class="ST3">"</span>;
|
||
|
||
std::regex placeholderRegex(<span class="ST3">"</span><span class="string"><([^>]+)></span><span class="ST3">"</span>);
|
||
std::vector<std::<span class="pragma-omp-keyword-directive">string</span>> placeholders;
|
||
<span class="literal">for</span> (std::sregex_iterator iter(storyTemplate.begin(), storyTemplate.end(), placeholderRegex), end; iter != end; ++iter) {
|
||
placeholders.push_back(iter->str());
|
||
}
|
||
|
||
std::vector<std::<span class="pragma-omp-keyword-directive">string</span>> inputs;
|
||
<span class="literal">for</span> (<span class="literal">const</span> <span class="literal">auto</span>& placeholder : placeholders) {
|
||
std::<span class="pragma-omp-keyword-directive">string</span> input;
|
||
std::cout << <span class="ST3">"</span><span class="string">Enter </span><span class="ST3">"</span> << placeholder << <span class="ST3">"</span><span class="string">: </span><span class="ST3">"</span>;
|
||
std::getline(std::cin, input);
|
||
inputs.push_back(input);
|
||
}
|
||
|
||
<span class="comment">//</span> <span class="comment">Replace</span> <span class="comment">placeholders</span> <span class="comment">with</span> <span class="comment">user</span> <span class="comment">inputs</span> <span class="comment">and</span> <span class="comment">print</span> <span class="comment">the</span> <span class="comment">complete</span> <span class="comment">story</span>
|
||
std::<span class="pragma-omp-keyword-directive">string</span> completedStory = replacePlaceholders(storyTemplate, inputs);
|
||
std::cout << <span class="ST3">"</span><span class="string">Completed story:</span><span class="ST3">\n</span><span class="ST3">"</span> << completedStory << std::endl;
|
||
|
||
}
|
||
|
||
<span class="literal">int</span> <span class="ST2">main</span>(<span class="literal">int</span> argc, <span class="literal">char</span>** argv) {
|
||
cout << <span class="ST3">"</span><span class="string">The car that drove 375 miles and burned 15 gallons of gasoline consumed the fuel at a rate of </span><span class="ST3">"</span> << milesPerGallon(<span class="number">15</span>, <span class="number">375</span>) << <span class="ST3">"</span><span class="string"> MPG.</span><span class="ST3">\n</span><span class="ST3">"</span>;
|
||
<span class="literal">int</span> maleStudents, femaleStudents = <span class="number">0</span>;
|
||
<span class="comment">//</span><span class="comment">double</span> <span class="comment">malePercentage</span><span class="comment">, </span><span class="comment">femalePercentage</span><span class="comment"> = 0</span><span class="comment">.</span><span class="comment">0;</span>
|
||
cout << <span class="ST3">"</span><span class="string">Enter the number of male students, followed by the number of female students: </span><span class="ST3">"</span>;
|
||
<span class="comment">//</span><span class="comment">cin</span><span class="comment"> >> </span><span class="comment">maleStudents</span><span class="comment"> >> </span><span class="comment">femaleStudents</span><span class="comment">;</span>
|
||
<span class="comment">//</span><span class="comment">auto</span><span class="comment"> [</span><span class="comment">malePercentage</span><span class="comment">, </span><span class="comment">femalePercentage</span><span class="comment">] = </span><span class="comment">maleFemalePercentages</span><span class="comment">(</span><span class="comment">maleStudents</span><span class="comment">, </span><span class="comment">femaleStudents</span><span class="comment">);</span>
|
||
<span class="comment">//</span><span class="comment">cout</span> <span class="ST6"><< "The ratio of male to female students is " << setprecision(2) << (malePercentage * 10) << "/" << setprecision(2) <<(femalePercentage * 10) << endl;</span>
|
||
cout << <span class="ST3">"</span><span class="string">0C is </span><span class="ST3">"</span> << std::to_string(celciusToFahrenheit(<span class="number">0</span>)) << <span class="ST3">"</span><span class="string">F.</span><span class="ST3">"</span> << endl;
|
||
cout << <span class="ST3">"</span><span class="string">100C is </span><span class="ST3">"</span> << std::to_string(celciusToFahrenheit(<span class="number">100</span>)) << <span class="ST3">"</span><span class="string">F.</span><span class="ST3">"</span> << endl;
|
||
cout << <span class="ST3">"</span><span class="string">23.8889C is </span><span class="ST3">"</span> << std::to_string(celciusToFahrenheit(<span class="number">23.8889</span>)) << <span class="ST3">"</span><span class="string">F.</span><span class="ST3">"</span> << endl;
|
||
monthlySalesTax(<span class="number">3</span>, <span class="number">2024</span>, <span class="number">100000.08</span>);
|
||
<span class="comment">//</span><span class="comment">mathTutor</span><span class="comment">();</span>
|
||
monthlyPayments(<span class="number">12</span>, <span class="number">36</span>, <span class="number">10000.00</span>, <span class="number">1957.15</span>, <span class="number">11957.15</span>);
|
||
madLib();
|
||
<span class="literal">return</span> <span class="number">0</span>;
|
||
}
|
||
|
||
|
||
</pre></body>
|
||
</html>
|