我很难在计算每小时的每周薪水时弄清楚我的这个守则

发布于 2025-01-24 06:18:45 字数 725 浏览 0 评论 0原文

#include <stdio.h>

int main() {
    const int a = 1200;
    int b = 40;
    int c = 250;
    int hour;

    printf("Enter total hours\n");
    scanf("%d", &hour);

    switch(hour){
        case 1:
            if(hour==40)
                printf("Your week pay is:%d.\n",a);
            break;

        case 2:
            if(hour>40)
                printf("Your week pay is: %d.\n", (hour-40)*b);
            break;

        case 3:
            if(hour>55)
                printf("Your week pay is: %d.\n", a+((hour-40)*b)+c);
            break;
    }

    return 0;
}

我正在计算每小时员工的每周薪水,该员工的常规小时费率为30美元,加班费为40美元。前40个小时使用常规费用支付,然后使用加班费用支付后续时间。还有危险工资,当员工工作超过55小时时,他们的额外费用为250美元。

#include <stdio.h>

int main() {
    const int a = 1200;
    int b = 40;
    int c = 250;
    int hour;

    printf("Enter total hours\n");
    scanf("%d", &hour);

    switch(hour){
        case 1:
            if(hour==40)
                printf("Your week pay is:%d.\n",a);
            break;

        case 2:
            if(hour>40)
                printf("Your week pay is: %d.\n", (hour-40)*b);
            break;

        case 3:
            if(hour>55)
                printf("Your week pay is: %d.\n", a+((hour-40)*b)+c);
            break;
    }

    return 0;
}

I'm computing for weekly salary for an hourly employee, the employee have a regular hourly rate of $30 and have an overtime rate of $40. The first 40 hours are paid using regular rate and the succeeding hours are paid using the overtime rate. There is also the hazard pay which when the employee works more than 55 hours their is additional $250.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2025-01-31 06:18:45

无需使用Switch语句。
您的代码永远不会显示任何输出。
如果/else ,则可以使用,如果/else。因此,删除交换机语句,然后尝试以下操作:

#include <stdio.h>

int main() {
    const int a = 1200;
    int b = 40;
    int c = 250;
    int hour;
    
    printf("Enter total hours\n");
    scanf("%d", &hour);
    
    if(hour > 55)
        printf("Your week pay is: %d.\n", a + ((hour-40)*b)+c);
    else if(hour > 40 )
        printf("Your week pay is: %d.\n", a + (hour-40)*b);
    else
        printf("Your week pay is:%d.\n", a);
    
    return 0;

} 

There is no need to use a switch statement.
Your code will never show any output.
You can use if/else if/else if/else. So, remove the switch statement and Try this:

#include <stdio.h>

int main() {
    const int a = 1200;
    int b = 40;
    int c = 250;
    int hour;
    
    printf("Enter total hours\n");
    scanf("%d", &hour);
    
    if(hour > 55)
        printf("Your week pay is: %d.\n", a + ((hour-40)*b)+c);
    else if(hour > 40 )
        printf("Your week pay is: %d.\n", a + (hour-40)*b);
    else
        printf("Your week pay is:%d.\n", a);
    
    return 0;

} 
青芜 2025-01-31 06:18:45

数学?

这是该程序正在尝试做什么;无论如何,它都不会起作用,因为它以给定的形式不正确。给出了逐步速率线性函数。给定的y,付款金额和x,工作时间,应该:

  • 相交。
  • 由直线组成; c2 在任何地方都应该是0;
  • 在40h时扭结;
  • 在55h时不连续;
  • 严格地单调(一个人希望至少工资。)

从问题中,案例为:0h&lt; = x&lt; = 40h,40h&lt; X&lt; = 55h,55h&lt; x。明显的角案例是0h,40h和55h,至少应该事先在纸上找出测试程序(我以41h,56h和60h的速度投掷)。可能会有所帮助。

将其放入程序中

switch语句是不合适的,因为这是测试平等;这是一系列值,因此如果块,这将更适合作为一系列。由于值单调增加,因此可以建立薪水,

pay = 0
if 55 < x
    pay += bonus
if 40 < x
    pay += ...
    x = 40
...

或者,正如您所注意到的,40h付款的部分超过40h,可以使用积分,$ 30/h * 40h =将案例分开。 $ 1200,并使用移位x直接进行计算。

if 55 < x
    pay = bonus + ...
else if 40 < x
    pay = 1200 + ...
else
    pay = ...

Math?

This is what the programme is attempting to do; it will not work, anyways, because it is incorrect in it's given form. We are given a stepwise-rate linear function. Given y, the payment amount, and x, the hours worked, It should:

  • intersect the origin;
  • be composed of straight lines; C2 should be 0 everywhere it's differentiable;
  • have a kink upwards at 40h;
  • have a discontinuity at 55h;
  • be strictly monotonic, (one would hope at least minimum wage.)

From the problem, the cases are: 0h <= x <= 40h, 40h < x <= 55h, 55h < x. Apparent corner cases are 0h, 40h, and 55h, at least, that one should figure out on paper beforehand to test this programme, (I'd throw in 41h, 56h, and 60h.) Doing a quick sketch of the graph on paper will probably be helpful.

Putting it into a programme

A switch statement is inappropriate because that's testing equality; this is a range of values, so this will be better suited to be a series of if blocks. Since the values are monotonically increasing, one could build up the pay,

pay = 0
if 55 < x
    pay += bonus
if 40 < x
    pay += ...
    x = 40
...

Or, as you have noticed, the section of 40h pay is a constant over 40h, one can separate the cases using the integral, $30/h * 40h = $1200, and calculate it directly using a shifted x.

if 55 < x
    pay = bonus + ...
else if 40 < x
    pay = 1200 + ...
else
    pay = ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文