【问题标题】:Displaying contracted hours and overtime in C在 C 中显示合同工时和加班时间
【发布时间】:2020-11-09 19:12:00
【问题描述】:

我正在编写一个程序,询问工作时间和工资率,然后计算他们的收入。如果这个人工作超过 40 小时,他们就会开始赚取加班费。

如何让它显示最多 40 小时的总收入,不包括加班费? "salary=hours<=40*rate;" 以 1 美元的价格返回。任何帮助将不胜感激!

#include <stdio.h>


int main(void)
{
    int hours;
    float rate,salary,totalPay, tax, netPay, overtime;
    printf("Enter # of hours worked: ");
    scanf("%d",&hours);
    

    printf("Enter hourly rate of the worker: ");
    scanf("%f",&rate);


    if (hours>40)
      {
        salary=hours<=40*rate;
         overtime=(hours-40)*rate*1.2;
         printf("Salary is $%.2f\n",salary);
         printf("overtime is $%.2f\n",overtime);
      }
      
    else
     { 
       salary=hours*rate;
       printf("Salary is $%.2f\n",salary);
       printf("No overtime has been earned\n");
     }


    totalPay=salary+overtime;
    printf("The total pay is $%.2f\n",totalPay);


     tax= totalPay*.25;
     printf("The amount of tax deducted is $%.2f\n",tax);

     netPay=totalPay-tax;
     printf("The net pay is $%.2f\n",netPay);
    return 0;

    


}

【问题讨论】:

  • 嗨。欢迎来到堆栈溢出。您能否编辑您的问题以提供最小可重复的示例?例如,可以编译的最小工作代码?有一些歧义(比如变量的数据类型)
  • salary=hours&lt;=40*rate; --> salary=40*rate;
  • 这太简单了,我什至没有想到!我倾向于过度思考事情。非常感谢您的帮助!

标签: c if-statement numeric


【解决方案1】:

以下不是你想要的:

salary=hours<=40*rate;

它分组为salary = (hours &lt;= (40 * rate)),它将hours40 * rate 进行比较,因此它将评估为0 (false) 或1 (true),具体取决于值。只需将其更改为:

salary = 40*rate;

你已经知道hours &gt;= 40,所以你只想在这里使用4040 以上的金额用于下一行的加班费。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多