c编程功能的薪水支票

时间:2018-10-13 04:07:38

标签: c

我正在努力学习如何使用C编程 现在我一直在尝试编写薪水检查程序

对你们来说会很容易。我是一个初学者

我不知道如何调用函数

你们能帮我吗?

非常感谢

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

float calculatePay(float payRate, float hours);
float hours = 50;
float payRate = 10;
float regHours;
float overtimeHours;
float regPay;
float overtimePay;
float grossPay;
float overtimeRate1 = 0.5;

int main()
{
    printf("The pay check for the first person is: %0.2f\n", calculatePay(payRate, hours), grossPay+overtimePay);
}
float calculatePay(float payRate, float hours)
{
    regPay = regHours * payRate;
    overtimePay = overtimeHours * payRate * overtimeRate1;
    grossPay = regPay + overtimePay;

    if (hours <= 40){
        regHours = hours;
        overtimeHours = 0;
        return grossPay+overtimePay;
    }
    else if (hours > 40);{
        regHours = 40;
        overtimeHours = regHours - 40;
        return grossPay+overtimePay;
    }
}

1 个答案:

答案 0 :(得分:1)

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

float calculatePay(); // Changed function prototype
// Global floats are default initialized to 0
float hours = 50;
float payRate = 10;
float regHours;
float overtimeHours;
float regPay;
float overtimePay;
float grossPay;
float overtimeRate1 = 0.5;
int main() {
  printf("Enter gross pay, over time pay and overtime hours:\n");
  if (scanf("%f %f %f", &grossPay, &overtimePay, &overtimeHours)<3) {
    perror("input failure for gross pay, over time pay and overtime");
    exit(EXIT_FAILURE);
  }
  printf("The pay check for the first person is: %0.2f\n",
         calculatePay()); // No need to pass arguments, since they are global
                          // variables
}
float calculatePay() {
  regPay = regHours * payRate;
  overtimePay = overtimeHours * payRate * overtimeRate1;
  grossPay = regPay + overtimePay;
  if (hours <= 40) {
    regHours = hours;
    overtimeHours = 0;
    return grossPay + overtimePay;
  }
  // else { No need of else condition since you are returning in if
  regHours = 40;
  overtimeHours = regHours - 40;
  return grossPay + overtimePay;
  //}
}


说明(对于新手;请阅读链接的文档):

  • 您已使用的变量在any函数外部声明,因此为global variables
  • 您可以在函数(local variables)中声明变量,并将其作为参数传递给calculatePay函数。
  • maindefault function,它是在您运行C程序时执行的。
  • 您可以使用scanf函数从输入中读取值;不要忘记测试其退货数量。
  • 您可以使用printf功能将消息打印到屏幕上;由于stdout已被缓冲(通常是行缓冲),因此您通常希望以\n结束其格式控制字符串(或适当地调用fflush)。
  • %d%f等被称为格式说明符,它们在printfscanf的字符串中使用,它们表示变量的数据类型,例如int,float,等等。
  • 如果在void foo(int, int);函数之后定义了函数(也称为函数声明),则必须编写函数的原型(如main)。
  • 您可以根据相应的function prototype调用像function_name(argument1,arguments2,...)这样的函数。

阅读How to debug small programs。不要忘记在编译时启用所有警告和调试信息(并改进您的代码以获取任何警告)。如果使用GCC,请编译with gcc -Wall -Wextra -Wno-prototypes -g