我一直在研究这个学校提示:
编写一个 C++ 程序,要求用户输入每月因驾驶汽车而产生的以下费用:贷款支付、保险、汽油、机油、轮胎和维护。然后,该程序应显示这些费用的每月总费用,以及这些费用的预计年度总费用。
标记每项成本。标签应左对齐,列宽为 30 个字符。成本应右对齐并显示为小数点后两位,列宽为 15。
如果年度总额大于 1000 美元,则将年度总额的 10% 添加到年度总额中。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double loan_Payment;
double insurance;
double gas;
double oil;
double tires;
double maintenance;
/// text setup
cout.width(30); cout << left << "Loan Payment" << right << "$ " << fixed << setprecision(2);
cout.width(15); cin >> right >> loan_Payment;
cout.width(30); cout << left << "Insurance" << right << " " << fixed << setprecision(2);
cout.width(15); cin >> right >> insurance;
cout.width(30); cout << left << "Gas" << right << " " << fixed << setprecision(2);
cout.width(15); cin >> right >> gas;
cout.width(30); cout << left << "Oil" << right << " " << fixed << setprecision(2);
cout.width(15); cin >> right >> oil;
cout.width(30); cout << left << "Tires" << right << " " << fixed << setprecision(2);
cout.width(15); cin >> right >> tires;
cout.width(30); cout << left << "Maintenance" << right << " " << fixed << setprecision(2);
cout.width(15); cin >> right >> maintenance;
// adding total.yearly total, 10%
cout.width(30); cout << left << "Total" << right << "$ ";
cout.width(15); cout << left << (loan_Payment + insurance + gas + oil + tires + maintenance) << fixed << setprecision(2) << endl;
cout.width(30); cout << left << "Yearly Total" << right << "$ ";
cout.width(15); cout << left << (12 * (loan_Payment + insurance + gas + oil + tires + maintenance)) << fixed << setprecision(2) << endl;
//if yearly total is greater than 1000 add 10 percent of the yearly total
double yearly_total;
yearly_total = (12 * (loan_Payment + insurance + gas + oil + tires + maintenance));
if (yearly_total >= 1000)
{
cout.width(30); cout << left << "10%" << right << "$ ";
cout.width(15); cout << left << (12 * (loan_Payment + insurance + gas + oil + tires + maintenance)) * .10 << fixed << setprecision(2) << left << endl;
cout.width(30); cout << left << "Grand Total" << right << "$ " << left << ((12 * (loan_Payment + insurance + gas + oil + tires + maintenance)) * .10) + (12 * (loan_Payment + insurance + gas + oil + tires + maintenance)) << fixed << setprecision(2) << endl;
}
}
我从该代码得到了这个输出:
Loan Payment $ 303.28
Insurance 75.00
Gas 125.00
Oil 45.00
Tires 0.00
Maintenance 15.00
Total $ 563.28
Yearly Total $ 6759.36
10% $ 675.94
Grand Total $ 7435.30
但是我需要的输出是这样的:
Loan Payment $ 303.28
Insurance 75.00
Gas 125.00
Oil 45.00
Tires 0.00
Maintenance 15.00
Total $ 563.28
Yearly total $ 6759.36
10% $ 675.93
Grand Total $ 7435.29
如您所见,输出并不完全匹配,因为小数位没有正确对齐。我不知道如何做到这一点,因为当我将输出的前半部分输入命令提示符时,它没有将它们排列起来,我也不知道如何将它设置到它所在的位置和后半部分。我也不明白为什么它会在总计和产出的 10% 部分四舍五入,也无法找出原因。任何帮助表示赞赏!
就个人而言,我会尝试将此代码放入 for 循环中并遍历一个包含这些数字的数组,这样您就不必一遍又一遍地重写打印语句,但如果这是您必须这样做的方式,只需删除第一组“固定”关键字,并可能将第二组 setw() 设置为 10 左右。
double loan_payment = 303.28, insurance = 75.00, gas = 125.00, oil = 45.00, tires = 0.00, maintenance = 15.00, total = 563.28;
cout << setw(30) << left << "Loan Payment" << right << "$" << setw(10) << right << fixed << setprecision(2) << loan_payment << endl;
cout << setw(30) << left << "Insurance" << right << "$" << setw(10) << right << fixed << setprecision(2) << insurance << endl;
cout << setw(30) << left << "Gas" << right << "$" << setw(10) << right << fixed << setprecision(2) << gas << endl;
cout << setw(30) << left << "Oil" << right << "$" << setw(10) << right << fixed << setprecision(2) << oil << endl;
cout << setw(30) << left << "Tires" << right << "$" << setw(10) << right << fixed << setprecision(2) << tires << endl;
cout << setw(30) << left << "Maintenance" << right << "$" << setw(10) << right << fixed << setprecision(2) << maintenance << endl;
cout << setw(30) << left << "Total" << right << "$" << setw(10) << right << fixed << setprecision(2) << (loan_payment + insurance + gas + oil + tires + maintenance)`