Gve*_*222 5 c++ printf text-alignment
我正试图制作一张这样的桌子......(只是没有用来分隔每个项目的点)
每周工资表:
名称.....................标题....... ......总税收......网
Ebenezer Scrooge合伙人250.00 ..62.25 .187.75
Bob Cratchit ..........职员...... 15.00 .... 2.00 ..13.00
这是我的代码看起来像这部分....
for (int count=0; count < numberOfEmployees; count++)
{
cout << "Employee: \n";
cout << "\t Name: ";
cin.getline (employees[count].name, EMPLOYEESIZE);
cout << "\t Title: ";
cin.getline (employees[count].title, EMPLOYEESIZE);
cout << "\t SSNum: ";
cin >> employees[count].SSNum;
cout << "\t Salary: ";
cin >> employees[count].Salary;
cout << "\t Withholding Exemptions: ";
cin >> employees[count].Withholding_Exemptions;
cin.ignore();
cout << "\n";
}
double gross;
double tax;
double net;
double adjusted_income;
cout << "\n";
cout << "Weekly Payroll: \nName \t \t Title \t Gross \t Tax \t Net \n";
for (int count=0; count < numberOfEmployees; count++)
{
gross = employees[count].Salary;
adjusted_income = subtraction_times (employees[count].Salary, employees[count].Withholding_Exemptions);
tax = adjusted_income * .25;
net = subtraction (gross, tax);
printf ("\n%s", employees[count].name);
}
Run Code Online (Sandbox Code Playgroud)
我有表的第一部分(名称部分),但在那之后我现在不知道要做其余的表.谁能帮我?
谢谢
Mey*_*sam 23
您可以使用printfleft-justify标志( - ).
printf("%-10s", "title"); // this will left-align "title" in space of 10 characters
Run Code Online (Sandbox Code Playgroud)
这是一个示例程序:
#include <string>
using namespace std;
int main()
{
string name = "Bob Cratchit";
string title = "Clerk";
float gross = 15;
float tax = 2;
float net = 13;
printf("%-25s%-20s%-10s%-10s%-10s\n", "Name", "Title", "Gross", "Tax", "Net");
printf("%-25s%-20s%-10.2f%-10.2f%-10.2f\n", name.c_str(), title.c_str(), gross, tax, net);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Name Title Gross Tax Net
Bob Cratchit Clerk 15.00 2.00 13.00
Run Code Online (Sandbox Code Playgroud)
最明显的问题是:为什么使用printf,其他工具更适应?另一个经常被遗忘的问题是(最终)输出介质是什么?如果文本最终会出现在打印机上或窗口系统的文本框中,您可能会为自己的工作做好准备.此类系统上的字体很少是固定宽度,因此您必须考虑单个字符的宽度.为了输出到打印机,我建议输出LaTeX然后进行后处理.对于输出到窗口,您正在使用的库可能有某种表组件,它将为您进行格式化.
例如,如果您输出的是某个固定宽度的字体设备 - 例如远程传输,那么您可以使用iostream操纵器或用户定义的类型.(没有办法干净地完成这个printf- 你需要iostreams.)抽象地说,定义类型Name,Title
并且MonitaryAmount是最干净的解决方案.在这种情况下,您只需<<为该类型定义适当的运算符.std::string然而,使用用户定义的名称和标题类型而不仅仅是,可能是过度的,并且操纵器方法可能是首选.(在一个非常大的应用程序中,单独的类型将被证明是合理的,您可能需要在不同的上下文中输出,并且希望操纵器也指定它们.)
在最简单的解决方案中,您可以只使用两个操纵器:
TextField并且MoneyField:每个操纵器将字段宽度作为构造函数的参数,并在其<<运算符中设置适当的格式字段,例如:
class TextField
{
int myWidth;
public:
TextField( int width ) : myWidth( width ) {}
friend std::ostream&
operator<<( std::ostream& dest, TextField const& manip )
{
dest.setf( std::ios_base::left, std::ios_base::adjustfield );
dest.fill( ' ' );
dest.width( manip.myWidth );
return dest;
}
};
Run Code Online (Sandbox Code Playgroud)
和
class MoneyField
{
int myWidth;
public:
MoneyField( int width ) : myWidth( width ) {}
friend std::ostream&
operator<<( std::ostream& dest, MoneyField const& manip )
{
dest.setf( std::ios_base::right, std::ios_base::adjustfield );
dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
dest.fill( ' ' );
dest.precision( 2 );
dest.width( manip.myWidth );
return dest;
}
};
Run Code Online (Sandbox Code Playgroud)
(实际上,使用Money类可能更好.例如,你需要特殊的乘法舍入规则;如果你计算税,事实上,你可能需要使用某种十进制类型,而不是double为了满足关于如何计算的法律要求.)
无论如何,考虑到上面的操纵者,你可以写下这样的东西:
TextField name( 15 );
TextField title( 8 );
MoneyField gross( 8 );
MoneyField tax( 6 );
MoneyField net( 8 );
for ( std::vector< Employee >::const_iterator employee = employees.begin();
employee != employees.end();
++ employee ) {
std::cout << name << employee->name()
<< title << employee->title()
<< gross << employee->salary()
<< tax << calculateTax( employee->salary() )
<< net << calculateNet( employee->salary() )
<< std::endl;
}
Run Code Online (Sandbox Code Playgroud)
(这假设您已经清理了其余部分,以使其成为惯用且可维护的C++.)
| 归档时间: |
|
| 查看次数: |
32659 次 |
| 最近记录: |