gpt4 book ai didi

c - 如何在函数内调用带有参数的函数?

转载 作者:行者123 更新时间:2023-11-30 16:15:22 25 4
gpt4 key购买 nike

尝试在“PayInput”中调用函数“TaxInput”,以便我的税款和净工资可以与每个员工的个人资料一起打印。相反,它会在最后一名员工输出后打印所有“税”和“净工资”。我尝试在“PayInput”中使用参数调用它,但它无法编译。如果我取出参数,那么它会完全跳过该函数,并且根本不打印税费或 netPay。请参见下文:

Pay to: Employee1
Hours worked: 45.00
Hourly rate: $1.00
Gross pay: $52.50
Base pay: $45.00
Overtime pay: $7.50

Pay to: Employee2
Hours worked: 45.00
Hourly rate: $1.00
Gross pay: $52.50
Base pay: $45.00
Overtime pay: $7.50
Taxes paid: $10.50
Taxes paid: $10.50

我的代码:

#include <stdio.h>

void PayInput(char empNames[5][32], float empHours[], float empRates[],
float overtime[], float overtime_pay[], float gross[],
float basepay[], float netPay[], float tax[])
{
for (int i = 0; i < Times; i++)
{
printf("\n\nPay to: %s \n", empNames[i]);
printf("Hours worked: %.2f \n", empHours[i]);
printf("Hourly rate: $%.2f \n", empRates[i]);

if (empHours[i] > 40)
{
overtime[i] = empHours[i] - 40;
overtime_pay[i] = overtime[i] * (empRates[i] * 1.5);
printf("Gross pay: $%.2f \n", gross[i] = empHours[i] *
empRates[i] + overtime_pay[i]);
printf("Base pay: $%.2f \n", basepay[i] = empHours[i]
* empRates[i]);
printf("Overtime pay: $%.2f \n", overtime_pay[i]);
}
else
{
printf("Gross income: $%.2f \n", gross[i] =
empHours[i] * empRates[i]);
printf("Base pay: $%.2f \n", basepay[i] = empHours[i]
* empRates[i]);
}
}
}

void TaxInput(float gross[], float tax[], float netPay[])
{
for (int i = 0; i < Times; i++)
{
tax[i] = gross[i] * 20 / 100;
printf("Taxes paid: $%.2f \n", tax[i]);

netPay[i] = gross[i] - tax[i];
printf("Net Pay: $%.2f \n", netPay[i]);
}
}

float TotalGross=0;

void allGross(float gross[])
{
for (int i = 0; i < Times; i++)
{
TotalGross += gross[i];
}
printf("Total paid to employees: $%.2f", TotalGross);
}

int main()
{
char empNames[5][32];
float empRates[5], empHours[5], overtime[5], overtime_pay[5],
gross[5], basepay[5], tax[5], netPay[5];

NameInput(empNames, empHours, empRates);

printf("\n\n-------------------------------------------\n");
printf("-------------------------------------------\n");

PayInput(empNames, empHours, empRates, overtime, overtime_pay, gross,
basepay, tax);

TaxInput(gross, tax, netPay);

printf("\n\n");

allGross(gross);

printf("\n\n");
}

最佳答案

it is printing all "tax" … after the last employee's output.

当然是这样,因为您现在有一个函数 PayInput(),它可以循环打印所有 Times 支付数据,然后是函数 TaxInput() 循环打印所有Times 税务数据。鉴于这种不切实际的程序结构,最简单的解决方法是通过删除行来合并两个函数

    }
}

void TaxInput(float gross[], float tax[], float netPay[])
{
for (int i = 0; i < Times; i++)
{

并且仅调用

    PayInput(empNames, empHours, empRates, overtime, overtime_pay, gross, 
basepay, netPay, tax);

main()中。

除此之外,评论者关于使用结构的建议非常值得考虑。

关于c - 如何在函数内调用带有参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com