我正在为工资单检查编写代码。功能会询问用户工作时间和小时工资。另一个函数接受小时和费率作为参数并计算并返回grosspay。然后将grosspay发送到一个函数,该函数将计算并返回payrolltax(.22)。然后调用一个接受毛钱和税收作为参数的函数并返回netpay。最后将所有数据发送到将显示所有值(totalhours,hourlyrate,grosspay,tax和netpay)的函数。我的netpay显示0,税收显示错误的答案。谁能告诉我哪里出错了?
/* Payroll Check */
#include <stdio.h>
float askhoursworked();
float askhourlyrate();
float calculategrosspay(float hours,float rate);
float calculatepayrolltax(float grosspay);
float calculatenetpay(float grosspay,float tax);
float displayallvalues(float ,float ,float,float ,float );
int main()
{
float hours;
float rate;
float grosspay;
float tax;
float netpay;
float values;
hours=askhoursworked();
rate= askhourlyrate();
tax= calculatepayrolltax( grosspay);
netpay= calculatenetpay( grosspay,tax);
grosspay=calculategrosspay( hours, rate);
values= displayallvalues( hours, rate, tax, grosspay, netpay);
return 0;
}
float askhoursworked()
{
float hours;
printf("\n Please enter hours worked \t");
scanf("%f",&hours);
return hours;
}
float askhourlyrate()
{
float rate;
printf("\n Please enter your payrate \t");
scanf("%f",&rate);
return rate;
}
float calculategrosspay(float hours,float rate)
{
float grosspay;
grosspay=hours*rate;
return grosspay;
}
float calculatenetpay(float grosspay,float tax)
{
float netpay;
netpay=grosspay-tax;
return netpay;
}
float calculatepayrolltax(float grosspay)
{
float tax;
tax=grosspay*.22;
return tax;
}
float displayallvalues(float hours,float rate,float tax,float grosspay,float netpay)
{
printf("\n Your total hours are %.2f",hours);
printf("\n Your hourly rate %.2f",rate);
printf("\n Your grosspay is %.2f",grosspay);
printf("\n Your taxes are %.2f",hours);
printf("\n Your netpay is %.2f",netpay);
}
答案 0 :(得分:1)
在C语言中,使用单通道编译器。这意味着,订单最重要。 你最后在计算毛钱。你正在用它来计算净支付和税收。
这是正确的方法..
hours = askhoursworked ();
rate = askhourlyrate ();
grosspay = calculategrosspay (hours, rate);
tax = calculatepayrolltax (grosspay);
netpay = calculatenetpay (grosspay, tax);
上查看
修改强> 您还以错误的方式打印输出。
printf ("\n Your total hours are %.2f", hours);
printf ("\n Your hourly rate %.2f", rate);
printf ("\n Your grosspay is %.2f", grosspay);
printf ("\n Your taxes are %.2f", tax);
printf ("\n Your netpay is %.2f", netpay);