C#格式化不同大小文本的cookie cutter输出的文本
是否有一种格式化文本的方法,我正试图使它看起来尽可能像一个工资存根,但看到的东西,如姓名/地址可以不同的人…手动格式化不适合每个人 我想,经过2年多的CLI实践,可能会有一个解决方案 目前的程序按我所希望的那样运行。这只是我试图微调的输出C#格式化不同大小文本的cookie cutter输出的文本,c#,string-formatting,command-prompt,C#,String Formatting,Command Prompt,是否有一种格式化文本的方法,我正试图使它看起来尽可能像一个工资存根,但看到的东西,如姓名/地址可以不同的人…手动格式化不适合每个人 我想,经过2年多的CLI实践,可能会有一个解决方案 目前的程序按我所希望的那样运行。这只是我试图微调的输出 using System; using System.IO; namespace Proj10 { class Program { const int ARRAY_TWO = 2; static void Main() {
using System;
using System.IO;
namespace Proj10
{
class Program
{
const int ARRAY_TWO = 2;
static void Main()
{
int count = 0;
Employee[] emp = new Employee[10];
//emp[0] = new Employee();
//emp[0].SetEmpNum(1);
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "\\";
Console.WriteLine("Enter a file name in My Documents: ");
string input = Console.ReadLine();
string path = environment + input;
StreamReader myFile = new StreamReader(path);
bool check = true;
do
{
if (myFile.EndOfStream)
{
break;
}
int num = int.Parse(myFile.ReadLine());
string name = myFile.ReadLine();
string address = myFile.ReadLine();
string hourNworked = myFile.ReadLine();
double[] values = new double[ARRAY_TWO];
sort(hourNworked, ref values);
emp[count] = new Employee();
emp[count].SetEmpNum(num);
emp[count].SetEmpName(name);
emp[count].SetEmpAddress(address);
emp[count].SetEmpHrlyPay(values[0]);
emp[count].SetEmpHrsWrked(values[1]);
//while count < 10 && !myfile.EOF();
//print loop using count
//emp[0].GetEmpNum();
//emp[0].CalcSalary();
/*Integer[] number = new Integer[5];
for (int i = 0; i < 5; i++)
number[i] = new Integer(i);
for (int i = 0; i < 5; i++)
Console.WriteLine(number[i].GetValue());
*/
count++;
} while (count < 10);
Console.WriteLine("\n\nAuthor: Daniel Demers");
Console.WriteLine("CS 1400 X01\n");
for (int i = 0; i < 2; i++)
{
Console.WriteLine("|-----------------------------------------------------------------------|");
Console.WriteLine("| Employee Number {0} |", emp[i].GetEmpNum());
Console.WriteLine("|-----------------------------------------------------------------------|");
Console.WriteLine("| Employee Name: {0} |", emp[i].GetEmpName());
Console.WriteLine("| Emplyee Address: {0} | |", emp[i].GetEmpAddress());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(emp[i].CalcSalary());
}
Console.ReadLine();
}
public static void sort(string hourNworked, ref double[] values)
{
char[] rules = { ' ' };
string[] splitArray = hourNworked.Split(rules);
values[0] = double.Parse(splitArray[0]);
values[1] = double.Parse(splitArray[1]);
}
public class Employee
{
private int empNum;
private string name;
private string address;
private double hrlyPay;
private double hrsWrkd;
private const double FED_TAX = .20;
private const double STATE_TAX = .075;
private double GROSS;
private double overTime;
private double overTimePay;
private double overTimeHours;
private double NET;
private double regTime;
private double fTaxAmount;
private double sTaxAmount;
public Employee()
{
empNum = 0;
}
public void SetEmpNum(int n)
{
empNum = n;
}
public int GetEmpNum()
{
return empNum;
}
public void SetEmpName(string n)
{
name = n;
}
public string GetEmpName()
{
return name;
}
public void SetEmpAddress(string a)
{
address = a;
}
public string GetEmpAddress()
{
return address;
}
public void SetEmpHrlyPay(double h)
{
hrlyPay = h;
}
public double GetEmpHrlyPay()
{
return hrlyPay;
}
public void SetEmpHrsWrked(double w)
{
hrsWrkd = w;
}
public double GetEmpHrsWrkd()
{
return hrsWrkd;
}
public double CalcSalary()
{
if (hrsWrkd > 40)
{
overTimeHours = hrsWrkd - 40;
overTimePay = hrlyPay * 1.5;
overTime = overTimePay * overTimeHours;
regTime = (hrsWrkd - overTimeHours) * hrlyPay;
GROSS = overTime + regTime;
fTaxAmount = (regTime + overTime) * FED_TAX;
sTaxAmount = (regTime + overTime) * STATE_TAX;
NET = GROSS - (fTaxAmount + sTaxAmount);
return NET;
}
else
{
regTime = hrlyPay * hrsWrkd;
fTaxAmount = regTime * FED_TAX;
sTaxAmount = regTime * STATE_TAX;
NET = regTime - (fTaxAmount + sTaxAmount);
return NET;
}
}
}
}
}
使用系统;
使用System.IO;
名称空间Proj10
{
班级计划
{
常数int数组_TWO=2;
静态void Main()
{
整数计数=0;
员工[]emp=新员工[10];
//emp[0]=新员工();
//emp[0].SetEmpNum(1);
字符串环境=System.environment.GetFolderPath
(System.Environment.SpecialFolder.Personal)+“\\”;
WriteLine(“在我的文档中输入文件名:”);
字符串输入=Console.ReadLine();
字符串路径=环境+输入;
StreamReader myFile=新的StreamReader(路径);
布尔检查=真;
做
{
if(myFile.EndOfStream)
{
打破
}
int num=int.Parse(myFile.ReadLine());
字符串名称=myFile.ReadLine();
字符串地址=myFile.ReadLine();
字符串hourNworked=myFile.ReadLine();
double[]值=新的double[ARRAY_TWO];
排序(工作时间、参考值);
emp[count]=新员工();
emp[count].SetEmpNum(num);
emp[count].SetEmpName(name);
emp[count].SetEmpAddress(地址);
emp[count].SetEmpHrlyPay(值[0]);
emp[count].SetEmpHrsWrked(值[1]);
//而计数<10&!myfile.EOF();
//使用计数打印循环
//emp[0].GetEmpNum();
//emp[0]。CalcSalary();
/*整数[]编号=新整数[5];
对于(int i=0;i<5;i++)
数字[i]=新整数(i);
对于(int i=0;i<5;i++)
Console.WriteLine(编号[i].GetValue());
*/
计数++;
}而(计数<10);
Console.WriteLine(“\n\n作者:Daniel Demers”);
控制台写入线(“CS 1400 X01\n”);
对于(int i=0;i<2;i++)
{
Console.WriteLine(“|-----------------------------------------------------------------------------------------|”);
Console.WriteLine(“|员工编号{0}|”,emp[i].GetEmpNum());
Console.WriteLine(“|-----------------------------------------------------------------------------------------|”);
Console.WriteLine(“|员工姓名:{0}|”,emp[i].GetEmpName());
WriteLine(“|雇员地址:{0}| |”,emp[i].GetEmpAddress());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(emp[i].CalcSalary());
}
Console.ReadLine();
}
公共静态无效排序(字符串hourNworked,ref double[]值)
{
char[]规则={''};
string[]splitArray=hourNworked.Split(规则);
值[0]=double.Parse(splitArray[0]);
值[1]=double.Parse(splitArray[1]);
}
公营雇员
{
私营企业;
私有字符串名称;
私有字符串地址;
私人双薪制;
私人双hrsWrkd;
私人康斯特双联邦税=.20;
私人建筑双州税=0.075;
私人双毛;
私人双加班;
私人双重加班费;
私人双倍加班时间;
私人双网;
私人双重注册时间;
私人双座;
私人双sTaxAmount;
公职人员()
{
empNum=0;
}
公共void SetEmpNum(int n)
{
empNum=n;
}
public int GetEmpNum()
{
返回empNum;
}
public void SetEmpName(字符串n)
{
name=n;
}
公共字符串GetEmpName()
{
返回名称;
}
public void SetEmpAddress(字符串a)
{
地址=a;
}
公共字符串GetEmpAddress()
{
回信地址;
}
公共无效SetEmpHrlyPay(双h)
{
hrlyPay=h;
}
公共双倍GetEmpHrlyPay()
{
返回hrlyPay;
}
公共无效设置已标记(双w)
{
hrsWrkd=w;
}
公共双GetEmpHrsWrkd()
{
返回hrsWrkd;
}
公共双计算器()
{
如果(hrsWrkd>40)
{
超时小时数=hrsWrkd-40;
超期支付=hrlyPay*1.5;
加班=加班费*加班时间;
regTime=(hrsWrkd-超时时间)*hrlyPay;
总=加班+注册时间;
fTaxAmount=(注册时间+加班)*联邦税;
sTaxAmount=(注册时间+加班)*州税;
净=总-(fTaxAmount+sTaxAmount);
回报网;
}
其他的
{
regTime=hrlyPay*hrsWrkd;
fTaxAmount=注册时间*联邦税;
sTaxAmount=注册时间*州税;
NET=regTime-(fTaxAmount+stataxamount);
回报网;
}
}
}
}
}
它已经存在了很长一段时间,但我想你正在寻找它。有了它,你可以将所有的字符串填充到一个固定的长度,然后所有的字符串都将对齐。@PeterLillevold-OP可能意味着带有占位符的模板。