繁体   English   中英

如何在 cpp 中获得所需格式的 output?

[英]How do I get output in desired format in cpp?

我在 cpp 中编写了一段代码,它在从用户那里获取多个输入后进行一些计算。

以下是我的工作代码:

#include<iostream>
#include<string>
#include<math.h>
#include <iomanip> 
#include <cmath>
using namespace std;

int main()
{
   int a ;
   cout<<"Enter a number of entries to calculate: ";
   cin>>a;
   for(int j=1;j<=a;j++)
   {
       string name;
       cout<<"Enter the name of the borrower: ";
       cin>>name;

       float mortage_blanace;
       cout<<"Enter the mortage balance: ";
       cin>>mortage_blanace;

       float interest_rate;
       cout<<"Enter the annual interest rate: ";
       cin>>interest_rate;

       float current_monthly_payment;
       cout<<"Enter the current monthly payment: ";
       cin>>current_monthly_payment;

       float extra_monthly_payment;
       cout<<"Enter the extra monthly payment: ";
       cin>>extra_monthly_payment;

       cout<<"\n"; 

       float new_payment = current_monthly_payment+extra_monthly_payment;
       float i = (interest_rate/100)/12;
       int current_duration_in_months =  ceil((log((current_monthly_payment/i)/((current_monthly_payment/i)-mortage_blanace)))/(log(1+i)));
       int new_duration_in_months = ceil((log((new_payment/i)/((new_payment/i)-mortage_blanace)))/(log(1+i)));
       float current_interest = (current_monthly_payment*current_duration_in_months)-mortage_blanace;
       float new_interest = (new_payment*new_duration_in_months)-mortage_blanace;
       int current_duration_years = current_duration_in_months/12;
       int current_duration_months = current_duration_in_months % 12;
       int new_duration_years = new_duration_in_months/12;
       int new_duration_months = new_duration_in_months%12;
       float savings = current_interest-new_interest;
       string fees;
       if(new_duration_in_months<=current_duration_in_months/2)
       {
           fees="Extra fees";
       }
       else
       {
           fees="No Fee";
       }
      
       
       cout<<"Name         |MortageBalance  |Interest Rate |            Current               |            New                |Savings      |Fees ";
       cout<<"\n";
       cout<<"                                               Payment    Duration   Interest   | Payment  Duration  Interest                            ";
       cout<<"---------------------------------------------------------------------------------------------------------------------------------";
       std::cout << std::fixed;
       cout<<name << "       " << "$" << setprecision(2) << mortage_blanace << "        " << interest_rate << "%" << "           " << "$" << current_monthly_payment << "    " << current_duration_years << "yrs" << " " << current_duration_months << "mo" << "  " << "$" << current_interest << "    " << "$" << new_payment << " " << new_duration_years << "yrs" << " " << new_duration_months << " mo" << "  " << "$" << new_interest << "  " << "$" << savings << "      " << fees;
       cout<<"\n";

   } 
  
    return 0;
}

我的 output:

在此处输入图像描述

但我希望 output采用以下格式,我一次获取所有输入,然后在一个 go 中打印输出

在此处输入图像描述

任何帮助表示赞赏。

谢谢。

一种可能的方法是使用struct ,如下所示:

#include<iostream>
#include<string>
#include<math.h>
#include <iomanip> 
#include <cmath>
#include <vector>
struct Details
{
    std::string name,fees;
    float mortage_blanace =0,interest_rate =0,current_monthly_payment=0,extra_monthly_payment=0,new_payment=0;
   float  current_interest=0,new_interest=0,savings=0;
   int current_duration_in_months=0,new_duration_in_months=0,current_duration_years=0,current_duration_months=0,new_duration_years=0,new_duration_months=0;
};
using namespace std;

int main()
{
   int a ;
   cout<<"Enter a number of entries to calculate: ";
   cin>>a;
   
   float i;
   
   //create vector of size a 
   std::vector<Details> myVec(a);
   //iterate through the vector and fill in details
   for(Details &person: myVec)
   {
       
       cout<<"Enter the name of the borrower: ";
       cin>>person.name;

       
       cout<<"Enter the mortage balance: ";
       cin>>person.mortage_blanace;

       
       cout<<"Enter the annual interest rate: ";
       cin>>person.interest_rate;

       
       cout<<"Enter the current monthly payment: ";
       cin>>person.current_monthly_payment;

       
       cout<<"Enter the extra monthly payment: ";
       cin>>person.extra_monthly_payment;

       cout<<"\n"; 

       person.new_payment = person.current_monthly_payment+person.extra_monthly_payment;
       i = (person.interest_rate/100)/12;
       person.current_duration_in_months =  ceil((log((person.current_monthly_payment/i)/((person.current_monthly_payment/i)-person.mortage_blanace)))/(log(1+i)));
       person.new_duration_in_months = ceil((log((person.new_payment/i)/((person.new_payment/i)-person.mortage_blanace)))/(log(1+i)));
       person.current_interest = (person.current_monthly_payment*person.current_duration_in_months)-person.mortage_blanace;
       person.new_interest = (person.new_payment*person.new_duration_in_months)-person.mortage_blanace;
       person.current_duration_years = person.current_duration_in_months/12;
       person.current_duration_months = person.current_duration_in_months % 12;
       person.new_duration_years = person.new_duration_in_months/12;
       person.new_duration_months = person.new_duration_in_months%12;
       person.savings = person.current_interest-person.new_interest;
       
       if(person.new_duration_in_months<=person.current_duration_in_months/2)
       {
           person.fees="Extra fees";
       }
       else
       {
           person.fees="No Fee";
       }
      
       
       
   }
   cout<<"Name         |MortageBalance  |Interest Rate |            Current               |            New                |Savings      |Fees ";
       cout<<"\n";
       cout<<"                                               Payment    Duration   Interest   | Payment  Duration  Interest                            ";
       cout<<"---------------------------------------------------------------------------------------------------------------------------------";
   //print all the details
   for(const Details &person: myVec)
   {
       
       std::cout << std::fixed;
       cout<<person.name << "       " << "$" << setprecision(2) << person.mortage_blanace << "        " << person.interest_rate << "%" << "           " << "$" << person.current_monthly_payment << "    " << person.current_duration_years << "yrs" << " " << person.current_duration_months << "mo" << "  " << "$" << person.current_interest << "    " << "$" << person.new_payment << " " << person.new_duration_years << "yrs" << " " << person.new_duration_months << " mo" << "  " << "$" << person.new_interest << "  " << "$" << person.savings << "      " << person.fees;
       cout<<"\n";

  
   }
   
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM