Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C คำนวณเงินเดือนพนักงานขายด้วยแมโคร

ปัญหา

บริษัทผลิตแล็ปท็อปมีนโยบายค่าตอบแทนรายเดือนสำหรับพนักงานขายดังนี้:

  • เงินเดือนฐานขั้นต่ำ: 3000.00
  • โบนัสต่อเครื่องที่ขายได้: 200.00
  • ค่าคอมมิชชั่นจากยอดขายรวมรายเดือน: 5 เปอร์เซ็นต์

เนื่องจากราคาแล็ปท็อปเปลี่ยนแปลง ราคาขายแต่ละเครื่องจะถูกกำหนดต้นของทุกเดือน

วิธีแก้ปัญหา

ตรรกะการคำนวณโบนัสและคอมมิชชั่น:

bonus = BONUS_RATE * quantity;
commission = COMMISSION * quantity * price;

เงินเดือนรวม (Gross salary) คำนวณจากสูตร:

Gross salary = basic salary + (quantity * bonus rate) + (quantity * price) * commission rate

ตัวอย่างโค้ด

โปรแกรม C คำนวณเงินเดือนพนักงานขายด้วยแมโคร:

#define BASIC_SALARY 3000.00
#define BONUS_RATE 200.00
#define COMMISSION 0.05

int main() {
    int quantity;
    float gross_salary, price;
    float bonus, commission;
    printf("number of items sold and their price\n");
    scanf("%d %f", &quantity, &price);
    bonus = BONUS_RATE * quantity;
    commission = COMMISSION * quantity * price;
    gross_salary = BASIC_SALARY + bonus + commission;
    printf("\n");
    printf("Bonus = %6.2f\n", bonus);
    printf("Commission = %6.2f\n", commission);
    printf("Gross salary = %6.2f\n", gross_salary);
    return 0;
}

ผลลัพธ์

เมื่อรันโปรแกรมจะได้ผลลัพธ์:

Number of items sold and their price
20 150000
Bonus = 4000.00
Commission = 150000.00
Gross salary = 157000.00