gpt4 book ai didi

c - 错误 C2440 和错误 4024

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

在尝试为我的程序调用这两个函数时,我不断遇到错误。由于这是我第一次上课,我不知道哪里出了问题。它应该简单地根据您选择的工资率调用函数grosscal和tax cal,并使用switch case循环选择。

#include <stdio.h>
#define BASEHRS 40 // hours at pay1
#define OVERTIME 1.5 // 1.5 time
#define AMT1 300 // 1st rate tier
#define AMT2 150 // 2st rate tier
#define RATE1 0.15 // rate for 1st tier
#define RATE2 0.20 // rate for 2nd tier
#define RATE3 0.25 // rate for 3rd tier

double grossCal(double, double, double);
double taxCal(double, double);
double netCal(double, double, double);

int main(void)
{
double hours, gross, net, taxes;
double pay1, pay2, pay3, pay4;
int payrate;
pay1 = 8.75;
pay2 = 9.33;
pay3 = 10.00;
pay4 = 11.20;

printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);

printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $%.2f/hr\t\t2) $%.2f/hr\n", pay1, pay2);
printf("3) $%.2f/hr\t\t4) $%.2f/h\n", pay3, pay4);
printf("5) quit\n");
printf("*****************************************************************\n");
scanf("%d", &payrate);

switch (payrate)
{
case '1' : grossCal(&gross, pay1, hours);
taxCal(gross, &taxes);
break;
case '2' : grossCal(&gross, pay2, hours);
taxCal(gross, &taxes);
break;
case '3' : grossCal(&gross, pay3, hours);
taxCal(gross, &taxes);
break;
case '4' : grossCal(&gross, pay4, hours);
taxCal(gross, &taxes);
break;
default : break;
}

net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
return 0;
}
double grossCal(double *grossPay, double pay, double hours){
if (hours <= BASEHRS)
*grossPay = hours * pay;
else
*grossPay = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME;
}
double taxCal(double gross, double *taxestotal){
if (gross <= AMT1)
*taxestotal = gross * RATE1;
else if (gross <= AMT1 + AMT2)
*taxestotal = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
*taxestotal = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
}

最佳答案

函数声明、函数调用和函数定义不匹配。

声明

double grossCal(double, double, double);
double taxCal(double, double);

用法

  case '1' :  grossCal(&gross, pay1, hours); // &gross is not double
taxCal(gross, &taxes); // &taxes is not double.
break;
case '2' : grossCal(&gross, pay2, hours);
taxCal(gross, &taxes);
break;
case '3' : grossCal(&gross, pay3, hours);
taxCal(gross, &taxes);
break;
case '4' : grossCal(&gross, pay4, hours);
taxCal(gross, &taxes);

实现

double grossCal(double *grossPay, double pay, double hours){
...

double taxCal(double gross, double *taxestotal){
...

即使函数不返回任何内容,您的用法和实现也是匹配的。

您可以通过以下方式清理内容:

最小变化

更改声明

void grossCal(double*, double, double);
void taxCal(double, double*);

保持当前使用情况

更改实现

void grossCal(double *grossPay, double pay, double hours){
...

void taxCal(double gross, double *taxestotal){
...

稍微多一点变化,但更好

接口(interface)

// Return the gross pay
double grossCal(double pay, double hours);

// Return the taxes.
double taxCal(double);

用法

简化它,以尽量减少重复代码。添加新变量 称为付款。

  case 1 :  pay = pay1;
break;
case 2 : pay = pay2;
break;
case 3 : pay = pay3;
break;
case 4 : pay = pay4;
break;

在 switch 语句之后,

  gross = grossCal(pay, hour);
taxes = taxCal(gross);

实现

double grossCal(double pay, double hours){
if (hours <= BASEHRS)
return (hours * pay);
else
return (BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME);
}

double taxCal(double gross){
if (gross <= AMT1)
return (gross * RATE1);
else if (gross <= AMT1 + AMT2)
return (AMT1 * RATE1 + (gross - AMT1) * RATE2);
else
return (AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3);
}

关于c - 错误 C2440 和错误 4024,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184389/

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