我还在学习但......我不认为这些功能:
void CalculateGross(float hours, float payrate, float *gross) //3.4
float CalculateGross(float hours, float payrate) //3.4
做同样的事情。那么哪种做法更好?我假设空虚更好?
/*
3.0 Payroll Application
3.1 PrintReportHeadings(inout reportFile as file)
3.2 InitializeAccumulators(out totreg, totovt,totpayrate,totgross,totfed,
totstate,totssi,totdefr, totnet as real)
3.3 InputEmployeeData(out lastname, firstname as string,
out hours, payrate, defr as real)
3.4 CalculateGross(in hours, payrate as real, out gross as real)
3.5 computeTaxes(in g,d as real, out ft, st, ssit as real)
3.5.1 cFed(in g,d as real, out fed as real)
3.5.2 cState(in ft as real,out state as real)
3.5.3 cSSI(in g,d as real, out ssi as real)
3.7 PrintSummaryReport( ......)
3.7.1 printTotals( ....)
3.7.2 printAverages( ....)
*/
#include <stdio.h>
#include "TAXRATES.h"
void InputEmployeeData(char *lastname,char *firstname, // 3.3
float *hours,float *payrate, float *defr);
void CalculateGross(float hours, float payrate, float * gross); //3.4
float CalculateGross(float hours,float payrate); //3.4
extern void computeTaxes(float g,float d,float * ft,float *st,float *ssit); //3.5
int main(void)
{
char ln[15+1];
char fn[10+1];
float fed,state,ssi;
float g,h,p,d,n;
InputEmployeeData(&ln[0],&fn[0],&h,&p,&d); // call 3.3
g = CalculateGross(h,p); // call 3.4
// vs
//CalculateGross(40.00,25.00,&g); // alternate call 3.4
computeTaxes(g,d,ADDR(fed),ADDR(state),ADDR(ssi)); // call 3.5
n = g-fed-state-ssi-d;
printf(" Fed = %8.2f\n",fed);
printf(" State = %8.2f\n",state);
printf(" SSI = %8.2f\n",ssi);
printf(" Net = %8.2f\n",n);
while(getchar() != '\n'); // flush(stdin)
return 0;
}
void CalculateGross(float hours, float payrate, float * gross) //3.4
{
if (hours <= 40)
*gross = hours * payrate;
else
*gross = 40* payrate + 1.5 * payrate * (hours-40);
}
float CalculateGross(float hours,float payrate) .. //3.4
{
if (hours <= 40)
return hours * payrate;
else
return 40* payrate + 1.5 * payrate * (hours-40);
}
void InputEmployeeData(char *lastname,char *firstname, // 3.3
float *hours,float *payrate, float *defr)
{
printf(" Enter the name ==> ");
scanf("%s%s",firstname,lastname);
printf(" Enter the hours and payrate ==> ");
scanf("%f%f",hours,payrate);
printf(" Enter the deferred earning amount ==> ");
scanf("%f",defr);
}
答案 0 :(得分:3)
函数应该有输入和单个输出。虚空说做点什么。指针很棒,但意图是计算一些东西。当然,尽可能使用f(x)= y,而不是f(x,out y)。
答案 1 :(得分:1)
void CalculateGross(float hours, float payrate, float * gross); //3.4
float CalculateGross(float hours,float payrate); //3.4
其中:
void CalculateGross(float hours, float payrate, float * gross) //3.4
{
if (hours <= 40)
*gross = hours * payrate;
else
*gross = 40* payrate + 1.5 * payrate * (hours-40);
}
此处 - 它对您的代码没有任何影响。 (你会得到警告..但没有影响)为什么?函数名前的type
定义函数的return
类型。如果你这样做,你会得到什么:
foo = CalculateGross (....);
由于CalculateGross
不返回任何值(它仅在内部对值进行操作),因此返回类型在其他任何位置都没有影响。
哪个是对的?这是一个不同的问题。 void
对于未返回任何值的所有函数都是正确的。指定float
时,编译器将检查return somevalue;
的函数。如果没有找到,它将产生警告(或错误)。
因此,如果函数返回一个值(或指针),则将type
与函数返回的类型相匹配。如果您的函数没有返回任何内容,void
是正确的返回类型。
注意:仅因为您声明函数void
并不意味着您无法在void函数中使用return
。您可以单独使用return;
使函数在该点返回( bail out )而不返回值。