|
Boost-Commit : |
From: lists.drrngrvy_at_[hidden]
Date: 2008-03-12 13:43:11
Author: drrngrvy
Date: 2008-03-12 13:43:11 EDT (Wed, 12 Mar 2008)
New Revision: 43580
URL: http://svn.boost.org/trac/boost/changeset/43580
Log:
Cleaning up example a bit.
Text files modified:
sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp | 118 +++++++++++++++++++--------------------
1 files changed, 57 insertions(+), 61 deletions(-)
Modified: sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp (original)
+++ sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp 2008-03-12 13:43:11 EDT (Wed, 12 Mar 2008)
@@ -1,6 +1,5 @@
#include <iostream>
#include <boost/cgi/acgi.hpp>
-#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <google/template.h>
@@ -8,21 +7,14 @@
std::string string_from_currency(std::string amt)
{
- using namespace boost::algorithm;
-
- erase_all_regex(amt, boost::regex("[$, ]")); // this is much too hardcore...
+ // this is much too hardcore...
+ boost::algorithm::erase_all_regex(amt, boost::regex("[$, ]"));
return amt;
}
-
-int main()
+template<typename Request>
+void fill_amortization_dictionary(google::TemplateDictionary& dict, Request& req)
{
- service s;
- request req(s);
- req.load(true);
- response resp;
-
- google::TemplateDictionary dict("example");
if (req.POST("LoanAmt").empty())
dict.SetValue("LoanAmt", "$250,000");
else
@@ -45,65 +37,69 @@
dict.ShowSection("NotAmortize");
else
{
- try {
- double P = boost::lexical_cast<double>(string_from_currency(req.POST("LoanAmt")));
- double i = boost::lexical_cast<double>(boost::algorithm::trim_copy(req.POST("YearlyIntRate"))) / 1200;
- double n = boost::lexical_cast<double>(req.POST("TermYrs")) * 12;
-
- //resp<< "P = " << P << ", i = " << i << ", n = " << n << ", ";
- double monthly_payments = (P*i) / (1 - std::pow((1+i), -n));
-
- //resp<< monthly_payments;
-
- google::TemplateDictionary* sub_dict = dict.AddSectionDictionary("RegPmtSummary");
- sub_dict->ShowSection("RegPmtSummary");
- sub_dict->SetFormattedValue("MonthlyPmt", "%.2f", monthly_payments);
-
- dict.ShowSection("Amortize");
-
- double balance = P;
- int row_num = 0;
- double interest;
- double principal_paid;
- double total_interest;
- do{
- google::TemplateDictionary* sub_dict2 = dict.AddSectionDictionary("PaymentEntry");
- sub_dict2->ShowSection("PaymentEntry");
- sub_dict2->SetFormattedValue("Payment", "%.2f", monthly_payments);
- sub_dict2->SetIntValue("ROW_NUM", ++row_num);
- sub_dict2->SetIntValue("ROW_TYPE", (row_num % 2) + 1);
- interest = balance * i;
- total_interest += interest;
- sub_dict2->SetFormattedValue("InterestPaid", "%.2f", interest);
-
- principal_paid = monthly_payments - interest;
- sub_dict2->SetFormattedValue("PrincipalPaid", "%.2f", principal_paid);
- balance -= principal_paid; // Note: balance can increase.
- sub_dict2->SetFormattedValue("Balance", "%.2f", balance);
-
- }while(balance > 0);
-
- sub_dict->SetFormattedValue("RegPmt_TotalIntPd", "%.2f", total_interest);
- sub_dict->SetFormattedValue("RegPmt_TotalPmts", "%.2f", total_interest + P);
-
- }catch(boost::bad_lexical_cast& blc)
- {
- std::cout<< "Bad lexical_cast: " << blc.what() << std::endl;
- return 0;
- }
+ double P = boost::lexical_cast<double>(string_from_currency(req.POST("LoanAmt")));
+ double i = boost::lexical_cast<double>(req.POST("YearlyIntRate")) / 1200;
+ double n = boost::lexical_cast<double>(req.POST("TermYrs")) * 12;
+ double monthly_payments = (P*i) / (1 - std::pow((1+i), -n));
+
+ google::TemplateDictionary* sub_dict = dict.AddSectionDictionary("RegPmtSummary");
+ sub_dict->ShowSection("RegPmtSummary");
+ sub_dict->SetFormattedValue("MonthlyPmt", "%.2f", monthly_payments);
+
+ dict.ShowSection("Amortize");
+
+ double balance = P;
+ int row_num = 0;
+ double interest;
+ double principal_paid;
+ double total_interest;
+ do{
+ google::TemplateDictionary* sub_dict2 = dict.AddSectionDictionary("PaymentEntry");
+ sub_dict2->ShowSection("PaymentEntry");
+ sub_dict2->SetFormattedValue("Payment", "%.2f", monthly_payments);
+ sub_dict2->SetIntValue("ROW_NUM", ++row_num);
+ sub_dict2->SetIntValue("ROW_TYPE", (row_num % 2) + 1);
+ interest = balance * i;
+ total_interest += interest;
+ sub_dict2->SetFormattedValue("InterestPaid", "%.2f", interest);
+ principal_paid = monthly_payments - interest;
+ sub_dict2->SetFormattedValue("PrincipalPaid", "%.2f", principal_paid);
+ balance -= principal_paid; // Note: balance can increase.
+ sub_dict2->SetFormattedValue("Balance", "%.2f", balance);
+ }while(balance > 0);
+
+ sub_dict->SetFormattedValue("RegPmt_TotalIntPd", "%.2f", total_interest);
+ sub_dict->SetFormattedValue("RegPmt_TotalPmts", "%.2f", total_interest + P);
}
+}
- google::Template* tmpl = google::Template::GetTemplate("example.tpl", google::STRIP_WHITESPACE);
+template<typename Request>
+void write_amortization_template(Request& req, response& resp)
+{
+ google::TemplateDictionary dict("amortization");
+
+ fill_amortization_dictionary(dict, req);
+
+ google::Template* tmpl
+ = google::Template::GetTemplate("example.tpl", google::STRIP_WHITESPACE);
std::string output;
tmpl->Expand(&output, &dict);
resp<< content_type("text/html")
<< output;
+}
- resp.send(req.client());
+int main()
+{
+ service s;
+ request req(s);
+ req.load(true);
+ response resp;
- return 0;
+ write_amortization_template(req, resp);
+
+ return_(resp, req, 0);
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk