我正在编写这个编程任务的毛钱和税收功能的输出,我正在编写以跟踪员工工资单。我只是想知道我是否正确传递参数并通过引用传递,这是我们应该使用的方法。我的班级经常不见面,这本书很糟糕,所以我很感激任何建议。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int main(){
int hoursworked[]={};
float rate[]={};
char name[][20]={};
float grosspay[]={};
float taxespaid[]={};
float sum;
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
for(int i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[]={};
for(int i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
float taxespaid[]={};
for(int i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
for (int i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
for(int i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}
答案 0 :(得分:1)
我讨厌成为坏消息的承载者,但C中没有传递参考.C定义的唯一参数传递方法是6.5.2.2p4中的传值:
在准备对函数的调用时,将计算参数,并为每个参数分配相应参数的值。
也许您打算询问C ++,在这种情况下......不,您没有使用传递引用。
否则,如果您打算询问C如何模仿传递引用,则使用指针(即参数具有指针类型和指针值)。
例如,在void userinput(char name[][20], int hoursworked[], float rate[]);
中,name
,hoursworked
和rate
都是指针类型。传递的值将是指针值。 指针值指向要修改的对象,因此可以得到相同的结果(也就是说,一旦函数返回,对数组所做的更改仍然可见。)
如果您对于为什么在技术上不符合传递参考资格而感到好奇,那就是因为一元*pointer
或array[subscript]
(或pointer[subscript]
运算符需要更改指向的值,其中 - 当使用pass-by-reference时,您应该只能分配一个值,而不是像array = value;
那样的值。如果你在编写swap
函数时将其视为测试:
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
如果您尝试调用此函数,您会注意到它没有成功交换其参数。如果你使用pass-by-value,那三行就会交换它的参数。例如,在C ++中:
void swap(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}
这个将交换参数。我不得不改变的是功能签名;我没有必要改变函数的使用方式或函数本身的代码......这是传递引用。
答案 1 :(得分:1)
当我在main()之前声明变量并使用SIZE初始化数组时,它工作正常:)
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int hoursworked[SIZE];
float rate[SIZE];
char name[SIZE][20];
float grosspay[SIZE];
float taxespaid[SIZE];
float sum;
int main(){
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
int i;
for(i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[SIZE];
int i;
for(i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
int i;
float taxespaid[SIZE];
for(i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
int i;
for (i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
int i;
for(i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}