gpt4 book ai didi

c - 为什么我的代码中出现段错误,您能解释一下按引用传递和按值传递吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:20 26 4
gpt4 key购买 nike

我正在用 C 编写程序。我正在尝试了解按引用传递和按值传递之间的区别。我不确定我是否做对了。

我在第二次执行 do while 循环后遇到段错误/核心转储错误。我在循环中遗漏了什么?

这是我的主要内容:

#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <iostream>

extern void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit);
extern float calcGross(float hours, float payrate);
extern void InputEmplData(char *lastname, char *firstname, float *hours, float *payrate, float *defr);
extern void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef, float *totnet);
extern void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt, float pr, float *totpayrate, float theGross, float *totgross,float ft, float *totfed, float st, float *totstate, float ssit, float *totssi, float defr, float *totdef, float net, float *totnet);

int main(void)
{
float fedtax,statetax,ssitax;
float theGross;
//int counter=0;
char answer;
char lastname, firstname;
float hours, payrate, defr, reghrs, ovthrs, net, numemps;
float totgross, totpayrate, totreg, totovt, totrate, totfed, totstate, totssi, totdef, totnet;

/*
FILE * reportFile; // 1) declare a FILE * variable

reportFile = fopen("./report.txt","wt");
// 2) open a report file with access mode "write-text"
if (reportFile == NULL)
{
printf(" Report file open failed ...\n");
fflush(stdin);
getchar();
exit(-10); // terminate w/ failure code -10; reqs <stdlib>
}
*/
printf(" Welcome to the Employee Tax Program\n");
printf(" Please enter the number of employees taxes will be calculated:");
scanf (" %f", numemps);
InitAcc(&totreg, &totovt, &totrate, &totgross, &totfed, &totstate, &totssi, &totdef, &totnet);

do
{
InputEmplData(&lastname, &firstname, &hours, &payrate, &defr);
theGross = calcGross(hours, payrate); // call 3.4
CalculateTaxes(theGross,defr,&fedtax,&statetax,&ssitax); // 3.5
net=(theGross-defr)-fedtax-statetax-ssitax;
AddDetail2Acc(reghrs, &totreg, ovthrs, &totovt, payrate, &totpayrate, theGross, &totgross,fedtax, &totfed, statetax, &totstate, ssitax, &totssi, defr, &totdef, net, &totnet);
printf(" Fedtax = %8.2f",fedtax);
//fprintf(reportFile," Fedtax = %8.2f",fedtax);
//fprintf(stdout," Fedtax = %8.2f",fedtax);
printf(" Statetax = %8.2f",statetax);
//fprintf(reportFile," Statetax = %8.2f",statetax);
//fprintf(stdout," Statetax = %8.2f",statetax);
printf(" SSItax = %8.2f\n",ssitax);
//fprintf(reportFile," SSItax = %8.2f\n",ssitax);
//fprintf(stdout," SSItax = %8.2f\n",ssitax);
//printf(" %f %f %f %f %f %f %f %f", totreg, totovt, totrate, totfed, totstate, totssi, totdef, totnet);
printf(" Do you have another employee(YES = 1 / No = 2) ==> ");
fflush(stdin);
scanf (" %c", answer);
answer = getchar();
getchar(); // removes '\n' from response

} while (answer == 'y' || answer == 'y');

//fclose(reportFile); // 4) close the file
printf(" Press any key ... ");
fflush(stdin);
getchar();
return 0;
}

这是我的功能:

#include <stdio.h>

void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr);

void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr)
{
printf(" Please enter your lastname: ");
scanf (" %s", lastname);
fflush(stdin);

printf(" Please enter your firstname: ");
scanf (" %s", firstname);
fflush(stdin);

printf(" Please enter the hours you have worked including overtime: ");
scanf (" %f", hours);
while (hours < 0)
{
printf(" Please enter a valid number. Must be between 0.");
scanf (" %f", hours);
}
printf(" Please enter your payrate(15.00): ");
scanf (" %f", payrate);
while (payrate <= 0)
{
printf(" Please enter a valid number. Must be above 0.");
scanf (" %f", payrate);
}
printf(" Please enter the amount exempt from taxes: ");
scanf (" %f", defr);

while (defr <= 0)
{
printf(" Please enter a valid number. Must be above 0.");
scanf (" %f", defr);
}
}

CalculateTaxes 函数:

#include "./taxrates.h" //user defined header for tax rates

void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit);
float calcFed(float gross, float defr);
float calcState(float ft);
float calcSSI(float gross,float defr);

void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit)
{
*ft = calcFed(gross,defr);
*st = calcState(*ft);
*ssit = calcSSI(gross,defr);
}

float calcFed(float gross, float defr)
{
return (gross-defr)*FEDTAXRATE;
}

float calcState(float ft)
{
return ft * STATETAXRATE;
}

float calcSSI(float gross,float defr)
{
return (gross-defr)*SSITAXRATE;
}


float calcGross(float h, float pr);

float calcGross(float h, float pr)
{
return h <= 40? h * pr : 40 *h + (h-40)*1.5*pr;
}

初始化计数器的函数:

void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef,  float *totnet);

void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef, float *totnet)
{
*totreg = 0;
*totovt = 0;
*totrate = 0;
*totgross = 0;
*totfed = 0;
*totstate = 0;
*totssi = 0;
*totdef = 0;
*totnet = 0;
}

使用计数器的函数(未完成):

#include <stdio.h>

void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt,
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet);

void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt,
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet)
{
*totgross+= theGross;
*totreg += reghrs;
*totovt += ovthrs;
*totpayrate += pr;
*totfed += ft;
*totstate += st;
*totssi += ssit;
*totdef += defr;
*totnet += net;

}

生成文件:

calc.obj:   calctaxes.cpp taxrates.h
g++ -c calctaxes.cpp -o calc.obj

main2.exe: main2.cpp calc.obj cGross.obj InputEmplData.obj InitAcc.obj
g++ main2.cpp calc.obj cGross.obj InputEmplData.obj InitAcc.obj AddDetail2Acc.obj -o main2.exe

cGross.obj: calcGross.cpp InputEmplData.obj
g++ -c calcGross.cpp -o cGross.obj

InputEmplData.obj: InputEmplData.cpp
g++ -c InputEmplData.cpp -o InputEmplData.obj

InitAcc.obj: InitAcc.cpp InputEmplData.obj
g++ -c InitAcc.cpp InputEmplData.obj -o InitAcc.obj

AddDetail2Acc.obj: AddDetail2Acc.cpp
g++ -c AddDetail2Acc.cpp InitAcc.obj -o AddDetail2Acc.obj

最佳答案

这里确实有很多代码,但这是我看到的第一个错误:

//...
extern void InputEmplData(char *lastname, char *firstname, float *hours, float *payrate, float *defr);
//...

char lastname, firstname;

//...
InputEmplData(&lastname, &firstname, &hours, &payrate, &defr);

//...
void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr)
{
printf(" Please enter your lastname: ");
scanf (" %s", lastname);
fflush(stdin);

printf(" Please enter your firstname: ");
scanf (" %s", firstname);
fflush(stdin);

除非您不假设 lastnamefirstname 的长度为 1,否则这是您的第一个错误。您正在接收段错误,因为您正在将长度大于 1 的字符串写入 char。

可能的更正是定义:

char lastname[NAME_LENGTH], firstname[NAME_LENGTH];

/...
InputEmplData(lastname, firstname, &hours, &payrate, &defr);

有了这个,您将达到预期的效果。我为 lastnamefirstname 使用了 char 数组,但是您可以使用 malloclastname 等动态分配内存,但我不建议您在不清楚自己在做什么的情况下这样做。

此外,我只有几个小建议,没有考虑段错误:

1.尝试创建一些结构,以表示不同的变量组。然后你可以写,例如,而不是

void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt, 
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet);

像这样:

void AddDetail2Acc(AccStruct *in_out_sourceAcc, AddDataStruct in_addData);

这更具可读性且不易出错。

2.据我所知,您正在使用货币值执行一些操作的变量名称。尽量不要使用 float 变量来表示货币值。 Floating point arithmetic有一天可以给你一个小小的惊喜。尝试使用整数类型。

关于c - 为什么我的代码中出现段错误,您能解释一下按引用传递和按值传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5769610/

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